7
目标是根据输入变量在Mysql存储过程中更改查询字符串。Mysql根据逻辑在存储过程中动态构建查询字符串
像这样:
CREATE DEFINER=`root`@`localhost` PROCEDURE `func`(type VARCHAR(15))
BEGIN
SET @type = type;
-- Check for the sort parameter
if @type="asc" THEN
SET @sort = " order by name asc";
elseif @type="desc" THEN
SET @sort = " order by name desc";
else
SET @sort ="";
end if;
SELECT id, name from table @sort;
END
的解决方案是使用执行和CONCAT: CREATE DEFINER ='root' @ 'localhost' PROCEDURE'test'(input VARCHAR(15)) BEGIN SET @input = input; if @ input =“asc”then SET @sort =“order by ActivityLogKey asc”; elseif @input =“desc”then SET @sort =“order by ActivityLogKey desc”;其他 SET @sort =“”; end if; SET @query = CONCAT('select * from activitylog',@ sort,'limit 0,5'); PREPARE stmt FROM @query; EXECUTE stmt; DEALLOCATE PREPARE stmt; END – 2011-12-18 03:57:29
如果这是解决方案,为什么你没有添加它作为答案,并接受它或编辑你的问题。 – 2011-12-18 11:51:13
没有100的声望,我必须等待8个小时才能回答我自己的问题 – 2011-12-18 17:10:40