2023年6月21日发(作者:)
SQLServer存储过程返回值总结1. 存储过程没有返回值的情况(即存储过程语句中没有return之类的语句) ⽤⽅法 int count = ExecuteNonQuery(..)执⾏存储过程其返回值只有两种情况 (1)假如通过查询分析器执⾏该存储过程,在显⽰栏中假如有影响的⾏数,则影响⼏⾏count就是⼏ (2)假如通过查询分析器执⾏该存储过程,在显⽰栏中假如显⽰ '命令已成功完成。' 则count = -1;在显⽰栏中假如有查询结果,则count = -1总结:eNonQuery()该⽅法只返回影响的⾏数,假如没有影响⾏数,则该⽅法的返回值只能是-1,不会为0。 B.不论ExecuteNonQuery()⽅法是按照Procedure或者执⾏,其效果和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" ,)};cmdParms[ - 1].Direction = Value;或者cmdParms[ - 1].Direction = 或者cmdParms[ - 1].Direction = ;
得到返回值 object bj = cmdParms[ - 1].Value;相关资源:
2023年6月21日发(作者:)
SQLServer存储过程返回值总结1. 存储过程没有返回值的情况(即存储过程语句中没有return之类的语句) ⽤⽅法 int count = ExecuteNonQuery(..)执⾏存储过程其返回值只有两种情况 (1)假如通过查询分析器执⾏该存储过程,在显⽰栏中假如有影响的⾏数,则影响⼏⾏count就是⼏ (2)假如通过查询分析器执⾏该存储过程,在显⽰栏中假如显⽰ '命令已成功完成。' 则count = -1;在显⽰栏中假如有查询结果,则count = -1总结:eNonQuery()该⽅法只返回影响的⾏数,假如没有影响⾏数,则该⽅法的返回值只能是-1,不会为0。 B.不论ExecuteNonQuery()⽅法是按照Procedure或者执⾏,其效果和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" ,)};cmdParms[ - 1].Direction = Value;或者cmdParms[ - 1].Direction = 或者cmdParms[ - 1].Direction = ;
得到返回值 object bj = cmdParms[ - 1].Value;相关资源:
发布评论