0

I have a problem with this sql code.

I have a table friends with three columns user1, user2, pending.

user1 and user2 are primary keys of datatype int.

The phpmyadmin returns this error:

MySQL said: #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 var int; Set var = SELECT COUNT(*) FROM friends WHERE ((user1 = id1 A' at line 1

note - i did it via phphmyadmin so i have This is my code:

Declare var int; 
Set var = SELECT COUNT(*) FROM friends WHERE ((user1 = id1 AND user2=id2) OR (user1 = id2 AND user2=id1));
IF var = 0                                 
 BEGIN
    INSERT INTO friends
                ( user1, user2,pending)
         VALUES (id1, id2,1);
    Print 'Data now added.';
 END
ELSE
 BEGIN
     Print 'Dah! already exists';
 END
4
  • You appear to be mixing SQL Server and MySQL syntax. Commented Feb 20, 2015 at 15:53
  • @gvee I've removed the sql-server tag as the error is MySQL related, but agree that looks like sql-server proc. benddror can you please clarify. Commented Feb 20, 2015 at 15:56
  • im working with phpmyadmin means MySql if thats what you ment by clarify. Commented Feb 20, 2015 at 16:07
  • so, somebody can please write me how it suppsed to be written in the syntax of MySql Commented Feb 20, 2015 at 16:19

2 Answers 2

2

Try to invert those 2 lines :

...
declare @var int
AS
...

As follow :

...
AS
declare @var int
...
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your answer, but it still give me the same error
As mentionned by gvee and Tanner in comments of your question, it seems your trying to use MSSQL syntax within MySQL. With my answer your procedure compiles successfully in MSSQL. So there's probably some misunderstanding somewhere.
so, somebody can please write me how it suppsed to be written in the syntax of MySql
0

1) Everything (including declarations) must be in a BEGIN ... END block.

2) You don't need to (and cannot) declare @session_veriables, only local_variables.

3) Missing parameters. They must be declared between parantheses: proc_name(p1 INT, p2 INT)

4) All statements must terminate with ;

5) The whole procedure should be wrapped inside:

DELIMITER ||
...
||
DELIMITER ;

unless you send it via phpMyAdmin.

3 Comments

Thank you for your answer, i did evrey thing you said and lokked a little bit more in google but didnt find why its still not working, i updated my code to the new one, also i did it through phpmyadmin...
1 Does phpmyadmin automatically enclode your code in BEGIN ... END? 2 Some ; are still missing 3 The subquery in the SET statement must be enclosed in paranthesis: SET var = (...);
Also, there is no print function. Use SELECT 'Dah! already exists';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.