Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring variables

Status
Not open for further replies.

aalnaif

Technical User
Jan 12, 2007
44
CA
I know this is really simple, but I've been suck on this for 2 hrs and I can't figure it out. I'm getting a syntax error when declaring a variable within an UPDATE statement. Here's how I'm declaring it:

DECLARE cnt int
SET cnt = 1

I'm using MySQL.

 
Well I dont think that you can declare a variable within an UPDATE statement. You could declare a user-defined variable in a procedure and use it in an UPDATE statement.

Code:
DECLARE @cnt INT
SET @cnt = 1

UPDATE MyTable SET
  some_column = @cnt
WHERE table_id = @cnt
 
You DECLARE variables you use in stored proceedures and functions but DECLARE is not part of an update statements syntax.

Why dont you post your code so we can see what you are on about.
 
OK, I tried making a procedure and declaring the variable outside of the UPDATE statement, but I'm still getting syntax errors related to declaring and setting the variable. Here is my code:

DELIMITER $$

DROP PROCEDURE IF EXISTS `procA`.`tableA`$$

CREATE PROCEDURE `procA`.`tableA`()
BEGIN
DECLARE cnt int
Update tableA
Set visit_type =
SET cnt = 1
WHILE (cnt < 5) -- largest country code is 4 digits
BEGIN
case when (count(select location from main where SUBSTRING(pid,4,cnt)) = 1) then
(select location from main where SUBSTRING(pid,4,cnt))
cnt = 5
else
cnt = cnt + 1
END
END
END
where isnull(visit_type);
END$$

DELIMITER ;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top