What is the optimal SQL Server method to check if a table exists? Comparing two methods: one queries sys.tables while the other uses the OBJECT_ID function.
-- Method 1:
IF EXISTS (SELECT TOP 1 * FROM sys.tables WHERE name = 'tableSample')
PRINT 'Table found';
ELSE
PRINT 'Table missing';
-- Method 2:
DECLARE @tblID INT = OBJECT_ID('tableSample', 'U');
IF @tblID IS NOT NULL
PRINT 'Table exists';
ELSE
PRINT 'No such table';