Link to home
Start Free TrialLog in
Avatar of bkrenzin
bkrenzin

asked on

SQL - Object already named .... Object does not exist.

SQl Server 2008r2

Have you ever run into a situation where SQL is telling you that there is already an object named “xxx” in the database.

However, when run

DROP TABLE xxx

SQL tells you that the object does not exist?

I have looked in stored procedures, tables, system database.  I cannot find an object named “xxx” anywhere.
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Try looking for it in sys.objects.  That will give you the type.

select * from sys.objects where name = 'xxx'
It might also be due to the table's schema.  For example, "user1.xxx" rather than "dbo.xxx".

You might have a different default schema, so you might not "see" the table, but other users do.
Avatar of bkrenzin
bkrenzin

ASKER

select * from sys.objects where name like '%spSmartPrinting_pre_update%'

returns no records.

select * from sys.objects where name = 'SBM01.._spSmartPrinting_pre_update'

returns no records
if the table is another schema.... how do I "see" it so that I can DROP it?
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
select * from sys.objects where name like '%spSmartPrinting%update%'

returns no records
Since we don't know the actual name of the sp,

Can you make sure that the database from where you run the query is correct ?
Also can you check for the object in master database
Correction.... when running

select * from sys.objects where name like '%spSmartPrinting%update%'

on the correct DB (sorry for not paying closer attention).

I get 1 record returned.
User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Scott,
Check the image he posted, its a user table not a procedure
Just out of curiosity, everyone is using sys.objects, how about sys.tables?

IF EXISTS (SELECT name FROM sys.tables WHERE name='your_table_name') 
   DROP TABLE your_table_name
GO

CREATE your_table_name (blah blah blah) 

Open in new window


btw Scott's comment #2 in line is something I fall for all the time, creating an object that gets saved with a schema as something other than dbo, so it sorts way up in the top/bottom of the table list, giving the appearenace it doesn't exist.
>Just out of curiosity, everyone is using sys.objects, how about sys.tables?
In this case we didn't know that "spSmartPrinting_pre_update" could be a table.
User generated imageOK... do I dropped the table successfully.. but when I cut and paste my SQL statement it still underlines the table name in the script..

So.... i executed the DROP TABLE command at the bottom of the above image and you can see the results in the messages tab.
User generated image
Thought I might check to see if table exists with select statement.

See Results above.
The table will be created only if you have at least one record on spSmartPrinting table.. You could use the 'Debug' functionality in SSMS to see step by step execution
You should add a statement to see if the table exists, and if it does, to DROP it.

IF OBJECT_ID('SBM01.._spSmartPrinting_pre_update') IS NOT NULL
    DROP TABLE SBM01.._spSmartPrinting_pre_update
Thank You Gents!!!
Really appreciate your help and time this afternoon.

Regards,
Brian