SQL Stored Procedure return value

Source: Internet
Author: User

1.  存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)

  用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况   (1)假如通过查询分析器执行该存储过程,在显示栏中假如有影响的行数,则影响几行count就是几   (2)假如通过查询分析器执行该存储过程,在显示栏中假如显示 ‘命令已成功完成。‘ 则count = -1;在显示栏中假如有查询结果,则count = -1 总结:A.ExecuteNonQuery()该方法只返回影响的行数,假如没有影响行数,则该方法的返回值只能是-1,不会为0。        B.不论ExecuteNonQuery()方法是按照CommandType.StoredProcedure或者CommandType.Text执行,其效果和A一样。 --------------------------------------------------------------------------------------------------------------------------------------------------- 2.  获得存储过程的返回值--通过查询分析器获得     (1)不带任何参数的存储过程(存储过程语句中含有 return )   ---创建存储过程   CREATE PROCEDURE  testReturn   AS   return 145   GO   ---执行存储过程   DECLARE @RC int   exec @RC=testReturn   select @RC   ---说明   查询结果为145     (2)带输入参数的存储过程(存储过程语句中含有 return )   ---创建存储过程   create procedure sp_add_table1    @in_name varchar(100),    @in_addr varchar(100),    @in_tel varchar(100)   as    if (@in_name = ‘‘ or @in_name is null )     return 1    else     begin      insert into table1(name,addr,tel) values(@in_name,@in_addr,@in_tel)      return 0     end   ---执行存储过程   <1>执行下列,返回1   declare @count int exec @count = sp_add_table1 ‘‘ , ‘中三路‘ , ‘123456‘ select @count   <2>执行下列,返回0   declare @count int exec @count = sp_add_table1 ‘‘ , ‘中三路‘ , ‘123456‘ select @count   ---说明   查询结果不是0就是1     (3)带输出参数的存储过程(存储过程中可以有 return 可以没有 return )   例子A:   ---创建存储过程   create procedure sp_output    @output int output   as    set @output = 121    return 1   ---执行存储过程   <1>执行下列,返回121   declare @ out int   exec sp_output @ out output   select @ out   <2>执行下列,返回1   declare @ out int   declare @count int   exec @count = sp_output @ out output   select @count   ---说明   return ,只要查询输出参数,则查询结果为输出参数在存储过程中最后变成的值;只要不查询输出参数,则查询结果为 return 返回的值   例子B:   ---创建存储过程   create procedure sp_output    @output int output   as    set @output = 121   ---执行存储过程   <1>执行下列,返回121   declare @ out int   exec sp_output @ out output   select @ out   <2>执行下列,返回0   declare @ out int   declare @count int   exec @count = sp_output @ out output   select @count   ---说明   没有 return ,只要查询输出参数,则查询结果为输出参数在存储过程中最后变成的值;只要不查询输出参数,则查询结果为0   总结:   (1)存储过程共分为3类:    A.返回记录集的存储过程---------------------------其执行结果是一个记录集,例如:从数据库中检索出符合某一个或几个条件的记录    B.返回数值的存储过程(也可以称为标量存储过程)-----其执行完以后返回一个值,例如:在数据库中执行一个有返回值的函数或命令    C.行为存储过程-----------------------------------用来实现数据库的某个功能,而没有返回值,例如:在数据库中的更新和删除操作   (2)含有 return 的存储过程其返回值为 return 返回的那个值   (3)没有 return 的存储过程,不论执行结果有无记录集,其返回值是0   (4)带输出参数的存储过程:假如有 return 则返回 return 返回的那个值,假如要 select 输出参数,则出现输出参数的值,于有无 return 无关 --------------------------------------------------------------------------------------------------------------------------------------------------- 3.获得存储过程的返回值--通过程序获得 --------------------------------------------------------------------------------------------------------------------------------------------------- SqlParameter[] cmdParms = { .. , new SqlParameter( "@return" ,SqlDbType.Int)}; cmdParms[cmdParms.Length - 1].Direction = ParameterDirection.ReturnValue;或者 cmdParms[cmdParms.Length - 1].Direction = ParameterDirection.Output或者 cmdParms[cmdParms.Length - 1].Direction = ParameterDirection.Input; 得到返回值 object bj = cmdParms[cmdParms.Length - 1].Value;This article reproduces the summary of SQL Server stored procedure return value from Zhang Hanlu zhe

SQL Stored Procedure return value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.