这正确执行:(奇怪的是我需要在实际执行的日期之前使用'')
DECLARE
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname) FROM [85137_PHY_Long_PG] c FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = 'SELECT statdate, ' + @cols + ' from
(
select statdate, statcolumnname, statcolumnvalue
from [85137_PHY_Long_PG]
) x
pivot
(
min(statcolumnvalue)
for statcolumnname in (' + @cols + ')
) p WHERE statdate BETWEEN ''2012-04-01 12:15:00'' AND ''2012-04-01 12:45:00'' ORDER BY statdate'
execute(@query)
现在我想用变量替换日期:
DECLARE
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@from AS NVARCHAR(MAX),
@to AS NVARCHAR(MAX);
set @from = '2012-04-01 12:15:00'
set @to = '2012-04-01 12:45:00'
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname) FROM [85137_PHY_Long_PG] c FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = 'SELECT statdate, ' + @cols + ' from
(
select statdate, statcolumnname, statcolumnvalue
from [85137_PHY_Long_PG]
) x
pivot
(
min(statcolumnvalue)
for statcolumnname in (' + @cols + ')
) p WHERE statdate BETWEEN ''+@from+'' AND ''+@to+'' ORDER BY statdate'
execute(@query)
我收到以下错误:将字符串转换为小日期时间数据类型时转换失败
将 where 语句更改为以下内容:
WHERE statdate BETWEEN ''+convert(smalldatetime,@from)+'' AND ''+convert(smalldatetime,@to)+'' ORDER BY statdate'
仍然给我同样的错误,只是似乎无法将日期替换为变量
并不奇怪;它是一种在varchars中启用撇号的符号。
连接时,请确保不要尝试连接 (n) varchar 和 (n) char 以外的任何内容,因为 Sql Server 将尝试将它们转换为其他数据类型;在您的情况下,在 smalldatetime 中。您可以通过在串联之前/期间将参数日期显式转换为 nvarchar 来避免此问题,但更好的解决方案是使用 sp_executesql 和参数。
如果将参数保留在查询中:
set @query = 'SELECT statdate, ' + @cols + ' from
(
select statdate, statcolumnname, statcolumnvalue
from [85137_PHY_Long_PG]
) x
pivot
(
min(statcolumnvalue)
for statcolumnname in (' + @cols + ')
) p WHERE statdate BETWEEN @from AND @to ORDER BY statdate'
您可以使用参数执行它:
exec sp_executesql @query, N'@from datetime, @to datetime', @from=@from_variable, @to=@to_variable
其中@from_variable和@to_variable是批处理中较早定义的日期时间变量。
更新:
如果您的最终目标是将此代码包装在存储过程中,下面是一个模板:
create proc MyProc (@dateFrom smalldatetime, @dateTo smalldatetime)
as
DECLARE
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname)
FROM [85137_PHY_Long_PG] c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = 'SELECT statdate, ' + @cols + ' from
(
select statdate, statcolumnname, statcolumnvalue
from [85137_PHY_Long_PG]
) x
pivot
(
min(statcolumnvalue)
for statcolumnname in (' + @cols + ')
) p WHERE statdate BETWEEN @from AND @to ORDER BY statdate'
exec sp_executesql @query, N'@from smalldatetime, @to smalldatetime', @from=@dateFrom, @to=@dateTo
以下是解决方案:
DECLARE
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@internal_fromdate AS SMALLDATETIME,
@internal_todate AS SMALLDATETIME;
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname) FROM [85137_PHY_Long_PG] c FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @internal_fromdate = '2012-04-01 12:15:00';
set @internal_todate = '2012-04-01 12:45:00';
set @query = 'SELECT statdate, ' + @cols + ' from
(
select statdate, statcolumnname, statcolumnvalue
from [85137_PHY_Long_PG]
) x
pivot
(
min(statcolumnvalue)
for statcolumnname in (' + @cols + ')
) p WHERE statdate BETWEEN @FromDate AND @ToDate ORDER BY statdate'
exec sp_executesql @query, N'@FromDate SMALLDATETIME, @ToDate SMALLDATETIME', @FromDate=@internal_fromdate, @ToDate=@internal_todate
更新
好的,我已经尝试了以下变体:
create proc MyProc9 (@tableName varchar,@dateFrom smalldatetime, @dateTo smalldatetime)
AS
DECLARE
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname)
FROM [85137_PHY_Long_MP] c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = 'SELECT statdate, ' + @cols + ' from
(
SELECT statdate, statcolumnname, statcolumnvalue
from @table
) x
pivot
(
min(statcolumnvalue)
for statcolumnname in (' + @cols + ')
) p WHERE statdate BETWEEN @from AND @to ORDER BY statdate'
exec sp_executesql @query, N'@table varchar,@from smalldatetime, @to smalldatetime', @table=@tableName,@from=@dateFrom, @to=@dateTo
错误:必须声明表变量"@table"。
在@query字符串中尝试了"+@tableName+":"8"附近的语法不正确。
在@query字符串中尝试了"+QUOTENAME(@tableName)+":对象名称"8"无效。
在@query字符串中尝试 ['+@tableName+']:对象名称"8"无效。
在@query字符串中尝试了 QUOTENAME(@table):对象名称"8"无效。
在@query字符串中尝试[85137_PHY_Long_MP]:工作正常,只是想替换它。
在@query字符串中尝试 [@tableName]:对象名称"@tableName"无效。
在@query字符串中尝试@tableName:必须声明表变量"@tableName"。
我不明白如何解决问题