Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I fix this IF statement error in sql

I got into a problem with the SQL IF statement. And I tried a few another statement, but it doesn’t work too.

I got my code here:

DELIMITER $
BEGIN NOT ATOMIC
    IF user = 'test' THEN 
       SELECT fox FROM conv WHERE user = test;
    ELSE
       INSERT INTO pending_conv (pending) VALUES ('test');
    END IF;
END $
DELIMITER ;

And the error message:
#1327 - Undeclared variable: user

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know I’m bad at this because I’m new to this. I appreciate all your anwser

>Solution :

It isn’t clear what you are attempting to do, but you could pass in a value when calling a stored procedure. Do this by adding an IN parameter to the CREATE PROCEDURE statement and give the parameter a name and data type:

DELIMITER $
CREATE PROCEDURE my_procedure(IN user VARCHAR(255))
BEGIN
    IF user = 'test' THEN 
       SELECT fox FROM conv WHERE user = 'test';
    ELSE
       INSERT INTO pending_conv (pending) VALUES ('test');
    END IF;
END $
DELIMITER ;

Call the stored procedure and pass a value for user like this:

CALL my_procedure('some_user');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading