问题
I am trying to create a view in mySQL that gets created or updated when a user enters a specific value (woeid). I've tried to do this with the stored procedure below:
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `sp_getChildren`(
IN woeid INTEGER(15)
)
BEGIN
CREATE OR REPLACE VIEW `test`.`children`
AS
SELECT *
FROM geoplanet_places gp
WHERE gp.parent_id = woeid;
END
The above, or so I have read, is not possible in the current version of mySQL because a view, within a stored routine, cannot refer to routine parameters or local variables. My question would be what the reason for this is, and if there are any effective alternatives. I don't really want to create a new table and have the procedure update said table.
Any help would be much appreciated, thanks.
回答1:
Try to use prepared statements -
CREATE PROCEDURE sp_getChildren(IN woeid INTEGER(15))
BEGIN
SET @sql = CONCAT('CREATE OR REPLACE VIEW `test`.`children` AS SELECT * FROM geoplanet_places gp WHERE gp.parent_id = ', woeid);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
来源:https://stackoverflow.com/questions/9424690/dynamic-views-alternative-in-mysql