Schema trap with SQL 2005

Schema trap with SQL 2005

by Peter Hancock 24. November 2007 02:42

It's been awhile since I played around with Oracle, and for me, the introduction of schemas in SQL Server 2005 really went unnoticed.  Well, today it came up and bit me in the backside pretty savagely.  It's quite obvious, (like everything) but the following code created a few issues for me...

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = "Address")
DROP TABLE Address

The issue? If any table in any schema has the name Address, then it will drop my table. The fix is pretty easy...

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = "Address" AND TABLE_SCHEMA="DBO")
DROP TABLE dbo.Address

As I said - obvious. Embarassed

Currently rated 4.0 by 3 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Software development

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

March 12. 2010 07:28

Recent posts

Recent comments