SQL Dynamic使用内部变量创建过程

时间:2014-04-10 14:03:24

标签: sql sql-server stored-procedures dynamic-sql

我的任务是构建一个带有2个参数的SP,每年并根据这些参数创建一个带有一些数据的临时表。

我设法做了没有变量的代码但是当我尝试将它放入一个过程并使用exec时我得到一个错误。

字符串'“

后面的未闭合引号
CREATE TABLE #rezultat2 (
    CodClient char(8) not null, PRIMARY KEY ( CodClient),
    Denumire varchar(100) not null DEFAULT '',
    VanzariIan decimal(22,6) DEFAULT 0,
    VanzariFeb decimal(22,6) DEFAULT 0,
    VanzariMar decimal(22,6) DEFAULT 0,
    VanzariApr decimal(22,6) DEFAULT 0,
    VanzariMai decimal(22,6) DEFAULT 0,
    VanzariIun decimal(22,6) DEFAULT 0,
    VanzariIul decimal(22,6) DEFAULT 0,
    VanzariAug decimal(22,6) DEFAULT 0,
    VanzariSep decimal(22,6) DEFAULT 0,
    VanzariOct decimal(22,6) DEFAULT 0,
    VanzariNoe decimal(22,6) DEFAULT 0,
    VanzariDec decimal(22,6) DEFAULT 0,
)


WHILE @counter <= 12
BEGIN
    IF ( @couter = 1 ) SET @luna = ' + 'd.VanzariIan' + '
        else if ( @couter = 2) SET @luna = ' + 'd.VanzariFeb' + '
            else if( @couter = 3) SET @luna = ' + 'd.VanzariMar' + '
                else if (@couter = 4) SET @luna = ' + 'd.VanzariApr' + ' 
                    else if ( @couter = 5) SET @luna =' +  'd.VanzariMai' + '
                        else if( @couter = 6) SET @luna = ' +'d.VanzariIun' + '
                            else if (@couter = 7) SET @luna ='+  'd.VanzariIul' +'
                                else if (@couter = 8) SET @luna ='+ 'd.VanzariAug' + '
                                    else if( @couter = 9) SET @luna = '+  'd.VanzariSep' + '
                                        else if (@couter = 10) SET @luna =' +  'd.VanzariOct' + '
                                            else if ( @couter = 11) SET @luna = ' + 'd.VanzariNoe' +'
                                                else if( @couter = 12) SET @luna = ' + 'd.VanzariDec' + '

update d 
set @luna = x.Vanzari
from #rezultat2 d , (SELECT d.CodTert, sum(d1.Cantitate*d1.PretVinzare) as Vanzari
FROM GEMsc106Antet  d left outer join GEMsc106Pozitii d1 on  d.Luna=d1.Luna and d.NumarI=d1.NumarI
where year(d.Data) = ' + @an + ' and MONTH(d.Data) ='  + @couter +'
group by d.CodTert) as x 
where d.CodClient= x.CodTert 
INSERT INTO #rezultat2 (CodClient , substring(@luna,3,10) )
    SELECT  b.CodTert, b.Vanzari from (SELECT d.CodTert, sum(d1.Cantitate*d1.PretVinzare) as Vanzari
FROM GEMsc106Antet  d left outer join GEMsc106Pozitii d1 on  d.Luna=d1.Luna and d.NumarI=d1.NumarI
where year(d.Data) = ' + @an + ' and MONTH(d.Data) ='  + @couter +'
group by d.CodTert) as b left outer join #rezultat2  as r on r.CodClient=b.CodTert
    WHERE NOT EXISTS ( SELECT CodClient from (SELECT d.CodTert, sum(d1.Cantitate*d1.PretVinzare) as Vanzari
FROM GEMsc106Antet  d left outer join GEMsc106Pozitii d1 on  d.Luna=d1.Luna and d.NumarI=d1.NumarI
where year(d.Data) =' + @an + '  and MONTH(d.Data) ='  + @couter + '
group by d.CodTert) as  a where  a.CodTert=r.CodClient)
END '

有人帮我吗?

1 个答案:

答案 0 :(得分:1)

我打赌逃避单一报价存在问题。请注意,如果你想要它在一个字符串里,你必须加倍。考虑下面的例子

declare @str nvarchar(100) = 'Denumire varchar(100) not null DEFAULT   '''    
print @str    
set @str = 'Denumire varchar(100) not null DEFAULT '''''    
print @str  

输出

Denumire varchar(100) not null DEFAULT '

Denumire varchar(100) not null DEFAULT ''

注意第一个结果只包含一个'字符。 在构建动态查询时,很容易错过它。所以我建议在执行之前打印查询。我相信你会找到一些应该被转义的单引号。

希望它有所帮助!