2012-02-24 73 views
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 
+1

爲什麼在它有任何內容之前從#source中選擇? – 2012-02-24 07:20:06

+0

我不會選擇什麼時候有任何東西。在dbo.Source的第一條記錄中,ITEMCode不是空的或不是。所以ststement不會執行第一次。當循環進行第二次der #source有數據時 – 2012-02-24 07:22:16

+2

如果你描述這個代碼的總體目的是什麼,你可能會得到更好的迴應。閱讀代碼很困難。 – 2012-02-24 07:24:02

回答

0

即使上面指定的查詢可能的工作,我寫在Excel中的宏,我們快一刀了ň在不到5分鐘

輸出
1

你可以使用一個recursiveCTE

WITH 
    Source (Row_ID, Document, ITEMCode, Text) AS (
    SELECT 2, '10223', '20235' , 'aaaa' UNION ALL 
    SELECT 3, ''  , ''  , 'bbbb' UNION ALL 
    SELECT 4, ''  , ''  , 'cccc' UNION ALL 
    SELECT 5, '10278', '202475', 'xxxx' UNION ALL 
    SELECT 6, '10278', '202475', 'yyyy' UNION ALL 
    SELECT 7, '10278', '202475', 'yyy' 
), 
    ranked AS (
    SELECT 
     *, 
     rnk = ROW_NUMBER() OVER (ORDER BY Row_ID) 
    FROM Source 
), 
    filled AS (
    SELECT 
     Row_ID, 
     Document, 
     ITEMCode, 
     Text, 
     rnk 
    FROM ranked 
    WHERE rnk = 1 
    UNION ALL 
    SELECT 
     r.Row_ID, 
     Document = ISNULL(NULLIF(r.Document, ''), f.Document), 
     ITEMCode = ISNULL(NULLIF(r.ITEMCode, ''), f.ITEMCode), 
     r.Text, 
     r.rnk 
    FROM ranked r 
     INNER JOIN filled f ON r.rnk = f.rnk + 1 
) 
SELECT 
    Row_ID, 
    Document, 
    ITEMCode, 
    Text 
FROM filled 

輸出:

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 

如果DocumentITEMCode實際上是整數,而不是字符串,上面的腳本會的工作,但通常會得到更好的改變這兩條線:

Document = ISNULL(NULLIF(r.Document, ''), f.Document), 
ITEMCode = ISNULL(NULLIF(r.ITEMCode, ''), f.ITEMCode), 

這樣的:

Document = ISNULL(NULLIF(r.Document, 0), f.Document), 
ITEMCode = ISNULL(NULLIF(r.ITEMCode, 0), f.ITEMCode), 
相關問題