我在 SQL SERVER 中的查询有问题,我写了这个动态查询:
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 as varchar),2)+']+b.['+@anho + RIGHT('00'+cast(@cont as varchar),2)+']+c.['+@anho + RIGHT('00'+cast(@cont as varchar),2)+']+d.['+@anho + RIGHT('00'+cast(@cont as varchar),2)+']) as ['+@anho + RIGHT('00'+cast(@cont as 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','''')+' as COD_GES into ti from Llamadas_Mensual_Oro_Final a 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 * from ti
好吧,我遇到的问题是当我使用存储过程执行查询时,它工作得很好,但是当我只执行代码时,SQL SERVER 向我显示如下错误消息:
消息 102,级别 15,状态 1,第 1 行 'a' 附近的语法不正确。
查询工作正常,但我想隐藏错误消息,有人可以帮我解决这个问题吗?