日期:2014-05-17 浏览次数:20459 次
declare @Hour varchar(10)
declare @s varchar(1024)
declare @HourValue varchar(256)
set @i =0
while @i < 24
begin
set @Hour = 'Hour_' + cast(@i as char)
set @s = (' ( select top 1 '+ @Hour + ' from check_day where dev_id = ' + ''''+ @dev_id + '''' +')')
exec(@s) -- 可以执行成功 返回 Hour_0 至 Hour_23 的值
set @HourValue = exec(@s) -- 报错 在关键字 'exec' 附近有语法错误。
set @i = @i + 1
end
declare @Hour nvarchar(1000)
declare @s nvarchar(4000)
declare @HourValue nvarchar(1000)
DECLARE @i INT,@dev_id VARCHAR(50)
set @i =0
SET @Hour=''
while @i < 24
SELECT @Hour = @Hour+CASE WHEN @i=0 THEN '' ELSE '+' END+'Hour_' + cast(@i as VARCHAR(2)),@i = @i + 1
set @s = ' ( select top 1 @HourValue='+ @Hour + ' from check_day where dev_id = ' + ''''+ @dev_id + '''' +')'
EXECUTE sp_executesql @s,N'@HourValue nvarchar(1000) output',@HourValue output
PRINT @HourValue
while @i < 24
begin
select top 1 @HourValue = case when @i=0 then hour_0
when @i=1 then hour_1
when @i=2 then hour_2
...
when @i=23 then hour_23
end
from check_day
where dev_id = @dev_id
--