CREATE PROCEDURE [dbo].[TestProc]
@name varchar(50)
AS
select 'Hello, World! ' + @name
return 1234
GO
@name varchar(50)
AS
select 'Hello, World! ' + @name
return 1234
GO
ADO.net
using (SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=test"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("TestProc", conn);
cmd.Parameters.Add(new SqlParameter("@name", "Q.yuhen"));
cmd.Parameters.Add(new SqlParameter("@return", SqlDbType.Int, 0, ParameterDirection.ReturnValue,
true, 0, 0, string.Empty, DataRowVersion.Default, DBNull.Value));
cmd.CommandType = CommandType.StoredProcedure;
object o = cmd.ExecuteScalar();
int returnValue = (int)cmd.Parameters["@return"].Value;
Console.WriteLine("{0}; Return:{1}", o, returnValue);
}
{
conn.Open();
SqlCommand cmd = new SqlCommand("TestProc", conn);
cmd.Parameters.Add(new SqlParameter("@name", "Q.yuhen"));
cmd.Parameters.Add(new SqlParameter("@return", SqlDbType.Int, 0, ParameterDirection.ReturnValue,
true, 0, 0, string.Empty, DataRowVersion.Default, DBNull.Value));
cmd.CommandType = CommandType.StoredProcedure;
object o = cmd.ExecuteScalar();
int returnValue = (int)cmd.Parameters["@return"].Value;
Console.WriteLine("{0}; Return:{1}", o, returnValue);
}
RETURN只能返回INT型,OUTPUT可以返回自定义类型
执行到RETURN的时候存储过程即结束。
而OUTPUT的变量可以重复设置