sql – 从存储过程返回多个值

前端之家收集整理的这篇文章主要介绍了sql – 从存储过程返回多个值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用sql Server.我从另一个存储过程调用存储过程.

我想从第一个存储过程返回多个值.

例如:我从Master_SP调用Sub_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,"ename",DataRowVersion.Default,null));
            cmd.Parameters.Add(new sqlParameter("@age","age",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();
        }
    }

猜你在找的MsSQL相关文章