How can SQL determine if a table exists?

In SQL, you can use the following statement to check if a table exists:

IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'table_name') 
BEGIN
    -- 表存在的处理逻辑
    PRINT '表存在'
END
ELSE
BEGIN
    -- 表不存在的处理逻辑
    PRINT '表不存在'
END

In the statement above, replace table_name with the name of the table to be checked. If the table exists, output “Table exists”; if the table does not exist, output “Table does not exist”.

Leave a Reply 0

Your email address will not be published. Required fields are marked *