mysql 存储过程动态传递参数

Posted 寻水的鱼001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 存储过程动态传递参数相关的知识,希望对你有一定的参考价值。

delimiter //
create procedure oneKey(in newName varchar(250),in oldName varchar(250),in idNum INT)
BEGIN  
    SET @sqlStmt = CONCAT(‘insert into ‘,newName,‘ (`name`,`age`,`sex`,`major`,`pass`,`photo`)
        select `name`,`age`,`sex`,`major`,`pass`,`photo` from ‘,oldName,‘ where id = ‘,idNum);
    PREPARE stmt FROM @sqlStmt;
    EXECUTE stmt;
END;
//
delimiter ;
 
call oneKey(‘stu1‘,‘student‘,5);

  自己模仿着,然后成功的完成了数据库名的动态替换:

CREATE DEFINER=`pnggu`@`%` PROCEDURE `district_to_db`(IN city_id int ( 20 ), IN basedb_city VARCHAR ( 255 ))
BEGIN
-- 参数说明:city_id:城市id;
--                     basedb_city 需要传递到的业务数据库名称
-- REPLACE INTO basedb_sh.district ( TID, STATUS, DistrictName, DistrictFullName, DescriptionDate )
-- select 
-- id as TID,
-- 0 as STATUS,
-- full_name as DistrictName,
-- full_name as DistrictFullName,
-- NOW() as DescriptionDate
-- from python3.district where parent_id = ‘310100‘;

SET @sqlStmt = CONCAT(‘REPLACE INTO ‘,basedb_city,‘ .district(`TID`,`STATUS`,`DistrictName`,`DistrictFullName`,`DescriptionDate`)
        select `id` as TID,0 as STATUS,`full_name` as DistrictName,`full_name` as DistrictFullName,NOW() as DescriptionDate from python3.district where parent_id = ‘,city_id);
    PREPARE stmt FROM @sqlStmt;
    EXECUTE stmt;
END

 

以上是关于mysql 存储过程动态传递参数的主要内容,如果未能解决你的问题,请参考以下文章

如何在存储过程中动态传递表名 - ORACLE [重复]

存储过程中参数是动态的,需要判断是不是为空

如何将C#数据表传递给mysql的存储过程参数

如何将输出参数从 vb.net 传递到 mysql 存储过程?

存储过程,将表名作为参数传递

MySQL-存储过程动态执行sql