自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Transact-SQL語句遍歷結(jié)果集的三種方法

數(shù)據(jù)庫 SQL Server
遍歷是我們經(jīng)常需要實(shí)現(xiàn)的功能,下文就教您三種使用Transact-SQL語句遍歷結(jié)果集的方法,希望對您能夠有所幫助。

Transact-SQL語句是可以實(shí)現(xiàn)遍歷的,有三種方法使用可以通過使用Transact-SQL語句遍歷一個(gè)結(jié)果集。下面就為您詳細(xì)介紹Transact-SQL語句遍歷結(jié)果集的幾種方法,供您參考。

一種方法是使用temp表。使用這種方法您創(chuàng)建的初始的SELECT語句的"快照"并將其用作基礎(chǔ)"指針"。例如:

  1. /**//********** example 1 **********/   
  2.  
  3. declare @au_id char( 11 )  
  4.  
  5. set rowcount 0  
  6. select * into #mytemp from authors  
  7.  
  8. set rowcount 1  
  9.  
  10. select @au_idau_id = au_id from #mytemp  
  11.  
  12. while @@rowcount <> 0  
  13. begin  
  14.     set rowcount 0  
  15.     select * from #mytemp where au_id = @au_id  
  16.     delete #mytemp where au_id = @au_id  
  17.  
  18.     set rowcount 1  
  19.     select @au_idau_id = au_id from #mytemp<BR/> 
  20. end  
  21. set rowcount 0  

第二個(gè)的方法是表格的一行"遍歷"每次使用 Min 函數(shù)。此方法捕獲添加存儲(chǔ)的過程開始執(zhí)行之后, 假設(shè)新行必須大于當(dāng)前正在處理在查詢中的行的***標(biāo)識(shí)符的新行。例如:

  1. /**//********** example 2 **********/   
  2.  
  3. declare @au_id char( 11 )  
  4.  
  5. select @au_id = min( au_id ) from authors  
  6.  
  7. while @au_id is not null  
  8. begin  
  9.     select * from authors where au_id = @au_id  
  10.     select @au_id = min( au_id ) from authors where au_id > @au_id  
  11. end  

注意 : 兩個(gè)示例1和2,則假定源表中的每個(gè)行***的標(biāo)識(shí)符存在。在某些情況下,可能存在沒有***標(biāo)識(shí)符 如果是這種情況,您可以修改temp表方法使用新創(chuàng)建的鍵列。例如:

  1. /**//********** example 3 **********/   
  2.  
  3. set rowcount 0  
  4. select NULL mykey, * into #mytemp from authors  
  5.  
  6. set rowcount 1  
  7. update #mytemp set mykey = 1 
  8.  
  9. while @@rowcount > 0  
  10. begin  
  11.     set rowcount 0  
  12.     select * from #mytemp where mykey = 1 
  13.     delete #mytemp where mykey = 1 
  14.     set rowcount 1  
  15.     update #mytemp set mykey = 1 
  16. end  
  17. set rowcount 0  

 

 

 

【編輯推薦】

動(dòng)態(tài)sql中使用臨時(shí)表的實(shí)例

Oracle存儲(chǔ)過程使用動(dòng)態(tài)SQL

SQL Server刪除視圖的兩種方法

SQL Server視圖的使用

SQL SERVER內(nèi)部函數(shù)大全

責(zé)任編輯:段燃 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-09-10 10:39:00

SQL變量聲明

2010-10-20 13:52:07

SQL Server數(shù)

2021-09-10 18:09:42

SQL注入漏洞網(wǎng)絡(luò)攻擊

2010-09-25 14:38:29

SQL分頁

2010-09-10 10:32:31

SQL變量Transact

2010-11-10 13:28:06

SQL Server刪

2009-07-08 12:56:32

編寫Servlet

2011-06-10 10:43:12

Ubuntu應(yīng)用安裝

2009-06-23 10:45:18

Hibernate支持

2022-06-19 23:39:58

機(jī)器學(xué)習(xí)數(shù)據(jù)集驗(yàn)證策略

2010-11-09 14:35:48

SQL Server查

2009-12-11 18:49:39

預(yù)算編制博科資訊

2022-07-13 16:06:16

Python參數(shù)代碼

2011-04-18 15:32:45

游戲測試測試方法軟件測試

2010-09-14 15:10:49

CSS注釋

2023-08-14 17:58:13

RequestHTTP請求

2024-11-15 07:00:00

Python發(fā)送郵件

2010-09-25 15:07:08

SQL插入語句

2010-09-06 09:11:24

SQLUPDATE語句

2020-06-17 10:52:00

DDoS攻擊網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)