Question

My task is to build a SP that takes 2 parameters , year and based on these it should create a temp table with some data .

I managed to do it the code without variabiles but when I try to put it into a procedure and use the exec I get an error.

"Unclosed quotation mark after the character string ' "

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 '

CAn someone help me?

Was it helpful?

Solution

I bet there's a problem with escaping a single quote. Please note if you want it inside a string you have to double it. Consider the example below

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

The output is

Denumire varchar(100) not null DEFAULT '

Denumire varchar(100) not null DEFAULT ''

Notice the first result contains only one ' character. While building a dynamic query it's easy to miss it. So I suggest printing the query before it's executed. I'm sure you will find some single quotes which should be escaped.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top