This query generates an error because table2 doesn't exist:
Select * FROM table WHERE table2.id IS NOT NULL
Is there anything like this for check the table2 before apply the check on the id?
Select * FROM table WHERE (EXIST(table2) AND table2.id IS NOT NULL) or not EXIST(table2)
You need to query this system table:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'yourdatabasename'
AND table_name = 'table2';
If a row is returned, then your table exists.
I do not believe there is any command or function in standard SQL to do this. You could query the data dictionary to check if the table exists before issuing your SQL query as follows:
SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name = 'xxx';
I do not think it could be done in a single SQL statement though.
Related
I am trying to delete rows if new_id column value is equal to 2 from multiple mysql tables, but I don't know if all those tables have column new_id.
I try the following statement, but it gives a syntax error:
DELETE FROM table_name WHERE new_id =2 IF EXISTS new_id int(11)
How to do this?
you can get column name by using below query
SHOW COLUMNS FROM `table_name` LIKE 'new_id';
Then from frontend you can take the decision to execute delete query
You can check in information schema:
IF EXISTS ( SELECT *
FROM information_schema.columns
WHERE table_name = 'table_name'
AND column_name = 'new_id'
AND table_schema = DATABASE () ) THEN
DELETE FROM table_name WHERE new_id = 2;
END IF;
Could not validate table exists in MySQL.
Tried below query but no output.
if !(select * from information_schema.tables where table_name='sy_code')
To see the schema of a table, use query
DESCRIBE table_name;
To see all the existing table in our database, use query
SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_db'
I know how to use queries but I've never had to use one for this particular tasks .. I know about the SHOW TABLES; command .. How can I write a query to check if a particular table exists in a MYSQL database .. For example , a query that checks if table MEMBERS exists in database called USERS ??
You can use INFORMATION_SCHEMA.TABLES
USE USERS;
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'MEMBERS'
Searching Table:
select * from
information_schema.tables
where table_name like '%MEMBERS%'
Searching Column:
select * from
information_schema.columns
where table_name like '%COLUMN%'
I have a script that drops a load of tables using DROP TABLE IF EXISTS, this works.
There is also a delete in this script to DELETE a row from another table that I do not manage. This table may or may not exist.Is there any to check the table exists before attempting to delete a row?
this needs to work for MYSQL and SQLServer
thanks
Alex
To check in SQL SERVER,
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'TheSchema' AND TABLE_NAME = 'TheTable'))
BEGIN
--Do Stuff
END
To check in mysql:
You simply count:
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = '[database name]'
AND table_name = '[table name]';
This one deletes the row and does not complain if it can't.
DELETE IGNORE FROM table WHERE id=1
source here.
For SQL Server: You could use:
IF OBJECT_ID('tablename','U') IS NOT NULL
I dont think you'll find a common syntax between SQL server and my SQL. I mean, you can check if the table exsits on SQL Server using something like:
if exists(select * from sys.objects where name like 'table_name')
but mySql would have its own catalog.
Unless you write a script like:
if (sql_server) then
if exists(select * from sys.objects where name like 'table_name')
else --mySQl
--execute the mysql script
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_NAME]') AND type in (N'U'))
It seems to me right the first item in the "Related" column on the right side answers your question.... Check if table exists in SQL Server
For MySQL
show tables like "test1";
For SQL Server
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'testSchema' AND TABLE_NAME = 'test1'
A question you want to ask yourself (in terms of database design): Why are you trying to delete rows from a table you are not sure exists? If it doesn't, but you expect it does, wouldn't you rather create the table than not delete it?
Anyway, Chris Gesslers answer does exactly what you are asking in SQL Server, but there is some smell here.
The construct in MySQL you can use is
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'databasename'
AND table_name = 'tablename'
and check for results
you can use bellow code:
DECLARE #TABLENAME VARCHAR(20)='TableName';
IF (OBJECT_ID(#TABLENAME) IS NOT NULL )
BEGIN
execute(N'TRUNCATE TABLE ' + #TABLENAME + '' );
END
ELSE
BEGIN
PRINT 'Table NOT Exists'
END
Hello I have a dynamic query like
SET #template = 'SELECT x AS X,... INTO temporalTable FROM' + #table_name
Then I execute it
EXEC (#template)
How do I validate if temporalTable
already exists, if so, drop it?
Just use OBJECT_ID
IF OBJECT_ID('temporalTable') IS NOT NULL
DROP TABLE temporalTable
No need to query any tables or do any aggregations.
Use information schema or sp_help function.
I would prefer information schema since it's SQL ANSI and you can port the code to other databases:
select count(1)
from information_schema.tables
where table_name = 'temporalTable';
sys.tables is a SQLServer specific option similar to inforamtion schema that you can also explore.
IF EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='tablename')
SELECT 'tablename exists.'
ELSE
SELECT 'tablename does not exist.'