By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,574 Members | 1,968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,574 IT Pros & Developers. It's quick & easy.

Condition check for If table exist

P: 31
How can I use the following Query using Dynamic Sql.The purpose of using Dynamic SQL is the table is on another server.

Generally when I use Dynamic SQL against select query I use
EXEC('SELECT * FROM '+@SERVERNAME+'.'+@LOCALDBNAME+'.DBO.'+'«USTOMERS' )

Here goes my Query

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MyTable]
GO
Sep 1 '09 #1
Share this Question
Share on Google+
1 Reply


ck9663
Expert 2.5K+
P: 2,878
try:

Expand|Select|Wrap|Line Numbers
  1.  
  2. exec('
  3. if object_id(''' + @database_name + '.owner.tablename'') is not null
  4.   drop table database.owner.tablename'
  5. ')
  6.  
  7.  
Happy Coding!!!


--- CK
Sep 1 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.