contano limitanti righe selezionate con un parametro di stored procedure in MySQL
Domanda
Ho un wich procedura di SelectProc contiene SELECT. Voglio aggiungere un LimitRowsCount procedura di param e usarlo come segue:
CREATE PROCEDURE SelectProc (IN LimitRowsCount INTEGER UNSIGNED)
BEGIN
SELECT (...)
LIMIT LimitRowsCount;
END
ma questo approccio non funziona.
Il SELEZIONA stesso contiene subquery nidificati, quindi non posso creare vista da esso. C'è un modo più propper quindi SQL dinamico (pareri redatti)?
Soluzione
CREATE PROCEDURE SelectProc (IN LimitRowsCount INT)
BEGIN
SET @LimitRowsCount1=LimitRowsCount;
PREPARE STMT FROM "SELECT (...) LIMIT ?";
EXECUTE STMT USING @LimitRowsCount1;
END
Altri suggerimenti
Dal manuale:
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 Manuale - 12.2.8. SELEZIONA Sintassi
Ecco, questo è un no -. Non si può
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow