1
我的查詢如下SQL服務器:任何人都可以簡化這一邏輯
declare @row_id int = 2
declare @last_row_id int =(select MAX(Row_ID) from dbo.Source)
create table #source (
Row_ID float null,
[Document] [nvarchar](255) NULL,
[ITEMCode] [nvarchar](255) NULL,
[Text] [nvarchar](255) NULL)
while(@row_id<=(@last_row_id))
begin
declare @Document nvarchar(255)
declare @itemcode nvarchar(255)
select @itemcode=ITEMCode,@Document=Document from dbo.Source where [email protected]_id
if ((@itemcode='' or @itemcode is null))
select @itemcode=ITEMCode,@Document=Document from #source where [email protected]_id-1
insert into #source
select Row_ID,@Document,@itemcode,[Text]
from dbo.Source where [email protected]_id
print @row_id
set @row_id= @row_id+1
end
select * from #source
drop table #source
目前我的桌子已經347000行。其採取一個多小時,讓我最終輸出。這個查詢怎麼做得更快。誰能幫忙?
要求:
來源:
Row_ID Document ITEMCode Text
2 10223 20235 aaaa
3 bbbb
4 cccc
5 10278 202475 xxxx
6 yyyy
7 yyy
輸出應該是:
Row_ID Document ITEMCode Text
2 10223 20235 aaaa
3 10223 20235 bbbb
4 10223 20235 cccc
5 10278 202475 xxxx
6 10278 202475 yyyy
7 10278 202475 yyy
爲什麼在它有任何內容之前從#source中選擇? – 2012-02-24 07:20:06
我不會選擇什麼時候有任何東西。在dbo.Source的第一條記錄中,ITEMCode不是空的或不是。所以ststement不會執行第一次。當循環進行第二次der #source有數據時 – 2012-02-24 07:22:16
如果你描述這個代碼的總體目的是什麼,你可能會得到更好的迴應。閱讀代碼很困難。 – 2012-02-24 07:24:02