当前位置:网站首页>mysql 存储过程 动态参数 查询执行结果
mysql 存储过程 动态参数 查询执行结果
2022-08-03 05:10:00 【lhorse003】
mysql function & procedure 学习使用小结:
1、动态sql, 即动态参数:
在存储过程中,想要直接用表名变量做参数,动态执行sql,不能直接写
1 2 3 4 5 6 7 | create procedure ( $tableName char(20) )BEGINselect * from tableName;END |
mysql 不支持表名作为变量,这样会直接将变量名“tableName”作为表名去查询,解决的方法是:
1 2 3 4 5 6 7 8 9 10 11 | BEGIN SET @sql = concat('select * from ', $tableName); PREPARE stmt1 FROM @sql; EXECUTE stmt1; DEALLOCATE PREPARE stmt1;END; |
2 得到动态sql, select查询的结果:
可以直接 在sql 语句中 得到, "select * into @var" 就行了,后边就可以直接运用该变量了。
例子:
1 2 3 4 5 6 7 | SET @sql = concat('select max(id) into @max_id from ', $tableName);PREPARE stmt1 FROM @sql;EXECUTE stmt1;DEALLOCATE PREPARE stmt1; |
此时,@max_id 就保存了查询结果。
参考链接:http://jonllen.javaeye.com/blog/370343
http://topic.csdn.net/u/20100402/00/b13c3d37-2f06-48d2-99c8-697f8a75e441.html
http://hi.baidu.com/annleecn/blog/item/794e3efbf2525a809e514696.html
附上一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | DROP PROCEDURE IF EXISTS get_service_id;DELIMITER $$CREATE PROCEDURE get_service_id( IN $tableName varchar(25), IN $beginDate varchar(25)) SET @sql = concat('select id into @mainId from ', $tableName , ' where date_time>=\"', $beginDate, '\" order by date_time limit 1'); PREPARE stmt1 FROM @sql; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; END;$$DELIMITER ; |
边栏推荐
- 3. 无重复字符的最长子串
- Detailed explanation of MOSN reverse channel
- [Developers must see] [push kit] Collection of typical problems of push service service 2
- Js学习笔记(四)
- JS底层手写
- unity2D横板游戏教程6-敌人AI以及受击动画
- 【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
- junit总结
- 1095 解码PAT准考证 (25 分)(C语言)
- tag单调栈-单调栈预备知识-lt.739. 每日温度
猜你喜欢

Exception(异常) 和 Error(错误)区别解析

Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne

接口测试框架实战(四)| 搞定 Schema 断言

typescript40-class类的保护修饰符

CAD有生僻字如何打出来、如何提交软件相关问题或建议?

typescript42-readonly修饰符

Online password generator tool recommendation

高可用 两地三中心

Js学习笔记(四)

Kotlin-Flow common encapsulation class: the use of StateFlow
随机推荐
Kotlin-Flow常用封装类:StateFlow的使用
Get the Ip tool class
2. 两数相加
2022/08/02 Study Notes (day22) Multithreading
2022暑假牛客多校联赛第一场
在树莓派上搭建属于自己的网页(2)
接口管理工具YApi怎么用?颜值高、易管理、超好用
typescript41-class类的私有修饰符
1054 求平均值 (20 分)
13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
【精讲】利用原生js实现todolist
Coordinate knowledge in digital twin campus scenarios
js garbage collection mechanism
C# async and multithreading
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
建造者模式(Builder Pattern)
How to prepare for the test interface test data
Power button 561. An array of split
接口测试实战| GET/POST 请求区别详解
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data