当前位置:网站首页>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);endScreenshot below

3,.execute statement
call p_sync_data("tb_kc");
commit;
4. Results data

边栏推荐
- TIM登陆时提示00001(TIM00001)
- Game Theory (Depu) and Sun Tzu's Art of War (42/100)
- button remove black frame
- 在互联网时代,有诸多「互联网+」模式的诞生
- 解决ffmpeg使用screen-capture-recorder录屏,有屏幕缩放的情况下录不全的问题
- 数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》
- Visual Studio提供的 Command Prompt 到底有啥用
- The maximum quantity leetcode6133. Grouping (medium)
- 风险策略调优中重要的三步分析法
- 软件测试面试(三)
猜你喜欢

Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记

win10 fixed local IP

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

button remove black frame

【愚公系列】2022年07月 Go教学课程 025-递归函数

typescript27-枚举类型呢

Introduction to Oracle

预言机简介

基于Arduino制作非接触式测温仪

Typescript20 - interface
随机推荐
Immutable
6-23漏洞利用-postgresql代码执行利用
数组问题之《两数之和》以及《三数之和 》
25. Have you been asked these three common interview questions?
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
PMP 项目沟通管理
开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
typescript20-接口
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
The maximum quantity leetcode6133. Grouping (medium)
Mysql中的数据类型和运算符
Difference Between Compiled and Interpreted Languages
Flink 1.13 (8) CDC
认真对待每一个时刻
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
软件测试基础理论知识—用例篇
这里有110+公开的专业数据集
深圳某游戏研发公司给每个工位都装监控,网友:堪比坐牢!
leetcode:126. Word Solitaire II
PMP 80个输入输出总结