日期:2014-05-18  浏览次数:20579 次

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


------解决方案--------------------
比如存贮过程为 tmp2,用@i来接收返回值

declare @i int
exec @i=tmp2
------解决方案--------------------
使用存储过程返回值
例1
SQL code

create procedure test
as
select 1

declare @i int
exec @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 返回表