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.
Related
It's my first time that I try to use trigger on mysql (and generally I don't use mysql so much).
The version of mysql that I'm using is 5.5.40 and the code I'm using to create the trigger is:
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END;
Where field1 and field2 are two fields in both tables (tab1 and tab2) both archer(120).
When I try to execute this code I receive an 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 7
(line 7 is near "WHEN" keyword).
I checked several answer on stack overflow and other web sites, I tried with and without delimiters, and I still not able to create a trigger.
What's my syntax error?
Is there some way to have some more accurate mysql debugging? Actually mysql answer (like I just posted) is just "there is an error (1064)" but it doesn't let me know what is wrong.
you have to change the delimiter to use it in your sql statement :
DELIMITER //
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END//
DELIMITER ;
I have a stored procedure that works:
call my_procedure('A,B,C,D');
I want to populate the A,B,C with a list from a subquery of another table, eg:
call my_procedure(
SELECT group_concat(letters) FROM table WHERE type = 'some_type')
);
Possible? Or am I doing it wrong?
SELECT my_function(group_concat(letters)) FROM table WHERE type = 'some_type';
You can assign the value to a user-defined variable and then pass that variable to the function.
For example:
SELECT group_concat(letters)
INTO #foo
FROM table
WHERE type = 'some_type';
call my_function(#foo);
Yes, you can pass a string to a procedure that's returned as the result of subquery.
But not as a bare query:
mysql> create procedure foo (in s text) begin select s; end !!
mysql> call foo( select group_concat(user) from mysql.user );
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
'select group_concat(user) from mysql.user )' at line 1
If you enclose the query in parentheses, it counts as a scalar subquery, that is, a subquery that is bound to return one row, one column:
mysql> call foo( (select group_concat(user) from mysql.user) );
+--------------------+
| s |
+--------------------+
| root,root,bill,xbm |
+--------------------+
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.
I've been trying to write this simple mysql statement to check if a table exists:
IF object_id('carpool'.'users', 'U') is not null THEN
PRINT 'Present!'
ELSE
PRINT 'Not accounted for'
END IF
'carpool' is my schema and 'users' is a table
My sql version is 5.1.52-community
I keep getting 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' at line 1 0.000 sec
I've tried several syntaxes such as IF BEGIN END ELSE BEGIN END to no avail.
any ideas?
It looks like you're trying to do a form of T-SQL, which MySQL does not support.
There is no print function, nor object_id. There is a form of IF function that can be used in a SELECT statement:
SELECT IF(1 = 1, 'present', 'not');
Are you reading MSSQL doco instead of MySQL doco? MSSQL supports these functions.
Depending on what you are after, you might want to use something like this:
SELECT
CASE WHEN EXISTS (SELECT NULL
FROM information_schema.tables
WHERE table_schema = 'carpool' AND table_name = 'users')
THEN 'Present'
ELSE 'Not accounted for' END;
This will check if table users exists in carpool schema. Please see example here.
Every statement must be terminated by semicolon.
Like this
IF object_id('carpool'.'users', 'U') is not null THEN
PRINT 'Present!';
ELSE
PRINT 'Not accounted for';
END IF;
I setup a database and one of the columns in a table is "status" (active,inactive,locked). I want the event to compare NOW() to the value of column "pdate" (which is a timestamp), and if greater than 30 days, update the value of "status" to "inactive".
I wrote the following, but I get a few syntax errors :s
CREATE EVENT `expireAccounts_oldPwd` ON SCHEDULE EVERY DAY
DO
USE databasename;
SELECT pdate FROM tablename WHERE status = "active";
FOR EACH ( ROW IN tablename WHERE( ( SELECT DATEDIFF(NOW(),pdate) AS age ) > 30 ) ) {
UPDATE tablename SET status = "inactive";
};
ERROR 1064 (42000): You have an error in your SQL syntax near 'USE databasename' at line 2
ERROR 1064 (42000): You have an error in your SQL syntax near 'FOR EACH ROW IN "tablename" WHERE( ( SELECT DATEDIFF(NOW(),"pdate") AS age )' at line 4
ERROR 1064 (42000): You have an error in your SQL syntax near '}' at line 6
of course 'databasename' was replaced the actual database's name and 'tablename' the actual table's name.
Now at least it's doing something:
+---------------------+
| pdate |
+---------------------+
| 2011-08-11 18:01:02 |
| 2011-08-11 18:03:31 |
+---------------------+
2 rows in set (0.00 sec)
If I don't included USE databasename; on line 2, I get no output.
FINAL CODE:
USE databasename;
DELIMITER %
CREATE EVENT eventname
ON SCHEDULE EVERY 1 DAY
DO UPDATE tablename SET status = "inactive" WHERE status = "inactive" AND DATEDIFF(NOW(), columnname) > 30);
%
I didn't realize events were database-specific (so you have to be in the database when you create it).
Thanks all!
Two event specific things apart from the obvious syntax problems:
Where does your event end? You need to enclose it in a BEGIN/END block (the same way as a stored procedure).
You need to switch the DELIMITER when defining an event (the same way as when you define a stored procedure).
There are two relevant examples at the end of http://dev.mysql.com/doc/refman/5.1/en/create-event.html.
Update:
Also check http://dev.mysql.com/doc/refman/5.1/en/stored-routines-syntax.html for SQL statements which are permitted in stored procedures and events. USE is not permitted.
One more update:
It would be advisable to first try to get your SQL working without putting it in a event. After you have fixed your statements so that they work, try creating a stored procedure out of it. When you get the stored procedure working, you can replace it with an event. This way it will be much easier to sort out the rest of the problems (such as where is the output of the first SELECT supposed to go? etc.).
Is there a reason you can't use a query like this one?
UPDATE tablename SET status = "inactive" WHERE status = "active" AND DATEDIFF(NOW(),pdate) > 30;
I've never even seen FOR EACH in MySQL...
First step's going to be using valid SQL.
SELECT "pdate" FROM databasename.tablename WHERE "status" = "active";
will always cause an error, because column names shouldn't be in quotes. If you enclose a table name in anything, it'd be backticks (`).
SELECT pdate FROM databasename.tablename WHERE status="active";
You have the same problem in the rest of your query.