Question : Can a SP return 2 values?

Hi

I would like to ask can a SP (MSSQL2005) return 2 values?

e.g. the SP is to :
@code nvarchar(50) output;
@price decimal(12,2) output
begin
select @code=productcode, @price=price from product where id = 1
end

How can I get those 2 return values in C# as they have string and decimal data type?

Answer : Can a SP return 2 values?



//C# SP Call

        public static void GetOutputVals()
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "GetOutputVals";
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar,50).Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@price", SqlDbType.Decimal,14).Direction = ParameterDirection.Output;

            string a;
            decimal b;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                a = (string)cmd.Parameters["@code"].Value;
                b = (decimal)cmd.Parameters["@price"].Value;
            }
            catch (SqlException err)
            {
                throw new ApplicationException("Data error." + err);
            }
            finally
            {
                con.Close();
                con.Dispose();
               
            }
           
        }


//Store Proc
CREATE proc GetOutputVals

@code nvarchar(50) output,
@price decimal(12,2) output
as

begin
set @code='111'
set @price=3333
select @code, @price
end
Random Solutions  
 
programming4us programming4us