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?