请高手帮忙这个在存储过程中怎么写?

来源:百度知道 编辑:UC知道 时间:2024/05/14 15:56:37
我有一个SQL语句字符串是'convert(integer,(select a from ragl))'
把这个字符串在存储过程中赋值给@a。然后想把这个字符串的结果值赋值给过程中定义的@b(一个INT型变量)。就是这个应该怎么写啊,请大侠赐教,谢谢。~~
1楼的大侠,你那样我不是就要在一个PRO里面再写一个PRO了?不好吧?还有就是对不起我没说清楚的地方,那个SQL字符串是变量的,所以要把它赋值到过程中的@a中啊~你这方法是把字符串当常量处理的吧?

create proc [dbo].[myproc1]
@a nvarchar(100),@b int output
as
declare @sql as nvarchar(200)
set @sql=N'set @c= '+@a+N';'
exec sp_executesql
@stmt=@sql,
@params=N'@c as int output',
@c=@b output;
然后你可以这样执行:
declare @b int
exec myproc1 N'convert(int,(select a from ragl))',@b output
select @b
就可以看到结果了,
但是'convert(integer,(select a from ragl))'如果返回字段a有多个值会发生错误,这就是你这个题目最初设计的问题了
首先呢,exec sp_executesql是动态sql语句,并不是储过程。
其次呢,在这里@a是存储过程中的输入变量,你用
exec myproc1 N'convert(int,(select a from ragl))',@b output 执行的话,
就是把变量N'convert(int,(select a from ragl))'赋值给@a,你也可以是
exec myproc1 11,@b output等

看来楼主是不懂存储过程的使用,人家回答的正确