我正在使用SQL Server。我正在从另一个存储过程调用存储过程。
我想从第一个存储过程中返回多个值。
例如:我是Sub_SP从打来的Master_SP。Sub_SP会传回多个值给Master_SP。
Sub_SP
Master_SP
谁能举一个使用OUTPUT参数的例子吗?
谢谢你。
ALTER procedure ashwin @empid int,@empname varchar(20) output,@age int output as select @empname=ename,@age=age from emp where empid=@empid; declare @ename varchar(20),@age int execute ashwin 101,@ename out,@age out select @ename,@age;
// ------------
namespace sqlserver { class Program { static void Main(string[] args) { createconnection(); } public static void createconnection() { SqlConnection con=new SqlConnection("Data Source=ASHWIN\\SQLEXPRESS;Initial Catalog=employee;Integrated Security=True;Pooling=False"); con.Open(); SqlCommand cmd=new SqlCommand("ashwin",con); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@empid",SqlDbType.Int,10,"empid")); cmd.Parameters.Add(new SqlParameter("@empname", SqlDbType.VarChar, 20,ParameterDirection.Output,false,0,20,"ename",DataRowVersion.Default,null)); cmd.Parameters.Add(new SqlParameter("@age", SqlDbType.Int, 20, ParameterDirection.Output, false, 0, 10, "age", DataRowVersion.Default, null)); cmd.Parameters[0].Value = 101; cmd.UpdatedRowSource = UpdateRowSource.OutputParameters; cmd.ExecuteNonQuery(); string name = (string)cmd.Parameters["@empname"].Value; int age = Convert.ToInt32(cmd.Parameters["@age"].Value); Console.WriteLine("the name is {0}--and age is {1}", name,age); Console.ReadLine(); } }