MySQL Forums
Forum List  »  Stored Procedures

Re: Dynamic Sql Statement in SP
Posted by: Roland Bouman
Date: January 11, 2006 03:56PM

Hi Emma,

I think you can do this without dynamic SQL. It's certainly what I would try.

First, you need to prefix the parameternames, so you can distinguish between them and the columns you're updating. I like a p_ for that, but you can use whatever you like best:

PROCEDURE `db_scribbles`.`sp_UpdateAuthor`(
IN p_author_id INTEGER
, IN p_firstname VARCHAR(50)
, IN p_surname VARCHAR(50)
, IN p_personal_webpage LONGTEXT
)
UPDATE tbl_author
SET firstName = COALESCE(p_firstname,firstName)
, surName = COALESCE(p_surName,surName)
, personal_webpage= COALESCE(p_personal_webpage,personal_webpage)
WHERE author_id = p_author_id

(I'm gueassing you intended to include the WHERE clause, you certainly don't want to update *ALL* the records in the author table, right?)

You see, COALESCE() takes two or more parameters, and returns the value of the first (reading from left to right) NOT NULL parameter. In this statement, the columns will be updated with the corresponding parameter, but if the parameter is in fact NULL, the current column value is assigned.

See: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html for more info on COALESCE().

(MySQL is even so smart that it wont even touch a particular field in a record during an update if the current value equals the assigned value; see: http://dev.mysql.com/doc/refman/5.0/en/update.html. Of course, the result would be exactly the same even if it would re-assign the current value; either way it should solve your problem)

BTW, the reason your original syntax is rejected is indeed to be found in the DECLARE line. You see, the @ in front of the variable name tells MySQL that you are referring to a so-called user variable (as Ming points out, user variable is actually a bit of a misnomer, see the previous posts in this thread). Well, you can't declare those explicitly. You can only declare local variables this way. For more info on this, read (or reread) this post:

http://forums.mysql.com/read.php?98,62373,62512#msg-62512

Options: ReplyQuote


Subject
Views
Written By
Posted
2758
January 09, 2006 06:22AM
1481
January 09, 2006 08:06AM
1219
January 09, 2006 02:10PM
1801
January 09, 2006 08:02PM
1685
January 10, 2006 12:24AM
1459
January 10, 2006 03:07AM
1674
January 10, 2006 06:51AM
1397
January 10, 2006 01:49PM
1742
January 10, 2006 06:35PM
1546
January 10, 2006 07:22PM
1632
January 10, 2006 10:33PM
1628
January 11, 2006 03:10AM
2200
January 11, 2006 04:27AM
1656
January 11, 2006 06:54AM
1489
January 11, 2006 03:34PM
Re: Dynamic Sql Statement in SP
1649
January 11, 2006 03:56PM
1637
January 12, 2006 03:20AM
1446
January 12, 2006 04:44AM
1418
January 11, 2006 09:08PM
1667
January 12, 2006 03:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.