Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Table

Status
Not open for further replies.

mayu03

Programmer
Oct 3, 2003
91
US
From a sp in database1 I need to drop a table in database2.
How do I do this? Right now my statement can't see that table located in different db.
drop table [dbo].
 
Here is my statement, however it is not working.
if exists (select * from dbo.sysobjects where id = object_id(N'[prov].[dbo].[Prov]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [prov].[dbo].[Prov]
 
OBJECTPROPERTY(id, N'IsUserTable') = 1)
works in the current database.
You also need to reference sysobjects in the remote database.

You can do this by using xtype on sysobjects

if exists (select * from Prov.dbo.sysobjects where id = object_id(N'[prov].[dbo].[Prov]') and xtype = 'U')
drop table [prov].[dbo].[Prov]


You could also use sp_executesql to execute in the context of the remote database


exec Prov..sp_executesql N'if exists (select * from dbo.sysobjects where id = object_id(N''[Prov]'') and OBJECTPROPERTY(id, N''IsUserTable'') = 1)
drop table [Prov]'




======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top