zoukankan      html  css  js  c++  java
  • mysql 存储过程动态执行sql语句

     之前经常在程序中拼接sql语句,其实我们也可以在存储过程中拼接sql 语句,动态的执行~~

     代码如下:
     

    DROP PROCEDURE IF EXISTS SearchByDoctor;
    CREATE PROCEDURE SearchByDoctor(
    IN DoctorId VARCHAR(50),
    IN deptId VARCHAR(50),
    IN beginDate VARCHAR(20),
    IN endDate VARCHAR(20),
    IN StandDeptId VARCHAR(50),
    IN OperationFlag VARCHAR(50),
    IN SsczflId VARCHAR(50),
    OUT OperNum INT,
    OUT AvgDangerIndex DOUBLE,
    OUT OperGrCase INT
    )
    BEGIN
    DECLARE cal1 VARCHAR(800);
    DECLARE cal2 VARCHAR(800);


    SET cal1="SELECT COUNT(1),AVG(DANGER_INDEX) INTO @para1,@para2 FROM yw_ssxxb WHERE 1=1 ";
    SET cal2="SELECT COUNT(1) INTO @para3 FROM gr_grbw WHERE OPE_RELID IN (SELECT RELID FROM yw_ssxxb WHERE 1=1";

    #拼接医生id
    SET cal1=CONCAT(cal1," ","AND OPEDOC_ID = ","'",DoctorId,"'");
    SET cal2=CONCAT(cal2," ","AND OPEDOC_ID = ","'",DoctorId,"'");

    #拼接科室id
    IF deptId <>'' THEN
    SET cal1=CONCAT(cal1,"AND DEPT_ID =","'",deptId,"'");
    END IF;

    #拼接开始结束日期
    IF beginDate<>'' AND endDate <>'' THEN
    SET cal1=CONCAT(cal1," ","AND OPER_AT BETWEEN ","'",beginDate,"'"," AND ","'",endDate,"'");
    END IF;

    #拼接标准科室
    IF StandDeptId<>'' THEN
    SET cal1=CONCAT(cal1," ","AND DEPT_ID IN ( SELECT ID FROM department WHERE STAND_DEPT_ID = ","'",StandDeptId,"'",")");
    END IF;

    #拼接数据来源
    IF OperationFlag <>'' THEN
    SET cal1=CONCAT(cal1," ","AND OPEPARTKINDID IN (SELECT ID FROM zh_ssczfl WHERE FLAG= ","'",OperationFlag,"'",")");
    END IF;

    #拼接手术操作类别
    IF SsczflId <> '' THEN
    SET cal1=CONCAT(cal1," ","AND OPEPARTKINDID = ","'",SsczflId,"'");
    END IF;

    SET cal2 =CONCAT(cal2,")");

    SET @sql1=cal1;
    SET @sql2=cal2;

    PREPARE stmt1 FROM @sql1;
    EXECUTE stmt1;
    DEALLOCATE PREPARE stmt1 ;

    PREPARE stmt2 FROM @sql2;
    EXECUTE stmt2;
    DEALLOCATE PREPARE stmt2 ;

    SET OperNum=@para1;
    SET AvgDangerIndex=@para2;
    SET OperGrCase=@para3;

    END;

     涉及到的具体的业务逻辑就不介绍了,语法方面还是可以参照下的。

  • 相关阅读:
    (转载)微软数据挖掘算法应用场景介绍
    (转载)微软数据挖掘算法:Microsoft 目录篇
    (转载)微软数据挖掘算法:Microsoft 线性回归分析算法(11)
    (转载)微软数据挖掘算法:Microsoft 神经网络分析算法(10)
    js分页
    预下载图片,避免图片闪烁
    http协议
    解决&#65279产生的空白行
    兼容ie浏览器的方法
    网站加载速度慢的原因
  • 原文地址:https://www.cnblogs.com/drcoding/p/4270241.html
Copyright © 2011-2022 走看看