2016-08-12 168 views
1

我有動態SQL的問題。 當我使用Print (@sql)和manualy複製粘貼&,它完美的作品,但使用exec (@sql)exec sp_executesql @sql如何在SQL Server中使用動態查詢執行sql

在這個例子中我使用的系統表sys.types

你有什麼想法我做錯了嗎?

CREATE TABLE [dbo].pomocnicza 
(okres VARCHAR(5) PRIMARY KEY 
, idWiersza INT 
, cnt INT 
) 

INSERT INTO [dbo].pomocnicza(okres, idWiersza, cnt) 
SELECT okres, idWiersza, cnt FROM(SELECT '07_03'okres, 2 idWiersza, 1 cnt 
UNION 
SELECT '07_04', 3, 2 
UNION 
SELECT '07_07', 6, 3 
UNION 
SELECT '07_10', 9, 4 
UNION 
SELECT '07_14', 13, 5) t 

和動態SQL:

DECLARE @sql VARCHAR(max) 
, @sqlSub VARCHAR(max) 
, @cnt INT = 0 
, @cntSub INT = 2 
, @cnt_total INT = 0 
, @okres VARCHAR(5) 
, @idWiersza INT; 

SELECT @cnt_total = COUNT(1) FROM [dbo].pomocnicza 

WHILE @cnt <= @cnt_total 
BEGIN 
    SELECT @okres = okres, @idWiersza = idWiersza FROM [dbo].pomocnicza WHERE cnt = @cnt 
    SET @sql = 'select distinct name, schema_id, ''' + @okres 
       + ''' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       ' 
    WHILE @cntSub <= @idWiersza 
    BEGIN 
     SET @sqlSub = @sqlSub + ' or isnull(lead(max_length,' + CAST(@cntSub AS VARCHAR) + ') over (partition by scale,precision order by precision),0)=0' 
     SET @cntSub = @cntSub + 1; 
    END 
     SET @sql = @sql + @sqlSub + ' then 0 else 1 end) all_period_available FROM sys.types' 

    if @cnt+1 <= @cnt_total 
    begin 
     SET @sql = @sql + ' 
     union all 
     '; 
    end 


    SET @cnt = @cnt + 1; 
    SET @sqlSub = '' 
    SET @cntSub = 2 

print (@sql) 
exec (@sql)  
END; 

根據什麼我執行我有不同的錯誤

1)只有exec (@sql)

消息:

Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 

(34 row(s) affected) 

結果:

select distinct name, schema_id, '07_03' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
select distinct name, schema_id, '07_04' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
select distinct name, schema_id, '07_07' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,4) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,5) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,6) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
select distinct name, schema_id, '07_10' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,4) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,5) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,6) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,7) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,8) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,9) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near 'all'. 
select distinct name, schema_id, '07_14' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,4) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,5) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,6) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,7) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,8) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,9) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,10) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,11) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,12) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,13) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 

(34 row(s) affected) 

結果::34行只從最後工會

3)只從最後工會

2)

print (@sql) 
exec (@sql) 

消息34級的行 只有print (@sql) 我有完美的工作SQL:

select distinct name, schema_id, '07_03' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

select distinct name, schema_id, '07_04' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

select distinct name, schema_id, '07_07' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,4) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,5) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,6) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

select distinct name, schema_id, '07_10' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,4) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,5) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,6) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,7) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,8) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,9) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
     union all 

select distinct name, schema_id, '07_14' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       or isnull(lead(max_length,2) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,3) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,4) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,5) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,6) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,7) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,8) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,9) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,10) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,11) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,12) over (partition by scale,precision order by precision),0)=0 or isnull(lead(max_length,13) over (partition by scale,precision order by precision),0)=0 then 0 else 1 end) all_period_available FROM sys.types 
+2

有什麼錯誤訊息?打印@sql時獲得的文字是什麼? –

+2

「不起作用」是什麼意思?拋出一個錯誤?,錯誤的結果?,什麼? – Lamak

+0

那個內在'while'看起來不太好... –

回答

1

你需要移動EXEC (@sql)外While循環,即 - 你的最後三行..

print (@sql) 
exec (@sql)  
END; 

..need更改爲。 。

END; 
print (@sql) 
exec (@sql) 

而且,你的第一個@sql =聲明必須@sql = @sql +像你在其他地方。由於您的陳述中有UNION,因此您只需要執行@sql一次。爲了實現這個功能,您需要在腳本的開始部分輸入set @sql = ''

這一修正的整個腳本將看起來像這樣:

DECLARE @sql VARCHAR(max) = '' 
, @sqlSub VARCHAR(max) = '' 
, @cnt INT = 1 
, @cntSub INT = 2 
, @cnt_total INT = 0 
, @okres VARCHAR(5) 
, @idWiersza INT; 

SELECT @cnt_total = COUNT(1) FROM [dbo].pomocnicza 

WHILE @cnt <= @cnt_total 
BEGIN 
    SELECT @okres = okres, @idWiersza = idWiersza FROM [dbo].pomocnicza WHERE cnt = @cnt 
    SET @sql = @sql + 'select distinct name, schema_id, ''' + @okres 
       + ''' as okres, getdate() as czas 
       , (case when scale<>0 then 100 else 0 end) scale 
       , (case when precision>=4 
       ' 
    WHILE @cntSub <= @idWiersza 
    BEGIN 
     SET @sqlSub = @sqlSub + ' or isnull(lead(max_length,' + CAST(@cntSub AS VARCHAR) + ') over (partition by scale,precision order by precision),0)=0' 
     SET @cntSub = @cntSub + 1; 
    END 
     SET @sql = @sql + @sqlSub + ' then 0 else 1 end) all_period_available FROM sys.types' 

    if @cnt+1 <= @cnt_total 
    begin 
     SET @sql = @sql + ' 
     union all 
     '; 
    end 


    SET @cnt = @cnt + 1; 
    SET @sqlSub = '' 
    SET @cntSub = 2 

END; 
print (@sql) 
exec (@sql) 
+0

當我用你的更正執行代碼時,我只看到空的消息部分和「查詢成功執行」。你有同樣的結果嗎? –

+0

我只是略微調整了腳本。 「@sqlsub」需要由「@sqlsub」=「'初始化」,就像「@sql」一樣。另外「@cnt」應該從1開始,因爲「cnt」從不爲0. –

+0

現在它完美地工作。非常感謝你:) –