Solved

Limit has variable limits workaround needed?

Posted on 2008-06-20
2
276 Views
Last Modified: 2011-07-08
Seem like I cannot use a variable in a limit clause like so: Limit @rowcount

Is there any workaround for the following syntax?

select @row_count = (Select colRows from tbltemp where colName = 'table1');
set @adj_row_count = (@row_count *.5);

INSERT INTO table2 (col1, col2) Select col1, col2 FROM table1 Limit @adj_row_count

0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Rurne earned 500 total points
ID: 21833920
You're going to need to create a user function to replicate this.  @variables are not allowed in straightforward SQL in MySQL. See:
http://bugs.mysql.com/bug.php?id=11918
http://bugs.mysql.com/bug.php?id=8094

The alternative is to create a stored procedure and call it instead (e.g., "CALL variable_insert();").  Try:
CREATE PROCEDURE variable_insert()
BEGIN
DECLARE row_count, adj_row_count INT;
SELECT colRows INTO row_count FROM tbltemp WHERE colName = 'table1';
set @adj_row_count = row_count / 2;
 
INSERT INTO table2 (col1, col2) SELECT col1, col2 FROM table1 LIMIT adj_row_count;
END;

Open in new window

0
 
LVL 1

Expert Comment

by:CMDAI
ID: 36151068
Im posting this for people like me who spent ages looking for an easy workaround for limit @variable
# THIS WILL FAIL
select * from some_table limit @a ;

#Instead use this work around
@a=0;
select * from some_table where (@a := @a +1) < @your_limit;

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql ide 10 56
Generate PDF from MySQL using PHP 3 71
MySQL stored procedure returning null values 4 37
How can I use javascript variable in mysql query 21 43
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question