Simple IF.. Else.. mysql Query - mysql

set #var=1;
if #var>1 then
select * from client;
else
select * from otherTable;
end if;
This is my mysql query. Can you guys point out why is it showing error?
This is really eating my brains out.
The Error displayed is
IF #var >1 THEN SELECT *
FROM client;
MySQL said: Documentation
#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 'if #var>1 then
select * from client' at line 1
Fast answer would be really helpful.

In MySQL you have to execute that in a trigger or procedure.
You can't just run script code without a function around it.
delimiter |
CREATE PROCEDURE simpleproc ()
BEGIN
set #var=1;
if #var>1 then
select * from client;
else
select * from otherTable;
end if;
END;
|
delimiter ;
After that you can execute it with
call simpleproc()

If both tables have the same columns, you could use a UNION query, like this:
SELECT * FROM Client WHERE #var>1
UNION ALL
SELECT * FROM otherTable WHERE #var<=1
Please see an example here.

Related

MySQL | MariaDB Creates a #1064 syntax error on import [duplicate]

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am 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 '' at line 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

How to call a stored procedure using select statement in mysql

I have call statement like
CALL report_procedure
('2013-02-01',now(),'2015-01-01','1');
and i want to use it in a select query.
i have tried like
Select * from ( CALL report_procedure
('2013-02-01',now(),'2015-01-01','1'));
but error occurs.
like
Error Code: 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 ( CALL report_procedure
('2013-02-01',now(),'2015-01-01','1') at line 3 0.297 sec
Can anyone suggest me a method to call stored procedure in Select statement in mysql??
It is not possible to use result set from procedure in FROM clause. MySQL does not allow doing this.
You may populate another table (or temporary table) in your procedure, and after, use that table in SELECT commands -
CALL report_procedure ('2013-02-01',now(),'2015-01-01','1'); -- fill temp_table
SELECT * FROM temp_table;
--Firstly your store procedure should look something like this:
CREATE PROCEDURE report_procedure(
IN d1 DATE,
dnow DATE,
d2 DATE,
val INT
)
BEGIN SELECT *
FROM yourtablename
WHERE date1 = d1
AND datenow > dnow
AND date2 > d2
AND value = val;
END
--The store procedure contains the select statement.
-- then you can call the store procedure like that:
CALL report_procedure('2013-02-01',now(),'2015-01-01','1');
--hope it helps

MySQL CREATE PROCEDURE syntax error

I've tried every possible combination I can think of to resolve this error but it keeps happening. Any help appreciated. This is just modifying the sakila sample database to do more complex things with.
See towards bottom I labeled the error with -- HERE!.
USE sakila;
DROP PROCEDURE IF EXISTS sp_randCustMult;
DELIMITER //
CREATE PROCEDURE sp_randCustMult()
BEGIN
/* section of code left out for troubleshooting
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = customer AND column_name = multiplier)
THEN ALTER TABLE customer DROP COLUMN multiplier;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = customer AND column_name = cust_ranking)
THEN ALTER TABLE customer DROP COLUMN cust_ranking;
END IF;
*/
-- add new columns
ALTER TABLE customer
ADD COLUMN multiplier DECIMAL(3,2) AFTER active;
/* this column not relevant now
ALTER TABLE customer
ADD COLUMN cust_ranking VARCHAR(10) AFTER multiplier;
*/
-- declare a counter
SET #start = (SELECT MIN(customer_id) FROM customer);
SET #stop = (SELECT MAX(customer_id) FROM customer);
-- start while loop
WHILE #start <= #stop
DO
UPDATE customer
-- insert multiplier based on random distribution
SET multiplier =
(SELECT
(CASE
WHEN RAND() <= 0.65 THEN 1.00
WHEN RAND() <= 0.90 THEN 0.85
WHEN RAND() <= 1.00 THEN 1.05
END)
)
WHERE customer_id = #start;
-- tick counter one up
SET #start = #start + 1;
END WHILE;
-- HERE! syntax error on END before //
END//
DROP PROCEDURE sp_randCustMult//
DELIMITER ;
EDIT1: To clarify, MySql version is:
MySQL Workbench Community (GPL) for Mac OS X version 6.1.4 revision 11773 build 1454
And the error response from Workbench:
Error Code: 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 'END' at line 35
EDIT2: Edited code as suggested. Error no longer happens, however data is not being updated at all. (all NULL in new column)
Your CREATE PROCEDURE doesn't match the required syntax as described in the MySQL manual (simplified below by including just the relevant parts):
CREATE
    PROCEDURE sp_name ([proc_parameter[,...]])
    routine_body
routine_body:
    Valid SQL routine statement
The routine_body consists of a valid SQL routine statement. This can be a simple statement such as SELECT or INSERT, or a compound statement written using BEGIN and END. Compound statements can contain declarations, loops, and other control structure statements. The syntax for these statements is described in Section 13.6, “MySQL Compound-Statement Syntax”.
Therefore, this junk…
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = customer AND column_name = multiplier)
THEN ALTER TABLE customer DROP COLUMN multiplier;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = customer AND column_name = cust_ranking)
THEN ALTER TABLE customer DROP COLUMN cust_ranking;
END IF;
… is illegal. Perhaps you meant to move it into the BEGIN … END compound statement?
You also need a semicolon after END WHILE.
All functional logic must be between the tags BEGIN and END
So the IF conditions and alter query stuff must lie between BEGIN and END tags of procedure..
Thanks
I figured out the issues, here is the solution on CR:
https://codereview.stackexchange.com/questions/51603/mysql-modifying-sakila-database

MySQL server stored procedure syntax error

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am 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 '' at line 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

MySQL if statement fails

I have a function called tableExists. It can be used to check for the existence of a table. I want to use it in a DB upgrade script. I can use the function like this:
select myDb.tableExists('myDb', 'someTable') as cnt into #exists;
And see the results like this:
mysql> select #exists;
+---------+
| #exists |
+---------+
| 1 |
+---------+
Now, I want to use it in an If statement, followed by a create table statement. But, I am having problems with the if. The following is what I am trying to test with:
mysql> IF (#exists = 1) THEN
-> select 'exists'
-> END IF;
ERROR 1064 (42000): 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 'IF (#exists = 1) THEN
select 'exists'
END IF' at line 1
What am I missing here? This should be simple.
You can only use the IF inside a stored procedure.
The valid select statement would be:
SELECT CASE (#exists) WHEN 1 THEN 'exists' END as DoesItExist
If you use the case as a stand alone element in a stored proc, you'll need to end it with end case how ever.
Why don't you just use IF NOT EXISTS in the CREATE TABLE query and save yourself all this trouble:
CREATE TABLE new_table IF NOT EXISTS
... {table definition}
If the table already exists, nothing will happen.