当前位置: 代码迷 >> Sql Server >> 在存储过程中,怎么返回值
  详细解决方案

在存储过程中,怎么返回值

热度:133   发布时间:2016-04-27 19:32:22.0
在存储过程中,如何返回值
....
EXEC ('DELETE train WHERE train_id IN (' + @selecteds + ')')
如果我加上return 1则提示:在此上下文中不能使用带有返回值的 RETURN 语句
我主要是想这样:删除成功时,则返回为1,否则为-1


------解决方案--------------------
比如存贮过程为 tmp2,[email protected]

declare @i int
exec @i=tmp2
------解决方案--------------------
使用存储过程返回值
例1
SQL code
create procedure testasselect 1declare @i intexec @i = test
------解决方案--------------------
方法有很多种的

1、可以利用 output 参数
 SqlParameter 的 Dicection 属性要设置为输出类型
Dim parameter As New SqlParameter("Description", SqlDbType.VarChar, 88)
parameter.Direction = ParameterDirection.Output

  
2、可以用 return 返回值
SqlParameter 的 Dicection 属性要设置为返回值类型
parameter.Direction = ParameterDirection.ReturnValue

3、如果返回表,可以直接 运行select 语句
然后执行 SqlCommand.ExecuteReader 返回表

  相关解决方案