Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    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:

    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 06:29 PM. Reason: Resolved

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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 

  • Users who have thanked Fumigator for this post:

    DDaku (12-30-2009)

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    ...wow. Thanks.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •