当前位置:网站首页>The method of solving stored procedure table name passing through variable in mysql
The method of solving stored procedure table name passing through variable in mysql
2022-08-01 04:26:00 【live healthy and safe】
A problem description
In the stored procedure, simply use the set or declare statement to define the variable, and then directly use it as the sql table name is not enough, mysql will treat the variable name asas table name. The same is true in other sql databases.This was not possible before mysql5.0. After 5.0, a new statement was introduced to achieve a function similar to sp_executesql (only valid for procedure, function does not support dynamic query):
Two solutions
If we need to do the insert operation in the stored procedure, according to the table name passed in from outside.The code is as follows:
drop PROCEDURE if EXISTS p_sync_data;CREATE PROCEDURE `p_sync_data`(IN pa_table_name varchar(255))BEGINDECLARE 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); #Use the concat splicing string function to assign the sql of the copied table to the variable sql_create_tableset @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;# preprocessing tablePREPARE stmt FROM @sql_create_table;# execute sqlEXECUTE stmt USING @id,@cname,@kc,@fs;deallocate prepare stmt;-- insert into tb_kc_789 values(@id,@tname,@kc,@fs);end
Screenshot below
3,.execute statement
call p_sync_data("tb_kc");
commit;
4. Results data
边栏推荐
- <JDBC> 批量插入 的四种实现方式:你真的get到了吗?
- Dynamic Programming 01 Backpack
- Hackers can how bad to what degree?
- A way to deal with infinite debugger
- MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
- PMP 项目质量管理
- Risk strategy important steps of tuning method
- The maximum quantity leetcode6133. Grouping (medium)
- 罗技鼠标体验记录
- Flutter “Hello world“ 程代码
猜你喜欢
随机推荐
今日睡眠质量记录68分
项目风险管理必备内容总结
这里有110+公开的专业数据集
Flutter “Hello world“ 程代码
【无标题】
罗技鼠标体验记录
Article summary: the basic model of VPN and business types
Flutter Tutorial 01 Configure the environment and run the demo program (tutorial includes source code)
预言机简介
UE4 模型OnClick事件不生效的两种原因
PMP 项目资源管理
移动端页面秒开优化总结
Mysql基础篇(Mysql数据类型)
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
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
Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink
The Principle Of Percona Toolkit Nibble Algorithm
对无限debugger的一种处理方式
A way to deal with infinite debugger
ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks