这正确执行:(我需要使用''按日期来实际执行),这很奇怪。
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)
我收到以下错误:将字符串转换为smalldatetime数据类型时转换失败
将where语句更改为以下内容:
WHERE statdate BETWEEN ''+convert(smalldatetime,@from)+'' AND ''+convert(smalldatetime,@to)+'' ORDER BY statdate'
仍然给我同样的错误,似乎无法将日期替换为变量
答案 0 :(得分:1)
''并不奇怪;它是一种符号,可以在varchars中使用撇号。
连接时确保你没有尝试连接(n)varchars和(n)字符以外的任何内容,因为Sql Server会尝试将它们转换为其他数据类型;在你的情况下,在smalldatetime。您可以通过在连接之前/期间显式将参数日期转换为nvarchars来避免此问题,但更好的解决方案是使用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
答案 1 :(得分:0)
以下解决方案:
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”。
我不明白如何解决问题