存储过程中调用另一存储过程中的返回值

代码

--存储过程1 返回一个值
create procedure SP1
  
@returnValue int output
as
  
set @returnValue = 1

--存储过程2 调用存储过程1中的值

create procedure SP2
as
  
Declare @output int
  
execute SP1 @output output
  
  
if @output = 1
    
select 'output = 1'
  
else
    
select 'output <> 1'

附:ADO.NET调用sp返回的值

代码
1.获取Return返回值
//存储过程
//Create PROCEDURE MYSQL
//     @a int,
//     @b int
//AS
//     return @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand 
= new SqlCommand("MYSQL", conn);
MyCommand.CommandType 
= CommandType.StoredProcedure;
MyCommand.Parameters.Add(
new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters[
"@a"].Value = 10;
MyCommand.Parameters.Add(
new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters[
"@b"].Value = 20;
MyCommand.Parameters.Add(
new SqlParameter("@return", SqlDbType.Int));
MyCommand.Parameters[
"@return"].Direction = ParameterDirection.ReturnValue;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters[
"@return"].Value.ToString());

2.获取Output输出参数值
//存储过程
//Create PROCEDURE MYSQL
//     @a int,
//     @b int,
//     @c int output
//AS
//     Set @c = @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand 
= new SqlCommand("MYSQL", conn);
MyCommand.CommandType 
= CommandType.StoredProcedure;
MyCommand.Parameters.Add(
new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters[
"@a"].Value = 20;
MyCommand.Parameters.Add(
new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters[
"@b"].Value = 20;
MyCommand.Parameters.Add(
new SqlParameter("@c", SqlDbType.Int));
MyCommand.Parameters[
"@c"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters[
"@c"].Value.ToString());


本文来自CSDN博客,转载请标明出处:http:
//blog.csdn.net/mngzilin/archive/2010/01/09/5162859.aspx
原文地址:https://www.cnblogs.com/icebutterfly/p/1654251.html