Dynamic Views Alternative in mySQL

一曲冷凌霜 提交于 2019-12-24 10:29:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!