关于Exec返回值的问题有很多,在这做个简要的总结。
   
   读查询语句示例:
     Declare @count int
1读取exec返回值    set @strSql=N'select @a= count(*) from ['+ @tblName + '] where  1=1 '+   @strWhere
2读取exec返回值    exec sp_executesql  @strSql ,N'@a int output',@Count output
3读取exec返回值    select @Count

    
   要点:
                        1.利用系统存储过程 sp_executesql
                        2. 在要执行的Sql文中加入参数,如 "@a",在sp_executesql的参数  声 明中要指定参数的类型,参数的方向。
                        3. sp_executesql的每个字符类型的参数都要是 n开头的数据类型,如是nvarchar 不能是      varchar,否则会报错“过程需要类型为 'ntext/nchar/nvarchar' 的参数”.

         读存储过程示例:

 

读取exec返回值create procedure ProTest
读取exec返回值(
读取exec返回值         
@name varchar(10),
读取exec返回值         
@money int output
读取exec返回值)
读取exec返回值
as
读取exec返回值
begin
读取exec返回值        
if(@name='1')
读取exec返回值                  
set @money=1000
读取exec返回值        
else
读取exec返回值                  
set @money=2000
读取exec返回值
end


这个只是一个简单的示例,这个存储过程返回的是@money 这个参数的值,那么当我们在另外一个存储过程中调用此存储过程的时候如何获取这个参数呢,方法如下:

读取exec返回值declare @m int ---用来接收返回值的变量
读取exec返回值
exec ProTest @name='1',@money=@m output --一定要注名是output


就这么简单,我们就获得了返回值,然后就可以利用它了

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-12-26
猜你喜欢
  • 2022-02-09
  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案