当前位置:网站首页>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 ; |
边栏推荐
- UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
- Js学习笔记(四)
- Flink状态
- ss-3.工程重构
- 1060 爱丁顿数 (25 分)
- 用户密码加密工具
- 在树莓派上搭建属于自己的网页(2)
- Installation of Apache DolphinScheduler version 2.0.5 distributed cluster
- Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
- 建立树形结构
猜你喜欢

在树莓派上搭建属于自己的网页(1)

在线密码生成工具推荐

Apache DolphinScheduler版本2.0.5分布式集群的安装

unity2D横板游戏教程6-敌人AI以及受击动画

高可用 两地三中心
![[Harmony OS] [ARK UI] ETS context basic operations](/img/40/d5924477c42e2b3246eb212f4be534.png)
[Harmony OS] [ARK UI] ETS context basic operations

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

Build your own web page on the Raspberry Pi (2)

High availability, two locations and three centers

13.
lt.647. Palindromic substring + lt.516. Longest palindrome subsequence
随机推荐
Interface Test Framework Practice (4) | Get Schema Assertion
How to prepare for the test interface test data
建造者模式(Builder Pattern)
Concepts and Methods of Exploratory Testing
获取Ip工具类
斐讯K2路由编译Padavan华硕固件和心得
Kotlin-Flow common encapsulation class: the use of StateFlow
Get the Ip tool class
DFS's complement to pruning
c语言结构体中的冒泡排序
js implements a bind function
集合框架知识
idea uses @Autowired annotation to explain the reasons and solutions
【Harmony OS】【FAQ】鸿蒙问题合集1
shell脚本循环语句
接口测试框架实战(二)| 接口请求断言
Online password generator tool recommendation
Interface testing framework combat (3) | JSON request and response assertion
力扣561. 数组拆分
junit总结