How can we write a SQL statement to drop a table. The drop statement should not generate any error if the table doesn't exists. I have achieved the same in Oracle by catching the 'table not found exception'
The code I wrote is as follows:
CREATE PROCEDURE drop_table(pTableName VARCHAR2)
vCommand := 'DROP TABLE ' || pTableName || ' CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE vCommand;
WHEN table_not_exist THEN
WHEN OTHERS THEN
Can I do something equivalent in MSSQL?
here is another way in T-SQL that I usually use, because the method abbe supposed I ran into some problem with when creating triggers. The exists returned false even though I could see that the trigger existed.
When using this method I always got the correct result.
object_id(N'['tableowner].['tablename']'),N'IsTable') = 1
DROP TABLE ['tableowner].['tablename']
The OBJECTPROPERTY routine tells you information about an object. in this case if it is a table it will return 1, if not then 0 and NULL if it is not an object.