Previously, we often concatenate SQL statements in the program. In fact, we can also splice SQL statements in the storage process for dynamic execution ~~
Previously, we often concatenate SQL statements in the program. In fact, we can also splice SQL statements in the storage process for dynamic execution ~~
Previously, we often concatenate SQL statements in the program. In fact, we can also splice SQL statements in the storage process for dynamic execution ~~
The code is as follows:
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 ";
# Splicing doctor id
SET cal1 = CONCAT (cal1, "", "AND OPEDOC_ID =", "'", DoctorId ,"'");
SET cal2 = CONCAT (cal2, "", "AND OPEDOC_ID =", "'", DoctorId ,"'");
# Splicing Department id
IF deptId <> ''Then
SET cal1 = CONCAT (cal1, "AND DEPT_ID =", "'", deptId ,"'");
End if;
# Splicing start and end date
IF beginDate <> ''AND endDate <> ''Then
SET cal1 = CONCAT (cal1, "", "AND OPER_AT BETWEEN", "'", beginDate, "'", "AND", "'", endDate, "'");
End if;
# Splicing standard departments
IF StandDeptId <> ''Then
SET cal1 = CONCAT (cal1, "", "AND DEPT_ID IN (select id from department WHERE STAND_DEPT_ID =", "'", StandDeptId ,"'",")");
End if;
# Splicing data sources
IF OperationFlag <> ''Then
SET cal1 = CONCAT (cal1, "", "and opepartkindid in (select id from zh_ssczfl where flag =", "'", OperationFlag ,"'",")");
End if;
# Splicing procedure
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;
The specific business logic involved is not described. For more information about the syntax, see.