简体   繁体   中英

Set Result Limit By Variable and Error When Declaring Variable

I want to set the limit of shown result from 0 until the value of declared variable. But I found the error when declaring the variable, it said : "#1064 - you have an error in your SQL syntax.". I know that DECLARE syntax only permitted in the BEGIN ... END statement. But why I always get an error?

This is what I've try :

BEGIN
DECLARE panjang INT DEFAULT 0
END;

SET panjang := 10;
SELECT * FROM `TABLENAME`
limit 0, panjang

Can you please tell me what is missing here? I'm using mysql version 5.5.34.

  1. DECLARE is allowed only in Stored Routines.
  2. Inside a stored routine, you can construct a SELECT with a variable for LIMIT; this uses CONCAT, PREPARE, and EXECUTE.

If this SELECT is coming from some other programming language (PHP, VB, Java, etc), I suggest you build the SQL there, so you won't have to learn about Stored Procedures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM