当前位置:网站首页>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 ; |
边栏推荐
- 1095 解码PAT准考证 (25 分)(C语言)
- 接口测试框架实战(一) | Requests 与接口请求构造
- typescript46-函数之间的类型兼容性
- 【 Harmony OS 】 【 ano UI 】 lightweight data storage
- 接口测试框架实战(二)| 接口请求断言
- Interface Test Framework Practice (4) | Get Schema Assertion
- Lambda表达式案例
- typescript41-class类的私有修饰符
- 安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
- Talking about GIS Data (6) - Projected Coordinate System
猜你喜欢

MySql数据库

Where is the value of testers

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

【Harmony OS】【FAQ】Hongmeng Questions Collection 1

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

【 Harmony OS 】 【 ano UI 】 lightweight data storage

BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier

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

idea使用@Autowired注解爆红原因及解决方法

Exception (abnormal) and Error (error) difference analysis
随机推荐
Concepts and Methods of Exploratory Testing
探索性测试的概念及方法
接口测试框架实战(一) | Requests 与接口请求构造
Business table analysis - balance system
Interface test practice | Detailed explanation of the difference between GET / POST requests
超好用的画图工具推荐
Interface Test Framework Practice (4) | Get Schema Assertion
集合框架知识
js implements a bind function
高可用 两地三中心
c语言结构体中的冒泡排序
[Developers must see] [push kit] Collection of typical problems of push service service 2
Create a tree structure
1054 求平均值 (20 分)
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
typescript40-class类的保护修饰符
2022暑假牛客多校联赛第一场
[Fine talk] Using native js to implement todolist
DFS's complement to pruning
js中的闭包