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;
Related
I need to generate a string like this in stored procedure.
select case when Sex like '%' then 'Person' end as Sex from tableName;
In stored Procedure I have generated like this.
select case when Sex like quote(%) then quote(Person) end as Sex from tableName;
The error i got is
Error Code: 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 '%) then quote(Person) end as Sex from tableName' at line 1 0.000 sec
My MariaDB version is '10.3.16-MariaDB'
Please help in resolving this issue.
You didn't quote the literals in the second query. Try:
SELECT CASE
WHEN sex LIKE quote('%') THEN
quote('Person')
END AS sex
FROM tablename;
But it doesn't make too much sense to quote() literals as you already have to escape any special characters in them. So applying quote() is superfluous.
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.
For example. I have a database upgrade script for adding a column to a database table. It looks a little like this:
IF NOT Exists(SELECT * FROM SysColumns sc, SysObjects so
WHERE sc.Name = 'dealer_number'
AND so.Name = 'collector'
AND so.Type= 'U'
AND so.id = sc.id)
BEGIN
-- SQL for creating column
END
ELSE
BEGIN
-- notify user that column already exists
END
How do I notify the user that the column already exists?
RAISERROR ('column already exists',0,1) with nowait
or
print 'column already exists'
you can use PRINT statement in SQL
RAISERROR seems appropriate here. See here.
Use PRINT - it works from most SQL client applications. SELECT also works
e.g
PRINT 'column already exists or something'
or
SELECT 'column already exists or something'
How could I want to add a column if not exist.
I tried this code:
IF EXISTS( SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'forwind.measuringdata_fino_10_00000000'
AND table_schema = 'forwind'
AND column_name != 'F1_USA(40)_u') THEN
ALTER TABLE `forwind.measuringdata_fino_10_00000000` ADD `F1_USA(40)_u` FLOAT NOT NULL default '0';
END IF;
but I get the following 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( SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
Good for me if somebody know an other solution!
MySQL does not support anonymous IF statements like that.
You have at least 3 options to address this:
1) Just run the ALTER TABLE statement and ignore the Duplicate column name error if the column already exists. If you're executing a bunch of DDL in a script you can use mysql -f to ignore the errors and keep going.
2) Use a scripting language such as bash, python, perl etc to check for the existence of the column and then add it if it does not already exist.
3) Create a stored procedure in MySQL to check for the existence of the column and then add it if it does not already exist.
P.S. As an aside, I recommend against putting parentheses in column names, because that forces you to quote the column name every time you reference it.
------Add an IpAddress column in ActionsTracking table if -----
DECLARE #tableName varchar(MAX) ='ActionsTracking'
DECLARE #columnName varchar(MAX) ='IpAddress'
IF EXISTS(SELECT 1 FROM sys.columns
WHERE Name = #columnName
AND Object_ID = Object_ID(#tableName))
PRINT 'Column Already Exists'
ELSE
BEGIN
EXEC('ALTER TABLE '+#tableName+'
ADD '+ #columnName +' varchar(15) null')
PRINT 'Column Added Sucessfully'
END
I'm getting that error running on MySQL 5.5.8
Mysql2::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 'WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?