当前位置:网站首页>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) ) BEGIN select * 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 ; |
边栏推荐
- JS学习笔记(三)
- Where is the value of testers
- 1058 选择题 (20 分)(C语言)
- Create a tree structure
- Interface Test Framework Practice (4) | Get Schema Assertion
- Interface testing framework combat (3) | JSON request and response assertion
- js garbage collection mechanism
- [Developers must see] [push kit] Collection of typical problems of push service service 2
- 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
- Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
猜你喜欢
MOSN 反向通道详解
Installation of Apache DolphinScheduler version 2.0.5 distributed cluster
tag单调栈-单调栈预备知识-lt.739. 每日温度
【Harmony OS】【FAQ】Hongmeng Questions Collection 1
js实现一个 bind 函数
Alienware上线首个数字时装AR试穿体验
Redis6学习笔记
接口测试实战| GET/POST 请求区别详解
Bubble sort in c language structure
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
随机推荐
Secondary development of WinForm controls
13.
lt.647. Palindromic substring + lt.516. Longest palindrome subsequence 力扣561. 数组拆分
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
信息编码、存储压缩与密码学
高可用 两地三中心
Exception (abnormal) and Error (error) difference analysis
测试人员的价值体现在哪里
Build your own web page on the Raspberry Pi (2)
Talking about GIS Data (6) - Projected Coordinate System
C# async and multithreading
Online password generator tool recommendation
2. 两数相加
Interface test framework combat (1) | Requests and interface request construction
【 Harmony OS 】 【 ano UI 】 lightweight data storage
在树莓派上搭建属于自己的网页(1)
MCM box model modeling method and source analysis of atmospheric O3
Odps temporary query can write SQL, turned out to a named?
Installation of Apache DolphinScheduler version 2.0.5 distributed cluster