能在详细点吗,没看明白,拼接过程中可以用mysql以外的函数,拼接成后 只能用mysql函数了
㈡ mysql存储过程能不能直接执行拼接的sql语句
当然可以,就是在mysql存储过程中使用动态sql,就可以拼接sql,然后执行了。
给你复制一段,如果不满意,自己搜索 mysql存储过程动态sql就可以了
;
CREATEPROCEDURESearchByDoctor(
INDoctorIdVARCHAR(50),
INdeptIdVARCHAR(50),
INbeginDateVARCHAR(20),
INendDateVARCHAR(20),
INStandDeptIdVARCHAR(50),
INOperationFlagVARCHAR(50),
INSsczflIdVARCHAR(50),
OUTOperNumINT,
OUTAvgDangerIndexDOUBLE,
OUTOperGrCaseINT
)
BEGIN
DECLAREcal1VARCHAR(800);
DECLAREcal2VARCHAR(800);
SETcal1="SELECTCOUNT(1),AVG(DANGER_INDEX)INTO@para1,@para2FROMyw_ssxxbWHERE1=1";
SETcal2="SELECTCOUNT(1)INTO@para3FROMgr_grbwWHEREOPE_RELIDIN(SELECTRELIDFROMyw_ssxxbWHERE1=1";
#拼接医生id
SETcal1=CONCAT(cal1,"","ANDOPEDOC_ID=","'",DoctorId,"'");
SETcal2=CONCAT(cal2,"","ANDOPEDOC_ID=","'",DoctorId,"'");
#拼接科室id
IFdeptId<>''THEN
SETcal1=CONCAT(cal1,"ANDDEPT_ID=","'",deptId,"'");
ENDIF;
#拼接开始结束日期
IFbeginDate<>''ANDendDate<>''THEN
SETcal1=CONCAT(cal1,"","ANDOPER_ATBETWEEN","'",beginDate,"'","AND","'",endDate,"'");
ENDIF;
#拼接标准科室
IFStandDeptId<>''THEN
SETcal1=CONCAT(cal1,"","ANDDEPT_IDIN(_DEPT_ID=","'",StandDeptId,"'",")");
ENDIF;
#拼接数据来源
IFOperationFlag<>''THEN
SETcal1=CONCAT(cal1,"","ANDOPEPARTKINDIDIN(SELECTIDFROMzh_ssczflWHEREFLAG=","'",OperationFlag,"'",")");
ENDIF;
#拼接手术操作类别
IFSsczflId<>''THEN
SETcal1=CONCAT(cal1,"","ANDOPEPARTKINDID=","'",SsczflId,"'");
ENDIF;
SETcal2=CONCAT(cal2,")");
SET@sql1=cal1;
SET@sql2=cal2;
PREPAREstmt1FROM@sql1;
EXECUTEstmt1;
DEALLOCATEPREPAREstmt1;
PREPAREstmt2FROM@sql2;
EXECUTEstmt2;
DEALLOCATEPREPAREstmt2;
SETOperNum=@para1;
SETAvgDangerIndex=@para2;
SETOperGrCase=@para3;
END;
㈢ mysql存储过程一个参数有多个逗号分割
这个应该可以使用字符串拼接,你是不是这样写存储过程: create procere p_proc(var_in int) begin delete from [tableName] where [colName] in (var_in); end 然后又这样调用: call p_proc(1, 2, 3); 这样肯定不行,那么你可以这样:
㈣ mysql使用存储过程将多条(4条以上)SQL使用 union all 合并
BEGIN
declaresel_sqlVARCHAR(100);
declareg_sqlVARCHAR(100);
declareu_sqlVARCHAR(100);
declaret_nameVARCHAR(100);
DECLAREdoneINTDEFAULTFALSE;
declaremonth__monthwhereflag=1;
=true;
setsel_sql='(selectsum(fee+fee2+fee3)Asfee,monthfromfee_detail_';/*左括号*/
setg_sql='groupbymonth)';/*右括号*/
setu_sql='unionall';/*加空格、上同*/
OPENmonth_cursor;
my_loop:loop
FETCHmonth_cursorintot_name;
ifdonethen
leavemy_loop;
endif;
ifs_sqlisnullthen
set@s_sql=CONCAT(sel_sql,t_name,g_sql);/*第一个SQL*/
else
set@s_sql=CONCAT(s_sql,u_sql,sel_sql,t_name,g_sql);/*开始Union后续SQL*/
endif;
endloop;
/*最后执行一次拼接后的SQL语句*/
PREPAREpre_s_sqlfrom@s_sql;
EXECUTEpre_s_sql;
deallocatepreparepre_s_sql;
closemonth_cursor;
END
思路修正,貌似还有不少语法问题,自行修复。
㈤ mysql存储过程求大神(拼接字符串)
DROPPROCEDUREIFEXISTS`p_create_view`
CREATEPROCEDUREp_create_view()
BEGIN
DECLAREtableNameVARCHAR(100);
DECLAREstopFlagINT;
DECLAREsql1VARCHAR(1000);
DECLAREcursor_nameCURSORFORSELECTTABLE_NAMEFROMinformation_schema.tablesWHERETABLE_NAMELIKE'test_2014%';
'02000'SETstopFlag=1;
SET@sql1='createviewtestasselectid,namefrom';
OPENcursor_name;
REPEAT
FETCHcursor_nameINTOtableName;
SELECTCONCAT(@sql1,tableName,'UNIONALLSELECTid,nameFROM')INTO@sql1FROMDUAL;
UNTILstopFlagENDREPEAT;
CLOSEcursor_name;
SELECTSUBSTR(@sql1,1,LENGTH(@sql1)-31)INTO@sql1FROMDUAL;
PREPAREstepFROM@sql1;
EXECUTEstep;
END;
㈥ mysql存储过程中调用Prepare返回值
事实上MySQL还不支持从动态sql中返回结果,既不能用PrepareStatement来声明游标,游标只能用嵌入式SQL来声明。
所以你要实现这个功能就要考虑其它的办法,给你一个建议就是创建一个临时表(Create temporary TableName),将你查询出的结果存放的这个临时表里,然后你可以对这张临时表进行操作。但是也很复杂的!!!~
㈦ mysql存储过程能不能直接执行拼接的sql语句
prepare statement_name from 'sql_text'
execute statement_name using [ variable1,variable2]
deallocate prepare statement_name
㈧ laravel 怎么调用mysql存储过程
laravel并没有提供直接调用存储过程的实现,只能通过拼sql的方式调用存储过程,比如:
DB::statement('call存储过程名称(参数)');
或者
DB::select('call存储过程名称(参数)');
㈨ mysql 存储过程 数据库表名字段作为参数传递给存储过程的方法
直接将数据库表名字段作为字符类型传到存储过程中,存储过程拼接sql语句,再用exec执行。例如:
CREATE PROCEDURE [proc_test]
(@tablename varchar(100))
WITH
EXECUTE AS CALLER
AS
declare @sql varchar(1000);
set @sql='select * from '+@tablename
exec(@sql)
GO
再直接执行 exec proc_test '学生表'