Check if table exists in SQL Server

前端 未结 28 1561
梦如初夏
梦如初夏 2020-11-22 04:23

I would like this to be the ultimate discussion on how to check if a table exists in SQL Server 2000/2005 using SQL Statements.

When you Google for the answer, you g

28条回答
  •  后悔当初
    2020-11-22 04:52


    -- -- create procedure to check if a table exists


    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `checkIfTableExists`;
    
    CREATE PROCEDURE checkIfTableExists(
        IN databaseName CHAR(255),
        IN tableName CHAR(255),
        OUT boolExistsOrNot CHAR(40)
    )
    
      BEGIN
          SELECT count(*) INTO boolExistsOrNot FROM information_schema.TABLES
          WHERE (TABLE_SCHEMA = databaseName)
          AND (TABLE_NAME = tableName);
      END $$
    
    DELIMITER ;
    

    -- -- how to use : check if table migrations exists


     CALL checkIfTableExists('muDbName', 'migrations', @output);
    

提交回复
热议问题