declare @sql nvarchar(1000) set @sql='select getdate()' declare @tt nvarchar(50) exec sp_executesql @sql,@tt output select @tt
查询中,怎样获取到字符脚本的执行结果,如:@sql
既然参数@tt可以返回,为什么不再定义个参数用于@sql输出:@OutSQL output
print(@sql)
DECLARE @temp TABLE ( Id INT, Title nvarchar(50), Value nvarchar(1000), Remark nvarchar(1000), Pass nvarchar(50) ) --获取自增最大值 declare @flag int select @flag=Isnull(MAX(Id),0) from AkWorkOrderPrintDate --变量 while @flag>0 begin --是否存在这条记录 declare @count int select @count=COUNT(*) from AkWorkOrderPrintDate where Id=@flag if @count=1 begin --拷贝这行的数据 insert into @temp select Id,Title,Value,Remark,'值错误' from AkWorkOrderPrintDate where Id=@flag --测试日期设置 declare @val nvarchar(1000) select @val=Value from @temp where Id=@flag set @val='select @a='+@val --执行 begin try declare @pass nvarchar(1000) exec sp_executesql @val,N'@a int output',@pass output update @temp set Pass=@pass where Id=@flag end try begin catch end catch end set @flag=@flag-1 end select * from @temp