xxxxxxxxxx
The syntax for the IF EXISTS clause (SQL Server 2019 and later)
# Method 1
USE master;
DROP DATABASE IF EXISTS TestDB;
Test if a database exists before deleting it
# Method 2
USE master;
IF DB_ID('TestDB') IS NOT NULL
DROP DATABASE TestDB;
CREATE DATABASE TestDB;
Another way to test for the existence of a table
# Method 3
IF EXISTS (SELECT * FROM sys.tables
WHERE name = 'InvoiceCopy')
DROP TABLE InvoiceCopy;