MYSQL syntax delimiter and syntax error - mysql

I have tried to fix the syntax errors in the following but I can't see what on earth is wrong here:
DELIMITER =
CREATE TRIGGER trigs BEFORE UPDATE ON autoinc
FOR EACH ROW BEGIN
DECLARE num_rows INTEGER;
SELECT (*) INTO num_rows FROM autoinc;
IF num_rows >=3 THEN
DELETE FROM autoinc LIMIT 1;
END IF;
END=
DELIMITER ;
The errors are:
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 '*) INTO num_rows FROM autoinc; IF num_rows >' at line 4
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 '3 THEN
Can any please help me fix this?

As already mentioned in the comments:
SELECT (*) AS num_rows ...
was probably meant to be
SELECT COUNT(*) AS num_rows ...
And the
IF num_rows >=3 THEN
breaks as you defined = as delimiter.
Use a delimiter that doesn't occur in your code, e.g.:
DELIMITER //
With these two changes things should work without syntax errors

Related

How to print the tables' name which don't contain a ID or don't contain the id column?

I am new to MySQL. Now, I have 40 tables. A part of they contains a field named "case_id" (char). I want to find which tables have the "case_id" column but don't have case_id = "123".
I just select all tables having the "case_id" column but I don't know how to find the required table from it.
Do you have any suggestions? Thank you so much!
select DISTINCT TABLE_NAME as test_table from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME IN ("case_id") and TABLE_SCHEMA="test_db";
For example,
Table A has a record with "case_id" = "123". so, it would not be printed.
Table B doesn't contain the column "case_id, it would not be printed either.
Table C has "case_id" but there isn't a record with "case_id" = "123", the name "Table C" would be printed.
EDIT #2
Just update my code based on #abk 's answer. I just changed the name of the scheme.
CREATE DEFINER=`root`#`localhost` PROCEDURE `MISS_CASE_CHECK`()
BEGIN
DECLARE Table_name TEXT;
DECLARE done INT DEFAULT 0;
DECLARE str LONGTEXT;
DECLARE my_cursor CURSOR FOR
SELECT DISTINCT TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ("case_id") and TABLE_SCHEMA="wes_bk";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN my_cursor;
SET str = '';
WHILE done = 0 DO
BEGIN
FETCH my_cursor INTO Table_name;
SET str = CONCAT(str,Table_name,',');
END;
END WHILE;
SELECT LEFT(str, LENGTH(str) - 1);
CLOSE my_cursor;
SELECT str;
END
Unfortunately, I received many error messages:
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 '' at line 3
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 'DECLARE done INT DEFAULT 0' at line 1
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 'DECLARE str LONGTEXT' at line 1
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 'DECLARE my_cursor CURSOR FOR
SELECT DISTINCT TABLE_NAME
From INFORMATION' at line 1
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 'DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1' at line 1
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 'OPEN my_cursor' at line 1
ERROR 1193 (HY000): Unknown system variable 'str'
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 'WHILE done = 0 DO
BEGIN
FETCH my_cursor INTO Table_name' at line 1
ERROR 1193 (HY000): Unknown system variable 'str'
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 'END' at line 1
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 'END WHILE' at line 1
ERROR 1054 (42S22): Unknown column 'str' in 'field list'
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 'CLOSE my_cursor' at line 1
ERROR 1054 (42S22): Unknown column 'str' in 'field list'
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 'END' at line 1
My mysql version is 5.7.12.
Any suggestions?
The stored procedure must look more like this.
The table_name will be put o a text variable and you see the result as comma-separated text.
If you need a table with rows, you must add a temporary table and select this at the end.
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `MISS_CASE_CHECK`()
BEGIN
DECLARE Tablename TEXT;
DECLARE done INT DEFAULT 0;
DECLARE str LONGTEXT;
DECLARE my_cursor CURSOR FOR
SELECT DISTINCT TABLE_NAME
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME ='Id' and TABLE_SCHEMA='testdb';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET #tablen = '';
OPEN my_cursor;
SET str = '';
WHILE done = 0 DO
BEGIN
FETCH my_cursor INTO Tablename;
SET str = CONCAT(str,Tablename,',');
END;
END WHILE;
SELECT LEFT(str, LENGTH(str) - 1);
CLOSE my_cursor;
END//
DELIMITER ;
You won't be able to do this in a single step (unless there is some super backwards creative way). Instead you will do your existing query to get a list of tables that are candidates for your second criteria. Then you will issue a single SQL statement for each table in that list to test if it has your value.
You can speed this up by having your first SQL statement write your other SQL statements as one big UNION QUERY that you can then execute as a second step.
Something like:
select DISTINCT
CONCAT('SELECT DISTINCT ', TABLE_NAME, ' AS table_name FROM ', TABLE_NAME, ' WHERE NOT EXISTS (SELECT 1 FROM ', TABLE_NAME, ' WHERE case_id=123) UNION ALL ') as sqlstatement
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME IN ("case_id") and TABLE_SCHEMA="test_db";
Just take that output, trim off the last UNION ALL and submit it (I haven't tested, but theoretically this should do the job)
If you will be doing this often, then this should be moved to a stored procedure or some other scripting language where you loop through your candidates (from your original SQL) and then issue the sql to query that table to see if it has that case_id and add it to a new table/list/array/something for output.

Query to return a table generates SQL ERROR (1064): Error in syntax

I'm trying to create a function, but keep getting the error message that
SQL ERROR (1064): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near "at line 3.
DELIMITER $$
DROP FUNCTION IF EXISTS mysql.HelpMePlz$$
CREATE FUNCTION mysql.HelpMePlz(input VARCHAR(255) ) RETURNS VARCHAR(255) BEGIN
DECLARE result VARCHAR(255);
SELECT name into result
FROM tb_company
WHERE company_info = input
LIMIT 3
RETURN result;
END $$
DELIMITER ;

SQL trigger giving syntax error in MySQL

DROP TRIGGER IF EXISTS demo_inc_when_viewstatus_one ;
DELIMITER $$
CREATE TRIGGER demo_inc_when_viewstatus_one AFTER UPDATE ON `tbl_ffa_demo`
FOR EACH ROW BEGIN
UPDATE `tbl_stats` SET open_demos=CASE WHEN NEW.demo_status=1 THEN
open_demos+1 ,
total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NEW.view_status=1
END $$
DELIMITER;
The error message I received was:
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 ' total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NE' at line 4
You did not follow the syntax for the case statement:
case when <condition> then <true branch> else <false branch> end
So, you should have something like:
... SET open_demos=CASE WHEN NEW.demo_status=1
THEN open_demos+1 ELSE open_demos=open_demos END, ...

Syntax errors in mysql with procedure, triggers and signal

I feel embarrassed that I have to come and ask for help with this, but as surely many before me have learned, it seems mySQL syntax error messages are about as useful as a pope hat on a grizzly bear. Attached is my first attempt at writing a trigger for a car company database. The table, can_lease, relates the id of an employee and the id of a car model. The trigger is expected to enforce two rules: 1) there can be at most 10 car models associated with 1 employee, and 2) the employee must be of type leasing (there is a column 'leasing' which must equal 'Y').
So the goal is for the trigger to catch violations of this rule and send a signal and a message explaining the violation. I'm simply not sure what the errors are, but I will attach the relevant error messages as well.
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end;
delimiter $$
create trigger can_lease_insert_trigger
after insert on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
create trigger can_lease_update_trigger
after update on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
And here are the error messages:
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 '' at line 3
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 ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then' at line 1
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 'end' at line 1
Thank you for your help! I would also appreciate any advice one has for debugging this sort of thing in general. Coming from gcc telling me at least something about why my code is wrong, this is a very foreign process!
EDIT: I realize that I should have moved the delimiter change up to above the procedure as well. I don't get it, but that removes all but one of the errors. Currently, the error is
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 (select count(rent_model_id) from can_lease where emp_id = eid) == 10
then s' at line 4
The semicolons (;) between the first begin and end keywords are the culprits. Just enclose your original create block with a DELIMITER, as follows. I use # as the delimiter in my example, and I recognize that you use $$, though there will be no resulting difference.
DELIMITER #
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end#
Also, there will be no difference if you end it like I did (end#), or like you did, with the semicolon after the end keyword:
end;
#

Can any one suggest me how to resolve this problem: Error Code : 1064 in MY SQL 5.5 ver

DELIMITER $$;
DROP FUNCTION IF EXISTS tonumeric $$;
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER; $$
When I executed this function, I am facing this 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 'IF EXISTS tonumeric' at line 1
(0 ms taken)
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 ';
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num' at line 1
(0 ms taken)
Thanks
How about this:
DELIMITER $$
DROP FUNCTION IF EXISTS tonumeric $$
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER ;
Delimiter is a special command, in that you shouldn't terminate it with a ; -- you're actually setting the delimiter to "$$;", not "$$".