Delete row if table exists SQL

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.


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_NAME]') AND type in (N'U'))
链接地址: http://www.djcxy.com/p/94446.html

上一篇: 检查数据库是否为空

下一篇: 如果表存在SQL,则删除行