SQL脚本异常
--以下是想在指定时间内获取最小ID栏位值给@id_value变量,但总是报错,请帮忙
declare @id_value bigint
set @id_value=0
set @t_talbe='table1'
declare @sql nvarchar(500)
set @sql = 'select @id_value=min(id) FROM '@t_talbe+' WHERE Time1 > ''2013-7-1 0:00:00'' and Time1 < ''2013-12-1 0:00:00'') '
exec(@sql)
select @id_value
if @@rowcount=0
print 'no data'
else
print @id_value
提示:必须声明标量变量 "@id_value"。
------解决方案--------------------try this,
declare @id_value bigint
declare @sql nvarchar(4000),@t_talbe nvarchar(50)
set @id_value=0
set @t_talbe=N'table1'
set @sql=N'select @id_value=min(id) from '+@t_talbe+N' where Time1>''2013-7-1 0:00:00'' and Time1<''2013-12-1 0:00:00'' '
exec sp_executesql @sql,N'@id_value bigint output',@id_value=@id_value output
select @id_value