0

It doesn't like the limit line below. How would I use the variable @row in this case to limit the result set?

SELECT @row := 5;
SELECT * FROM MyTable
limit @row

Error:

Unexpected '@row'

2 Answers 2

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 https://dev.mysql.com/doc/refman/8.0/en/select.html

So,

SELECT * FROM MyTable  
limit 5
Sign up to request clarification or add additional context in comments.

1 Comment

You can use ? placeholders in prepared statements, or in stored routines you can use integer-typed routine parameters or local variables. But not user variables, which have no declared type.
0

You could use a prepared statement...

SET @row = 5;
SET @s = CONCAT('SELECT * FROM MyTable LIMIT ', @row);
PREPARE stmt FROM @s;
EXECUTE stmt;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.