Sometimes you need to truncate table. But sometimes truncate fails, when there is no table with such name.
Solution: just use “if exists”.
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'@{item().TableName}' and TABLE_SCHEMA = N'@{item().SchemaName}')
begin
truncate table @{item().SchemaName}.@{item().TableName}
end
You can use this in many situations when you need to verify the existence of an object.
DROP TABLE IF EXISTS @{item().SchemaName}.@{item().TableName}
DROP TRIGGER IF EXISTS trigger_name
ALTER TABLE DROP COLUMN IF EXISTS
ALTER TABLE DROP CONSTRAINT IF EXISTS