15

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

Alex Edwards
  • 1,613
  • 3
  • 24
  • 48

9 Answers9

14

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]';
7

This one deletes the row and does not complain if it can't.

DELETE IGNORE FROM table WHERE id=1

source here.

Community
  • 1
  • 1
  • 3
    At least for MySQL this does not actually work at all on a table that doesn't exist, as commented on the very link provided. – jlh Nov 23 '18 at 09:30
3

For SQL Server: You could use:

IF OBJECT_ID('tablename','U') IS NOT NULL
Darren
  • 68,902
  • 24
  • 138
  • 144
3

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
Diego
  • 34,802
  • 21
  • 91
  • 134
2
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_NAME]') AND type in (N'U'))
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
1

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

Community
  • 1
  • 1
MrTJ
  • 13,064
  • 4
  • 41
  • 63
1

For MySQL

show tables like "test1";

For SQL Server

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'testSchema' AND     TABLE_NAME = 'test1'
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
1

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

Martijn
  • 11,964
  • 12
  • 50
  • 96
0

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
Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34