1

我将下面的代码放在存储过程(SQL Server 2008)中。代码已放置在一个字符串中,以允许其中一个参数具有多个值。我遇到的问题是,当我在 SQL Server Management Studio 中测试 SQL 时,它说我的字符串不是有效的标识符,并指向我在周围放置双 ** 的行。

我看不出我做错了什么,我想知道我是否遗漏了什么或询问这种查询是否可以动态执行。如果后者是不可能的,那么如何将许多参数传递给这种查询?

谢谢

DECLARE @SQL AS VARCHAR(MAX)

SELECT @SQL = 'select Production_Site, [Target],[Action],[Fail]'
SELECT @SQL = @SQL + ' from'
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' select Production_Site, value, Period, YEAR, week'
SELECT @SQL = @SQL + ' from t_Pqe_Grocery'
SELECT @SQL = @SQL + ' unpivot ('
SELECT @SQL = @SQL + ' value'
SELECT @SQL = @SQL + ' for col in (Grocery_Packaging_And_Coding, Grocery_Measurable,'
SELECT @SQL = @SQL + ' Grocery_Appearance, Grocery_Aroma,'
**SELECT @SQL = @SQL + ' Grocery_Flavour, Grocery_Texture)) unp'**
SELECT @SQL = @SQL + ' ) src '
SELECT @SQL = @SQL + ' pivot '
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' count(value)'
SELECT @SQL = @SQL + ' for value in ([Target], [Action], [Fail])'
SELECT @SQL = @SQL + ' ) piv'
SELECT @SQL = @SQL + ' where Production_Site IN (' + @Site + ') AND YEAR = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Year)) + ' AND Period = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Period)) + ' and Week = ' +   
                     CONVERT(varchar(50), CONVERT(BIGINT, @Week)) 
EXECUTE @SQL
4

2 回答 2

0

更改EXECUTE @SQLEXECUTE (@SQL)。我相信这是语法问题。

尝试将其粘贴到 SSMS 中并运行它,它运行时没有引号问题:

DECLARE @Site VARCHAR(1000) = '''Avonmouth'',''Lifton'''
DECLARE @Year VARCHAR(50) = '12'
DECLARE @Period VARCHAR(50) = '10'
DECLARE @Week VARCHAR(50) ='41'

DECLARE @SQL AS VARCHAR(MAX)

SELECT @SQL = 'select Production_Site, [Target],[Action],[Fail]'
SELECT @SQL = @SQL + ' from'
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' select Production_Site, value, Period, YEAR, week'
SELECT @SQL = @SQL + ' from t_Pqe_Grocery'
SELECT @SQL = @SQL + ' unpivot ('
SELECT @SQL = @SQL + ' value'
SELECT @SQL = @SQL + ' for col in (Grocery_Packaging_And_Coding, Grocery_Measurable,'
SELECT @SQL = @SQL + ' Grocery_Appearance, Grocery_Aroma,'
SELECT @SQL = @SQL + ' Grocery_Flavour, Grocery_Texture)) unp'
SELECT @SQL = @SQL + ' ) src '
SELECT @SQL = @SQL + ' pivot '
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' count(value)'
SELECT @SQL = @SQL + ' for value in ([Target], [Action], [Fail])'
SELECT @SQL = @SQL + ' ) piv'
SELECT @SQL = @SQL + ' where Production_Site IN (' + @Site + ') AND YEAR = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Year)) + ' AND Period = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Period)) + ' and Week = ' +   
                     CONVERT(varchar(50), CONVERT(BIGINT, @Week)) 
PRINT @SQL
EXECUTE (@SQL)
于 2012-11-20T13:40:38.577 回答
0

我猜你的@site参数周围没有足够的单引号,而不是传入的。添加以下内容:

Production_Site IN (''' + @Site + ''') 

如果@site是一个字符串值,那么你需要在它周围加上单引号。您当前的查询缺少它们。

所以你的最终查询是:

DECLARE @SQL AS VARCHAR(MAX)

SELECT @SQL = 'select Production_Site, [Target],[Action],[Fail]'
SELECT @SQL = @SQL + ' from'
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' select Production_Site, value, Period, YEAR, week'
SELECT @SQL = @SQL + ' from t_Pqe_Grocery'
SELECT @SQL = @SQL + ' unpivot ('
SELECT @SQL = @SQL + ' value'
SELECT @SQL = @SQL + ' for col in (Grocery_Packaging_And_Coding, Grocery_Measurable,'
SELECT @SQL = @SQL + ' Grocery_Appearance, Grocery_Aroma,'
SELECT @SQL = @SQL + ' Grocery_Flavour, Grocery_Texture)) unp'
SELECT @SQL = @SQL + ' ) src '
SELECT @SQL = @SQL + ' pivot '
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' count(value)'
SELECT @SQL = @SQL + ' for value in ([Target], [Action], [Fail])'
SELECT @SQL = @SQL + ' ) piv'
SELECT @SQL = @SQL + ' where Production_Site IN (''' + @Site + ''') AND YEAR = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Year)) + ' AND Period = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Period)) + ' and Week = ' +   
                     CONVERT(varchar(50), CONVERT(BIGINT, @Week)) 

EXECUTE (@SQL)
于 2012-11-20T15:02:40.543 回答