当前位置:网站首页>mysql中解决存储过程表名通过变量传递的方法
mysql中解决存储过程表名通过变量传递的方法
2022-08-01 04:20:00 【健康平安的活着】
一 问题描述
在存储过程中,简单的用set或者declare语句定义变量,然后直接作为sql的表名是不行的,mysql会把变量名当作表名。在其他的sql 数据库中也是如此。这在 mysql5.0之前是不行的,5.0之后引入了一个全新的语句,可以达到类似sp_executesql的功能(仅对procedure有效,function不支持动态查询):
二 解决办法
如果我们需要在存储过程中,根据外面传进来的表名,做插入操作。代码如下:
drop PROCEDURE if EXISTS p_sync_data;
CREATE PROCEDURE `p_sync_data`(
IN pa_table_name varchar(255)
)
BEGIN
DECLARE n int(10);
set @currMonth=MONTH(CURDATE());
set @[email protected];
if @a=1 or @a=2 or @a=3 then set n=123;
elseif @a=4 or @a=5 or @a=6 then set n=456;
elseif @a=7 or @a=8 or @a=9 then set n=789;
else set n=101112;
end if;
set @tbName=concat(pa_table_name,'_',n); #利用concat拼接字符串函数,将复制表的sql赋值到变量sql_create_table
set @id=7;
set @cname="zhangsan";
set @kc="yingyu";
set @fs=90;
set @sql_create_table = concat('insert into ',@tbName," set `id`=?,`name`=?,`kecheng`=?,`fenshu`=?;");
select @sql_create_table;
# 预处理建表
PREPARE stmt FROM @sql_create_table;
# 执行sql
EXECUTE stmt USING @id,@cname,@kc,@fs;
deallocate prepare stmt;
-- insert into tb_kc_789 values(@id,@tname,@kc,@fs);
end
截图如下
3,。执行语句
call p_sync_data("tb_kc");
commit;
4.结果数据
边栏推荐
- Valentine's Day Romantic 3D Photo Wall [with source code]
- 解决ffmpeg使用screen-capture-recorder录屏,有屏幕缩放的情况下录不全的问题
- The Principle Of Percona Toolkit Nibble Algorithm
- Dart named parameter syntax
- SQL Analysis of ShardingSphere
- Message queue design based on mysql
- Article summary: the basic model of VPN and business types
- 博客系统(完整版)
- Input input box cursor automatically jumps to the last bug after the previous input
- 罗技鼠标体验记录
猜你喜欢
【搜索专题】看完必会的BFS解决最短路问题攻略
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
Pyspark机器学习:向量及其常用操作
TypeScript简化运行之ts-node
Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
【无标题】
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
Software Testing Interview (3)
预言机简介
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
随机推荐
Take you to experience a type programming practice
Flink 1.13 (8) CDC
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
lambda
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
Introduction to Oracle
「以云为核,无感极速」顶象第五代验证码
PMP 相关方管理必背总结
Software Testing Interview (3)
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
RSA主要攻击方法
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
在沈自所的半年总结
MySQL4
Message queue design based on mysql
Summary of mobile page optimization in seconds
Basic Theoretical Knowledge of Software Testing - Use Cases
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
高数 | 【重积分】线面积分880例题