It is recommended to use TRUNCATE TABLE
instead of DELETE
. However, truncate table does not support the IF EXISTS
clause. The alternative is to DROP TABLE
and recreate but needs DDL. Is there a way to do TRUNCATE TABLE
if only the table exists?
You have two options to achieve it:
SQL Procedure/Script
Using IF
condition, checking if table exists, then only truncate your table.
With Plain SQL Statements
Use Create
table with if not exists
in combination with Truncate
, this will ensure table always exists & your consecutive SQL statements don't error out & stop.
CREATE TABLE @tobetruncated IF NOT EXISTS
TRUNCATE TABLE @tobetruncated
NOTE: This is not specific to REDSHFIT, mostly applies to all DB unless it supports special functions (like one I know Oracle has
TABLE_EXISTS_ACTION
). Truncate is like a all or nothing operation, and thats what makes it much better in performance than DELETE.