SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 7th, 2007, 12:56 PM
vamsinadella vamsinadella is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 2 vamsinadella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 27 sec
Reputation Power: 0
MYSQL Function - Syntax Error

Hi,

I'm a newbie in MYSQL. I'm trying to create this small function which takes 2 parameters - 'Table Name and Column name' and returns the (max ID +1) of the column from the table. Basically it is returning the max+1 of primary key.

But the catch is my primary keys are Varchars. It has 2 letters in the beginning and then the number. so I'm trying to get the number substring and increment it and then concatenate string and incremented number and return it back.

I'm getting this weird syntax error for this function and I'm not able to debug it.


Script line: 4 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= substr(val,1, 2);
intval= substr(val,3);
intval= intval + 1;
val =' at line 7


I have checked the syntax using a select statement and it works fine. Bt i don;t know what is happening here.

Can anyone give me any idea what is wrong? Thanks.


here is the function:


DELIMITER $$

DROP FUNCTION IF EXISTS `perepository2`.`Get_Max_Id` $$
CREATE DEFINER=`root`@`localhost` FUNCTION `Get_Max_Id`(TabName Varchar(50),ColName Varchar(50)) RETURNS varchar(50) CHARSET latin1
BEGIN
Declare val, charval varchar(50);
Declare intval int;

Select max(ColName) from TabName into val;
charval= substr(val,1, 2);
intval= substr(val,3);
intval= intval + 1;
val = concat(charval, intval);
RETURN val;
END $$

DELIMITER ;

Reply With Quote
  #2  
Old March 8th, 2007, 12:22 PM
vamsinadella vamsinadella is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 2 vamsinadella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 27 sec
Reputation Power: 0
Cool....Nobody replied. well i got the answer myself. But for anyone who may have the same problem in future, here is the solution.

You have to use SET before assigning a value to a variable. From my above code instead of saying:

charval= substr(val,1, 2);

we have to say:

SET charval= substr(val,1, 2);

and same goes with any assignment statement. This a MYSQL thing may be.

- Vamsi

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > MYSQL Function - Syntax Error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway