This question already has answers here:
A table name as a variable (10 answers)
Closed 5 days ago.
DECLARE @tablename VARCHAR(100) = 'House';
IF OBJECT_ID(@tablename, N'U') IS NOT NULL
BEGIN
IF EXISTS(SELECT 1 FROM @tablename)
BEGIN
PRINT 'Table already exists and has data, not dropping it.'
END
ELSE
BEGIN
DROP TABLE @tablename
PRINT 'Table dropped successfully.'
END
END
ELSE
BEGIN
PRINT 'Table does not exist.'
END
Please correct it for SQL Server
1条答案
按热度按时间hs1ihplo1#
You can try this:
Also, it would be better to pass and the schema of the table.