zoukankan      html  css  js  c++  java
  • SQLServer存储过程的返回值 查询分析器/程序

    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无关

  • 相关阅读:
    .net core上传
    C#/.NET整数的三种强制类型转换(int)、Convert.ToInt32()、int.Parse()的区别
    14、Silverlight 滤镜到 UWP 滤镜的移植(二)
    13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)
    1、揭秘通用平台的 HttpClient (译)
    12、uwp 开发的零碎总结
    11、使用 WinAppDeployCmd 部署appx 包到 Windows10 Mobile上(更新)
    10、Windows10 上,在窗口左侧向右滑动打开 SplitView 的 Pane面板
    09、win32 转换为 store app
    08、通过自定义依赖属性,用 StateTrigger 修改全局主题样式
  • 原文地址:https://www.cnblogs.com/andy_tigger/p/1863287.html
Copyright © 2011-2022 走看看