Delete row if table exists SQL

后端 未结 9 893
旧时难觅i
旧时难觅i 2021-01-17 10:36

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 d

相关标签:
9条回答
  • 2021-01-17 11:09

    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]';
    
    0 讨论(0)
  • 2021-01-17 11:15
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_NAME]') AND type in (N'U'))
    
    0 讨论(0)
  • 2021-01-17 11:17

    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
    
    0 讨论(0)
提交回复
热议问题