0

我在 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' 附近的语法不正确。

查询工作正常,但我想隐藏错误消息,有人可以帮我解决这个问题吗?

4

3 回答 3

1

我不知道为什么它会在存储过程中工作。

你的第一个表达:

exec (@sqlquery2)

正在以下字符串上运行:

(a.[201301]+b.[201301]+c.[201301]+d.[201301]) as [201301],
(a.[201302]+b.[201302]+c.[201302]+d.[201302]) as [201302],
(a.[201303]+b.[201303]+c.[201303]+d.[201303]) as [201303],
(a.[201304]+b.[201304]+c.[201304]+d.[201304]) as [201304],
(a.[201305]+b.[201305]+c.[201305]+d.[201305]) as [201305],
(a.[201306]+b.[201306]+c.[201306]+d.[201306]) as [201306],
(a.[201307]+b.[201307]+c.[201307]+d.[201307]) as [201307],
(a.[201308]+b.[201308]+c.[201308]+d.[201308]) as [201308],
(a.[201309]+b.[201309]+c.[201309]+d.[201309]) as [201309],
(a.[201310]+b.[201310]+c.[201310]+d.[201310]) as [201310],
(a.[201311]+b.[201311]+c.[201311]+d.[201311]) as [201311],
(a.[201312]+b.[201312]+c.[201312]+d.[201312]) as [201312],

这些都不是有效的 SQL 语句。你可以看到,失败在于a没有被识别。

我最好的猜测是存储过程中的代码并不是您在问题中显示的代码。可能有些东西被遗漏了。

编辑:

你想要这样的代码:

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 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

set @Cont = @Cont + 1
end

set @sqlquery = @sqlquery + ' 'GES08' as COD_GES 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'
print @sqlquery;
exec(@sqlquery);

特别是,您需要了解执行字符串与仅将其打印出来之间的区别。

于 2013-07-23T18:04:57.273 回答
0

看起来“sqlquery2”缺少“select”

而不是设置@sqlquery = '',试试这个:

set @sqlquery = 'select' -- 然后构建查询并附加 sqlquery2,这可能有意义。

于 2013-07-23T18:04:53.997 回答
0

这部分是完全错误的:

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)

此语句中没有 SELECT 或 FROM,它永远不会执行。曾经。您需要构造一个实际的 SQL 语句,该语句将在此 Dynamic Query 工作之前正确执行。

于 2013-07-23T19:38:05.493 回答