0

I have a script which has a DROP TABLE command like this:

IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('msdb.dbo.lobloblob') AND type in (N'U'))
DROP TABLE msdb.dbo.lobloblob
CREATE TABLE msdb.dbo.lobloblob
....

this command execute properly. but in script the drop command doesn't execute and propose this error

There is already an object named 'msdb.dbo.lobloblob' in the database

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mohammad Sheykholeslam
  • 1,379
  • 5
  • 18
  • 35

1 Answers1

4

Try

IF OBJECT_ID('msdb.dbo.lobloblob', 'U') IS NOT NULL
    DROP TABLE msdb.dbo.lobloblob;

CREATE TABLE msdb.dbo.lobloblob (...);

instead. I presume you are running this script from the context of a database other than msdb so it won't be in sys.objects

Martin Smith
  • 438,706
  • 87
  • 741
  • 845