【问题标题】:SQL Dynamic creating a procedure with internal variablesSQL 动态创建具有内部变量的过程
【发布时间】:2014-04-10 14:03:23
【问题描述】:

我的任务是构建一个带有 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 '

有人可以帮我吗?

【问题讨论】:

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


    【解决方案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 ''
    

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

    希望对你有帮助!

    【讨论】:

    • 它确实有帮助,没有解决我的问题,但它有帮助。无论如何,我设法自己做到了。谢谢
    猜你喜欢
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2022-01-16
    相关资源
    最近更新 更多