以下是我的存储过程
DELIMITER $$
USE `us_db`$$
DROP PROCEDURE IF EXISTS `getTransactionDetails`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getTransactionDetails`(IN _MainDB VARCHAR(32),
IN _mobile VARCHAR(50),
IN _market_id VARCHAR(50),
IN _from DATE,
IN _to DATE)
BEGIN
SELECT
transaction_id AS txn_id,
DATE_FORMAT(createdon,"%d-%b-%Y %H:%i:%S") AS createdon,
order_id,
order_conv_total AS amount,
msisdn AS msisdn,
channel_id,
transmode_id,
statusid,
(SELECT fullname FROM _MainDB.entities WHERE entity_id=retailer_id) AS retailer,
FROM `us_transaction` `t` WHERE
t.market_id = _market_id AND msisdn = _mobile AND t.createdon BETWEEN CONCAT(DATE(_from)," 00:00:00") AND CONCAT(DATE(_to)," 23:59:59") ORDER BY t.createdon DESC LIMIT 100;
END$$
DELIMITER ;
现在问题是零售商名称存储在我想通过_MainDB调用的单独数据库中。此DB名称作为参数出现,但由于_MainDB被视为varchar类型,因此它不能正常运行。如何将DB名称作为存储过程中的参数???
答案 0 :(得分:0)
用户变量和CONCAT功能;
DECLARE STR VARCHAR(1000)
SET @STR = 'SELECT
transaction_id AS txn_id,
........
(SELECT fullname FROM
'
SET @STR = CONCAT(@STR,@_MAINDB)
SET @STR = CONCAT(@STR,'.entities ..................')
PREPARE STMT FROM @STR
EXECUTE STMT