problems with errors on SQL SERVER -
i have issue query in sql server, have wrote dynamic query:
declare @cont int declare @sqlquery varchar(1000) declare @sqlquery2 varchar(500) declare @sqlquery3 varchar(2000) declare @anho varchar(4) set nocount on drop table ti set @anho = (select year(getdate())) set @cont = 1 set @sqlquery = '' while @cont <= 12 begin set @sqlquery2 = '(a.['+@anho + right('00'+cast(@cont varchar),2)+']+b.['+@anho + right('00'+cast(@cont varchar),2)+']+c.['+@anho + right('00'+cast(@cont varchar),2)+']+d.['+@anho + right('00'+cast(@cont varchar),2)+']) ['+@anho + right('00'+cast(@cont varchar),2)+'],' exec (@sqlquery2) set @sqlquery = @sqlquery + @sqlquery2 exec (@sqlquery) set @cont = @cont + 1 end exec(@sqlquery) set @sqlquery3 = 'select a.gestion,'+@sqlquery+' '+quotename('ges08','''')+' cod_ges ti llamadas_mensual_oro_final inner join llamadas_mensual_plata_final b on a.gestion = b.gestion inner join llamadas_mensual_reten_final c on b.gestion = c.gestion inner join llamadas_mensual_cable_final d on c.gestion = d.gestion' exec(@sqlquery3) set nocount off select * ti
well, issue have when execute query store procedure, it's work's well, when execute code, sql server show me error message this:
msg 102, level 15, state 1, line 1 incorrect syntax near 'a'.
the query work's ok, want hide error message, can me issue?
i have no idea why work in stored procedure.
your first expression:
exec (@sqlquery2)
is being run on following strings:
(a.[201301]+b.[201301]+c.[201301]+d.[201301]) [201301], (a.[201302]+b.[201302]+c.[201302]+d.[201302]) [201302], (a.[201303]+b.[201303]+c.[201303]+d.[201303]) [201303], (a.[201304]+b.[201304]+c.[201304]+d.[201304]) [201304], (a.[201305]+b.[201305]+c.[201305]+d.[201305]) [201305], (a.[201306]+b.[201306]+c.[201306]+d.[201306]) [201306], (a.[201307]+b.[201307]+c.[201307]+d.[201307]) [201307], (a.[201308]+b.[201308]+c.[201308]+d.[201308]) [201308], (a.[201309]+b.[201309]+c.[201309]+d.[201309]) [201309], (a.[201310]+b.[201310]+c.[201310]+d.[201310]) [201310], (a.[201311]+b.[201311]+c.[201311]+d.[201311]) [201311], (a.[201312]+b.[201312]+c.[201312]+d.[201312]) [201312],
none of these valid sql statements. can see, failure on a
not being recognized.
my best guess code in stored procedure not code showing in question. possibly being left out.
edit:
you want code this:
declare @cont int; declare @sqlquery varchar(1000); declare @sqlquery2 varchar(500); declare @sqlquery3 varchar(2000); declare @anho varchar(4); set nocount on drop table ti set @anho = (select year(getdate())) set @cont = 1 set @sqlquery = 'select a.gestion, ' set @sqlquery = while @cont <= 12 begin set @sqlquery2 = '(a.['+@anho + right('00'+cast(@cont varchar),2)+']+b.['+@anho + right('00'+cast(@cont varchar),2)+']+c.['+@anho + right('00'+cast(@cont varchar),2)+']+d.['+@anho + right('00'+cast(@cont varchar),2)+']) ['+@anho + right('00'+cast(@cont varchar),2)+'], ' --exec (@sqlquery2) set @sqlquery = @sqlquery + @sqlquery2 set @cont = @cont + 1 end set @sqlquery = @sqlquery + ' 'ges08' cod_ges llamadas_mensual_oro_final inner join llamadas_mensual_plata_final b on a.gestion = b.gestion inner join llamadas_mensual_reten_final c on b.gestion = c.gestion inner join llamadas_mensual_cable_final d on c.gestion = d.gestion' print @sqlquery; exec(@sqlquery);
in particular, need understand difference between executing string , printing out.
Comments
Post a Comment