当前位置:网站首页>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 ; |
边栏推荐
- Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
- 【Biotin Azide|cas:908007-17-0】Price_Manufacturer
- PotPlayer实现上班摸鱼电视自由
- [Harmony OS] [ArkUI] ets development graphics and animation drawing
- 1095 解码PAT准考证 (25 分)(C语言)
- tag单调栈-单调栈预备知识-lt.739. 每日温度
- typescript46-函数之间的类型兼容性
- Object类与常用API
- Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
猜你喜欢

Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)

FileZilla 搭建ftp服务器

Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface

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

接口测试如何准备测试数据
![[Harmony OS] [ArkUI] ets development graphics and animation drawing](/img/36/f4c91f794b1321f11a24505d1617fb.png)
[Harmony OS] [ArkUI] ets development graphics and animation drawing

超好用的画图工具推荐

2022/08/02 学习笔记 (day22) 多线程

【Harmony OS】【ARK UI】Date 基本操作

【Harmony OS】【FAQ】鸿蒙问题合集1
随机推荐
1079 延迟的回文数 (20 分)
DFS对剪枝的补充
Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
odps的临时查询能在写sql的时候就给结果一个命名不?
信息编码、存储压缩与密码学
MOSN 反向通道详解
Exception(异常) 和 Error(错误)区别解析
背压机制
Interface test practice | Detailed explanation of the difference between GET / POST requests
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
Jmeter 模拟多用户登录的两种方法
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
GIS数据漫谈(六)— 投影坐标系统
FileZilla 搭建ftp服务器
4.如何避免缓存穿透、缓存击穿、缓存雪崩
接口测试框架实战(二)| 接口请求断言
3. 无重复字符的最长子串
typescript40-class类的保护修饰符
Interface testing framework of actual combat (2) | interface request assertion
Shell conditional statement judgment