If exists table using dynamic sql

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

    If exists table using dynamic sql

    Hi
    I want to check if a particular table exists or not in my DB.But that I have to check using dynamic sql.Can anyone provide me with the syntax
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    What do you have so far?

    -- CK

    Comment

    • SnehaAgrawal
      New Member
      • Apr 2009
      • 31

      #3
      I didn't get u?
      What I am trying to do is use the following query if that particular table exist in my DB.
      The query is

      select * from @Tablename

      @Tablename is my parameter passed from my prog.
      before using the select query I just wanna check if that table exists in my DB if Yes then go for select else return from my SP

      could u help out with this?

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        try:
        Code:
        IF OBJECT_ID(@Tablename) is not null
          then proceed
        else
          table does not exist
        or

        Code:
        if exists(select 1 from dbname..sysobjects where name = @Tablename and xtype = 'U')
           then proceed
        else
          table does not exists
        Happy Coding!!!

        --- CK

        Comment

        Working...