我有一个包含SELECT语句的SelectProc程序。我想添加一个过程参数LimitRowsCount并使用它如下:
CREATE PROCEDURE SelectProc (IN LimitRowsCount INTEGER UNSIGNED)
BEGIN
SELECT (...)
LIMIT LimitRowsCount;
END
但这种做法不起作用。
SELECT本身包含嵌套子查询,因此我无法从中创建视图。有没有一种方法比动态SQL(预处理语句)更多?
答案 0 :(得分:3)
CREATE PROCEDURE SelectProc (IN LimitRowsCount INT)
BEGIN
SET @LimitRowsCount1=LimitRowsCount;
PREPARE STMT FROM "SELECT (...) LIMIT ?";
EXECUTE STMT USING @LimitRowsCount1;
END
答案 1 :(得分:2)
从手册:
The LIMIT clause can be used to constrain the number of rows
returned by the SELECT statement. LIMIT takes one or two numeric
arguments, which must both be nonnegative integer constants
(except when using prepared statements).
MySQL Manual - 12.2.8. SELECT Syntax
所以这不是 - 你不能。