gpt4 book ai didi

sql-server-2008 - 在 SQL Server 2008 中使用表变量执行动态 SQL 查询时出错

转载 作者:行者123 更新时间:2023-12-04 06:59:33 25 4
gpt4 key购买 nike

这些是用于执行动态sql查询的以下参数和表变量:

声明

DECLARE @sQuery VARCHAR(MAX)
DECLARE @FYear NVARCHAR(10)='2016-2017'
DECLARE @TYear NVARCHAR(10)='2016-2018'

DECLARE @CLAIMSUM TABLE
(
ClaimType NVARCHAR(MAX),
JAN DECIMAL(19,6),
FEB DECIMAL(19,6),
MAR DECIMAL(19,6),
APR DECIMAL(19,6),
MAY DECIMAL(19,6),
JUN DECIMAL(19,6),
JUL DECIMAL(19,6),
AUG DECIMAL(19,6),
SEP DECIMAL(19,6),
OCT DECIMAL(19,6),
NOV DECIMAL(19,6),
DEC DECIMAL(19,6),
TOTAL DECIMAL(19,6)
)

动态 SQL 查询

 SET @sQuery=N'SELECT C1.ClaimType,
SUM(C1.JAN) AS ''JAN '+@FYear+'-'+@TYear+''',SUM(C1.FEB) AS ''FEB '+@FYear+'-'+@TYear+''',SUM(C1.MAR) AS ''MAR '+@FYear+'-'+@TYear+''',
SUM(C1.APR) AS ''APR '+@FYear+'-'+@TYear+''',SUM(C1.MAY) AS ''MAY '+@FYear+'-'+@TYear+''',SUM(C1.JUN) AS ''JUN '+@FYear+'-'+@TYear+''',
SUM(C1.JUL) AS ''JUL '+@FYear+'-'+@TYear+''',SUM(C1.AUG) AS ''AUG '+@FYear+'-'+@TYear+''',SUM(C1.SEP) AS ''SEP '+@FYear+'-'+@TYear+''',
SUM(C1.OCT) AS ''OCT '+@FYear+'-'+@TYear+''',SUM(C1.NOV) AS ''NOV '+@FYear+'-'+@TYear+''',SUM(C1.DEC) AS ''DEC '+@FYear+'-'+@TYear+''',
SUM(C1.TOTAL) AS ''TOTAL''
FROM @CLAIMSUM C1 GROUP BY C1.ClaimType';

EXECUTE @sQuery

错误执行此查询时出现以下错误:

The name 'SELECT C1.ClaimType,
SUM(C1.JAN) AS 'JAN 2016-2017',SUM(C1.FEB) AS 'FEB 2016-2017',SUM(C1.MAR) AS 'MAR 2016-2017',
SUM(C1.APR) AS 'APR 2016-2017',SUM(C1.MAY) AS 'MAY 2016-2017',SUM(C1.JUN) AS 'JUN 2016-2017',
SUM(C1.JUL) AS 'JUL 2016-2017',SUM(C1.AUG) AS 'AUG 2016-2017',SUM(C1.SEP) AS 'SEP 2016-2017',
SUM(C1.OCT) AS 'OCT 2016-2017',SUM(C1.NOV) AS 'NOV 2016-2017',SUM(C1.DEC) AS 'DEC 2016-2017',
SUM(C1.TOTAL) AS 'TOTAL'
FROM @CLAIMSUM C1 GROUP BY C1.ClaimType' is not a valid identifier.

最佳答案

我已经将 EXECUTE @sQuery 替换为 EXECUTE (@sQuery),现在它工作正常。

关于sql-server-2008 - 在 SQL Server 2008 中使用表变量执行动态 SQL 查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com