views:

47

answers:

2

Hi I'm getting the error while executing following code in MySQL :

BEGIN
DECLARE right_most_sibling INTEGER;
SET right_most_sibling
= (SELECT rgt
FROM Personnel
WHERE emp = 'Albert');
UPDATE Personnel
SET lft = CASE WHEN lft > right_most_sibling
THEN lft + 2
ELSE lft END,
rgt = CASE WHEN rgt >= right_most_sibling
THEN rgt + 2
ELSE rgt END
WHERE rgt >= right_most_sibling;
INSERT INTO Personnel (emp, lft, rgt)
VALUES ('Bert', right_most_sibling,
(right_most_sibling + 1))
END;

I'm getting the following error :

1064 - 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 right_most_sibling INTEGER' at line 2

Any ideas, how do I fix it ?

I was following this tutorial

link text

Thank you

+1  A: 

It looks like you're using stored procedure syntax, without declaring a stored procedure header.

You can't use DECLARE unless it's in a compound statement in the body of a stored procedure.

http://dev.mysql.com/doc/refman/5.1/en/declare.html says:

DECLARE is allowed only inside a BEGIN ... END compound statement...

http://dev.mysql.com/doc/refman/5.1/en/begin-end.html says:

BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs.

The tutorial you're linking to was written by Joe Celko, and I'd assume he's writing code that works on Oracle or maybe IBM DB2. It's unlikely that he ever uses MySQL for his example code.


Re your comment, I got it to at least accept the syntax by declaring the block within a procedure:

DELIMITER //
CREATE PROCEDURE insert_new_node()
BEGIN
DECLARE right_most_sibling INTEGER;
. . . 
END//
DELIMITER ;

I have not tested the procedure, because I'm not going to run through the entire tutorial.

Bill Karwin
this code above is from the link I pasted written by Joe Celko and not me, can you tell me how to correct this code?This is exactly what I need I just googled it ..
c0mrade
+2  A: 

Don't try to just copy/paste code -- read & understand the concepts, then put them to use on your specific problem.

In my answer to your previous question, I edited and added a second link to page showing how to implement this stuff at the PHP level (since you're using PHP).

The Celko stuff is good -- as Bill says, you're not going to be able to just drop it into mySQL without thinking about what's going on.

First understand the concepts, then go ahead an build your own implementation.

timdev
Not sure what you mean by concepts, if you think I don't understand binary tree concept I assure you that is not the case. On the other hand if you meant "mysql syntax" you are right. This is the first time I'm working with if/else/whatever with mysql before I used SELECT, UPDATE, DELETE. Thank you again for your time and effort to post.
c0mrade
I found other useful link http://dev.mysql.com/tech-resources/articles/hierarchical-data.html -> its actually closest to my needs, I'm using it to solve my problem .. hopefully everything will work out just fine
c0mrade
What I meant is if you get the nested set concept, then run with it. Don't worry about sticking a bunch of conditional statements (IF/ELSE) in SQL. The nice thing about nested set stuff is that with a couple of fairly efficient queries, you can get all the important operations done. There's nothing wrong with implementing those operations as stored procedures -- it's generally the best way to do it. But there's nothing stopping you from implementing the logic in PHP first, and worrying about optimizing later.
timdev