zoukankan      html  css  js  c++  java
  • 存储过程实例

    Alter PROCEDURE [dbo].[calc_MRScoreLevel]
     (
      @ipd_no nvarchar(12),        --住院号
      @MRScore  Float output,     --病案得分
      @MRLever  NVarchar(10) output   --病案等级
     )
    AS
    BEGIN
         --声明变量 
                DECLARE @ItemType NVARCHAR(12) --缺陷类型
                declare @DefectScore nchar(10),@CheckCount int
                declare @haveYL nchar(10)
        --声明游标 赋值
          declare mycursor cursor for
          select s.ItemType,s.DefectScore,sr.CheckCount 
                 from MRD_Score s WITH(NOLOCK)
                 left join MRD_ScoreResult sr with(nolock) on s.PCode=sr.PCode and s.ItemCode=sr.ItemCode
                 where IPD_NO=@ipd_no
                 
                 open mycursor
        
                 --读取第一行
                 fetch next from mycursor into @ItemType,@DefectScore,@CheckCount
                 
                 while(@@fetch_status = 0)
                 begin
                   if @CheckCount IS NOT NULL AND len(@checkCount)>0    -- 存在已审核
                     begin
                     --如果缺陷类型是 单项否决(丙)
                      IF  @ItemType ='03'
                            BEGIN
                                SET @MRLever = '丙级'
                                return;
                            END 
                     --如果缺陷类型是 单项否决(乙),分数>75=乙,else 丙
                      ELSE if @ItemType='02'
                            BEGIN
                                
                                SET @haveYL ='Y'
                                Set @MRScore =@MRScore+ CONVERT(Float,@DefectScore) * @CheckCount;
                              
                            END
                      ELSE --常规 根据分数定等级
                          BEGIN
                           
                           SET @MRLever ='甲级'
                           set @MRScore = @MRScore + convert(Float,@DefectScore) * @CheckCount
                        
                          end
                     end
                     -- 下一行
                     fetch next from mycursor into @ItemType,@DefectScore,@CheckCount
                  END
                  --根据缺陷类型返回相应的值
                    if @haveYL='Y'
                        begin
                           if @MRScore >=75 set @MRLever='乙级'
                           else set @MRLever='丙级' 
                        end
                    else    
                        begin
                            if @MRScore >=90 set @MRLever='甲级'  
                            else if (@MRScore >=75 and @MRScore <90) set @MRLever='乙级'
                            else set @MRLever='丙级'
                        end
                    
                 close mycursor
                 deallocate mycursor 
    End

    存储过程执行结果,分别为output类型和Return Value类型:

     

    C#调用过程:

    public DataTable GetMRScoreAndLever(string ipd_no, float score, string lever)
           {
               SqlParameter[] para = { 
                                         new SqlParameter("@ipd_no",ipd_no),
                                         new SqlParameter("@MRScore", score),
                                         new SqlParameter("@MRLever", lever)
                                       };
    
               para[1].Direction = ParameterDirection.InputOutput; 
               para[2].Direction = ParameterDirection.InputOutput; 
               int count = 0;
                int resutlt = DbHelperSQL.RunProcedure("calc_MedicalRecordScore", para, out count);
                object o = para[1].Value;
               string MRScore =para[1].Value.ToString(); //分数 
               string MRLever = para[2].Value.ToString();//等级Convert.IsDBNull
    
               DataTable dttemp = new DataTable();
               DataRow newRow = dttemp.NewRow();
               dttemp.Columns.Add("MRScore");
               dttemp.Columns.Add("MRLever");
               newRow["MRScore"] = MRScore;
               newRow["MRLever"] = MRLever;
               dttemp.Rows.InsertAt(newRow, 0);
               return dttemp;
           }
    其中ParameterDirection.InputOutput:参数类型为output类型,ParameterDirection.ReturnValue:参数类型为OutputReturnValue类型。

      使用的是执行存储过程的方法ExecuteNonQuery(),而不是查询方法。

     1 public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
     2         {
     3             using (SqlConnection connection = new SqlConnection(connectionString))
     4             {
     5                 int result;
     6                 connection.Open();
     7                 SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
     8                 rowsAffected = command.ExecuteNonQuery();
     9                 result = (int)command.Parameters["ReturnValue"].Value;
    10                 //Connection.Close();
    11                 return result;
    12             }
    13         }

    最后附上 游标的用法:

    declare @id int
    declare @name varchar(50)
    declare cursor1 cursor for         --定义游标cursor1
    select * from table1               --使用游标的对象(跟据需要填入select文)
    open cursor1                       --打开游标
    fetch next from cursor1 into @id,@name  --(读取第一行)将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
    while @@fetch_status=0           --判断是否成功获取数据(有数据=0)
    begin
    update table1 set name=name+'1'
    where id=@id                           --进行相应处理(跟据需要填入SQL文)
    fetch next from cursor1 into @id,@name  --将游标向下移1行
    end
    close cursor1                   --关闭游标
    deallocate cursor1
  • 相关阅读:
    Navicat连接mysql出现10061/10060错误的解决
    对话守则
    一次Django admin bug解决的思维过程
    Think different
    SVN版本库的迁移
    Windows Phone 8 锁屏背景与通知
    Windows Phone 7 应用升级 Windows phone 8 方案预览 选择合适的 Key Feature
    从 windows phone7 到 windows phone 8 更新 如何设配两个版本
    windows phone 8 语音 Speech for Windows Phone 8
    windows phone 8 中的应用间通信
  • 原文地址:https://www.cnblogs.com/yexiaoyanzi/p/4243702.html
Copyright © 2011-2022 走看看