以下在mysql语句中工作正常,但抛出错误:"存储函数或触发器中不允许动态SQL#34;在创建mysql函数时。
SET @type='read';
SET @per_status=CONCAT('SELECT u.',@type,' INTO @res FROM user_permission u WHERE u.users_id = 2 AND u.web_pages_id=1');
PREPARE stmt1 FROM @per_status;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SELECT @res as 'Status';
这是我到目前为止所做的,但无法摆脱错误。我们将非常感谢您的帮助。
DELIMITER $$
CREATE DEFINER=`abi`@`localhost` FUNCTION `func_check_status`(`emp_id` INT(11) UNSIGNED, `type` VARCHAR(20), `page_id` INT(11) UNSIGNED) RETURNS varchar(20) CHARSET latin1
NO SQL
BEGIN
DECLARE res_status varchar(20);
SET @per_status=CONCAT('SELECT u.',type,' INTO @res FROM user_permission u WHERE u.users_id = 2 AND u.web_pages_id=1');
PREPARE stmt1 FROM @per_status;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET res_status = (SELECT @res);
RETURN res_status;
END$$
DELIMITER ;
编辑:根据documentation,我了解预处理语句只能用于过程而不能用于函数或触发器。在@ wchiquito&{39}的帮助下,我试图在我需要的另一个程序中使用包含准备好的语句的程序。但我仍然得到语法错误。是否可以在select查询中使用该过程?如果是,请帮助下面的任何人请帮助我从我的最终尝试的演示:
DELIMITER $$
CREATE DEFINER=`abi`@`localhost` PROCEDURE `sp_checked_attr_for_permission_checkboxes`(IN `emp_id` INT(11) UNSIGNED, IN `type` VARCHAR(20), IN `page_id` INT(11) UNSIGNED, OUT `res_status` VARCHAR(3))
NO SQL
BEGIN
SET @`emp_id` := `emp_id`,
@`page_id` := `page_id`,
@`per_status` := CONCAT('
SELECT `u`.`', `type`, '` INTO @`res_status`
FROM `user_permission` `u`
WHERE `u`.`users_id` = ? AND `u`.`web_pages_id` = ?
');
PREPARE `stmt` FROM @`per_status`;
EXECUTE `stmt` USING @`emp_id`, @`page_id`;
DEALLOCATE PREPARE `stmt`;
SET `res_status` := @`res_status`;
END$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`abi`@`localhost` PROCEDURE `sp_select_pages_list`(IN `emp_id` INT(11) UNSIGNED)
NO SQL
select
`web_pages`.`web_pages_id` as 'ID',
`web_pages`.`name` 'Name',
`web_pages`.`page_name` as 'Page Name',
concat('<input type="checkbox" value="',(call sp_checked_attr_for_permission_checkboxes(emp_id,'read',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'" id="',`web_pages`.`web_pages_id`,'[read]" name="',`web_pages`.`web_pages_id`,'[read]" onclick="change_permission(''',`web_pages`.`web_pages_id`,'[read]'');" ',(call sp_checked_attr_for_permission_checkboxes(emp_id,'read',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'>') as 'Read',
concat('<input type="checkbox" value="',(call sp_checked_attr_for_permission_checkboxes(emp_id,'insert',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'" id="',`web_pages`.`web_pages_id`,'[insert]" name="',`web_pages`.`web_pages_id`,'[insert]" onclick="change_permission(''',`web_pages`.`web_pages_id`,'[insert]'');" ',(call sp_checked_attr_for_permission_checkboxes(emp_id,'insert',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'>') as 'Insert',
concat('<input type="checkbox" value="',(call sp_checked_attr_for_permission_checkboxes(emp_id,'edit',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'" id="',`web_pages`.`web_pages_id`,'[edit]" name="',`web_pages`.`web_pages_id`,'[edit]" onclick="change_permission(''',`web_pages`.`web_pages_id`,'[edit]'');" ',(call sp_checked_attr_for_permission_checkboxes(emp_id,'edit',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'>') as 'Edit',
concat('<input type="checkbox" value="',(call sp_checked_attr_for_permission_checkboxes(emp_id,'delete',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'" id="',`web_pages`.`web_pages_id`,'[delete]" name="',`web_pages`.`web_pages_id`,'[delete]" onclick="change_permission(''',`web_pages`.`web_pages_id`,'[delete]'');" ',(call sp_checked_attr_for_permission_checkboxes(emp_id,'delete',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'>') as 'Delete',
concat('<input type="checkbox" value="',(call sp_checked_attr_for_permission_checkboxes(emp_id,'update',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'" id="',`web_pages`.`web_pages_id`,'[update]" name="',`web_pages`.`web_pages_id`,'[update]" onclick="change_permission(''',`web_pages`.`web_pages_id`,'[update]'');" ',(call sp_checked_attr_for_permission_checkboxes(emp_id,'update',`web_pages`.`web_pages_id`,@res_status); SELECT @res_status;),'>') as 'Update'
FROM
`web_pages`
WHERE
`web_pages`.`status` = 'active'
$$
DELIMITER ;