mysql stored procedure call unable to access parameter value
I have a stored procedure named create_new_db(p VARCHAR(45)) on mysqldb. The syntax is as follows.
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `create_new_db`( p VARCHAR(45))
BEGIN
CREATE SCHEMA IF NOT EXISTS p DEFAULT CHARACTER SET latin1 ;
-- -----------------------------------------------------
-- Table `p`.`events`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS p.events ( yada, yada, ...)
When I call the stored procedure using the
Call mysqldb.create_new_db('new_db_name');
The procedure executes, the database and tables get created but the parameter i am passing in is NOT being accessed. The database that is created is named p and not the name paramter that i pass in to the procedure
I have tried assigning the paramater to a global variable within the script
@P = p;
I can then see that the parameter name that i am passing into the procedure is getting into the stored procedur开发者_如何学Goe but how come it not working ??
I am likely missing something obvious here but ... some help would be appreciated.
You can't create dynamic sql like that. You must use EXECUTE IMMEDIATE 'some dynamic sql stmt'
You pass a string to it, so build your sql up in your stored proc, like this:
EXECUTE IMMEDIATE concat('CREATE SCHEMA IF NOT EXISTS ', p, ' DEFAULT CHARACTER SET latin1');
and similar for your other CREATE
statements
精彩评论