Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-30-2009, 04:14 PM   PM User | #1
DDaku
New Coder

 
Join Date: Aug 2009
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
DDaku is an unknown quantity at this point
Angry Syntax error with DECLARE variable?

I usually work with MS-SQL 2000/2005, so I might be doing something really stupid here, but the syntax for declaring a variable (within a Stored Procedure) seems pretty simple, based on http://dev.mysql.com/doc/refman/5.0/en/declare.html.

The code I'm trying in a Script Tab in MySQL Query Browser is:

Code:
DELIMITER $$

BEGIN
  DECLARE x INT;
  SET x = 5;
  SELECT x;
END$$

DELIMITER ;
When I execute that, I get the following error:

Quote:
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 'DECLARE x INT;
SET x = 5;
SELECT x;
END' at line 2
This is on a MySQL 5.1 server, but I've also tried it on another 5.0 server with the same error (and the docs link up top is for 5.0)

Last edited by DDaku; 12-30-2009 at 05:29 PM.. Reason: Resolved
DDaku is offline   Reply With Quote
Old 12-30-2009, 05:21 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You need to create a procedure with this begin/end block inside it.

PHP Code:
DELIMITER $$

CREATE PROCEDURE MYPROCEDURE ()
BEGIN
  
DECLARE x INT;
  
SET x 5;
  
SELECT x;
END$$

DELIMITER 
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
DDaku (12-30-2009)
Old 12-30-2009, 05:30 PM   PM User | #3
DDaku
New Coder

 
Join Date: Aug 2009
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
DDaku is an unknown quantity at this point
...wow. Thanks.
DDaku is offline   Reply With Quote
Reply

Bookmarks

Tags
declare, mysql, procedure, stored procedure, variable

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 PM.


Advertisement
Log in to turn off these ads.