I want to select data from one of the tables in the database, on the basis of the parameter.
Here's what I did.
DELIMITER $$
CREATE PROCEDURE `myDB`.`Temp`(
IN ID INT(11)
)
BEGIN
SELECT CASE
WHEN ID IN(1,2) THEN
SELECT * FROM table1;
WHEN ID IN(3,4) THEN
SELECT * FROM table2;
END CASE;
END$$
DELIMITER ;
But. I'm getting error:
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 'SELECT * FROM table1;
WHEN ID IN(3,4) THEN
SELECT * FROM table2;
END' at line 7
You can not use subquery inside case when you can use if else clauses.
IF(ID IN(1,2)) THEN
SELECT * FROM table1;
ELSEIF(ID IN(3,4)) THEN
SELECT * FROM table2;
END IF;
select * from table1 where id in (1,2)
union all
select * from table2 where id in (3,4)
although the column count has to the same across both tables for this to work (and in any case it is not at all bad practice to name the columns in the SELECT to avoid confusion, bahaviour-creep and so on).
Related
I have this query in MySQL database 8.0.12 version
mysql> SELECT tTbl INTO #tTbl FROM t_table WHERE tTbl = "t_contents_1_2021";
SELECT #tTbl;
Query OK, 1 row affected (0.07 sec)
+-------------------+
| #tTbl |
+-------------------+
| t_contents_1_2021 |
+-------------------+
1 row in set (0.07 sec)
Now I need test whether a row exists in a MySQL table or not, using exists condition.
The exists condition can be used with subquery.
It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.
I have tried without success
mysql> SELECT EXISTS(SELECT tTbl INTO #tTbl FROM t_table
WHERE tTbl = "t_contents_1_2021");
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 '(SELECT tTbl INTO #tTbl FROM t_table
WHERE tTbl' at line 1
mysql>
How to do resolve this?
SELECT .. INTO does not return the rowset. EXISTS needs in rowset. So SELECT .. INTO cannot be used in EXISTS.
Remove it:
SELECT EXISTS ( SELECT tTbl
FROM t_table
WHERE tTbl = "t_contents_1_2021" );
If you need both check the row existence and save the value to the variable then use inline assigning:
SELECT EXISTS ( SELECT NULL
FROM t_table
WHERE (#tTbl := tTbl) = "t_contents_1_2021" );
I am trying to insert data in table emp_master where token_no and vehicle_no is available, I am using mentioned below query to add and check the record
insert into emp_master (token_no, vehicle_no) values (1234,12345) where not exists (select * from emp_master where vehicle_no = '12345');
but whenever I am trying this then error comes in as
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 'where not exists (select * from emp_master where vehicle_no = '12345')' at line 1
The suggestion will be helpful
If you need to have this check in your insert statement, you can structure it like this:
SQL Fiddle: http://sqlfiddle.com/#!9/d3dd77/1
insert into emp_master (token_no, vehicle_no)
select 1234, 12345
from dual
where not exists (select * from emp_master where vehicle_no = '12345');
Another way to do this on any DBMS:
DB Fiddle Demo
insert into emp_master (token_no, vehicle_no)
select token_no, vehicle_no
from (
select 1234 token_no, 12345 vehicle_no
) rec
where not exists (select * from emp_master where vehicle_no = rec.vehicle_no);
Put a UNIQUE constraint on the vehicle_no column, and then insert the data with no WHERE clause. Instead, handle the exception if it fails.
I would like to limit the size of a table to X rows (I'll use 5 for example). When the limit is reached, I want to copy the oldest row to another table, then delete it. I currently have:
CREATE TRIGGER LimitRows BEFORE INSERT ON MyTable
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM MyTable) >= 5 THEN
INSERT INTO HistoryTable
SELECT *
FROM MyTable A
WHERE vhID = A.min(vhID);
DELETE FROM MyTable
WHERE vhID = min(vhID);
END IF;
END;
Currently, I get the 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 '' at line 8
How do I write this trigger correctly? Also, how can I modify to cut the table down to 5 rows if it starts out at something like 100 rows?
You need to change the delimiter first
delimiter |
CREATE TRIGGER LimitRows BEFORE INSERT ON MyTable
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM MyTable) >= 5 THEN
INSERT INTO HistoryTable
SELECT *
FROM MyTable A
WHERE vhID = A.min(vhID);
DELETE FROM MyTable
WHERE vhID = min(vhID);
END IF;
END
|
delimiter ;
Otherwise the trigger definition would end at the first ; which would make it incomplete.
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
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.