如何 - 从存储过程中的返回变量命名模式?

时间:2015-06-23 16:23:44

标签: mysql

我有一个归档工作,我需要允许用户输入客户ID。由此,存储过程需要选择客户名称,从名称中删除所有空格并创建具有该名称的模式。 到目前为止,我有:

call ArchiveCustomerRecords_proc(41)

这应该吸引客户" Fred Schmidt"。然后存储的proc应该继续并创建一个名为" FredSchmidt"的模式。我无法让sp进行REPLACE(),除了模式名称的字符串文字之外,我无法创建模式。

这是我尝试过的一些代码,但都没有。

CREATE DEFINER=`root`@`%` PROCEDURE `ArchiveCustomerRecords_proc`(IN CustID Integer)
BEGIN

-- Declare CustName varchar(50);

-- Set @s = 'CREATE SCHEMA ?';

-- PREPARE stmt1 FROM @s; 

SET @CustName = 'Select CustomerName from tblCustomer where CustomerID =' CustID;

SET @CustName = REPLACE(@CustName,' ', '');

-- SET CustName = CONCAT('CREATE SCHEMA ',CustName);

-- EXECUTE stmt1 USING @CustName;

-- DEALLOCATE PREPARE stmt1;

    SET @s = CONCAT('CREATE DATABASE ', @CustName);
    PREPARE stmt1 FROM @s;
    EXECUTE stmt1;
    DEALLOCATE PREPARE stmt1;
END

我终于明白了。

CREATE DEFINER=`root`@`%` PROCEDURE `ArchiveCustomerRecords_proc`(IN CustID Integer)
BEGIN

DECLARE CustName varchar(50);

Select CustomerName into CustName from tblCustomer where CustomerID = CustID;
Select REPLACE(CustName,' ', '') into CustName;

SET @SQL = CONCAT('CREATE DATABASE ', CustName);
PREPARE stmt1 FROM @SQL;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END

0 个答案:

没有答案