错误在SQL Server与表变量执行动态SQL查询2008

这些是以下参数用于执行动态SQL查询&表变量:错误在SQL Server与表变量执行动态SQL查询2008

宣言

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 '[email protected]+'-'[email protected]+''',SUM(C1.FEB) AS ''FEB '[email protected]+'-'[email protected]+''',SUM(C1.MAR) AS ''MAR '[email protected]+'-'[email protected]+''',

SUM(C1.APR) AS ''APR '[email protected]+'-'[email protected]+''',SUM(C1.MAY) AS ''MAY '[email protected]+'-'[email protected]+''',SUM(C1.JUN) AS ''JUN '[email protected]+'-'[email protected]+''',

SUM(C1.JUL) AS ''JUL '[email protected]+'-'[email protected]+''',SUM(C1.AUG) AS ''AUG '[email protected]+'-'[email protected]+''',SUM(C1.SEP) AS ''SEP '[email protected]+'-'[email protected]+''',

SUM(C1.OCT) AS ''OCT '[email protected]+'-'[email protected]+''',SUM(C1.NOV) AS ''NOV '[email protected]+'-'[email protected]+''',SUM(C1.DEC) AS ''DEC '[email protected]+'-'[email protected]+''',

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与表变量执行动态SQL查询2008 的全部内容, 来源链接: utcz.com/qa/260399.html

回到顶部