Condition check for If table exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SnehaAgrawal
    New Member
    • Apr 2009
    • 31

    #1

    Condition check for If table exist

    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.'+'ÇUSTO MERS')

    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
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    try:

    Code:
    exec('
    if object_id(''' + @database_name + '.owner.tablename'') is not null
      drop table database.owner.tablename'
    ')
    Happy Coding!!!


    --- CK

    Comment

    Working...