Function to see if Table exists in sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barno77
    New Member
    • Oct 2007
    • 22

    Function to see if Table exists in sql server

    Trying to build a function to see if table exists and if it doesn't then create that table!

    Help would be greatly appreciated!
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by Barno77
    Trying to build a function to see if table exists and if it doesn't then create that table!

    Help would be greatly appreciated!

    lots of ways...one of them:

    if not exists (select 1 from sysobjects where name = @mytableparamet ername)
    begin
    create table ....

    or you can also do a..

    select * newtablename from blanktemplateta ble

    or

    set @sqlstring = ('create table ' + @mytableparamet ername + '.....')

    exec (@sqlstring)

    or

    set @sqlstring = ('select * into ' + @mytableparamet ername + ' from blanktemplateta ble')
    exec (@sqlstring)

    end


    this is obviously a pseudo-code...but i hope you get the idea...

    -- CK

    Comment

    • Barno77
      New Member
      • Oct 2007
      • 22

      #3
      Originally posted by ck9663
      lots of ways...one of them:

      if not exists (select 1 from sysobjects where name = @mytableparamet ername)
      begin
      create table ....

      or you can also do a..

      select * newtablename from blanktemplateta ble

      or

      set @sqlstring = ('create table ' + @mytableparamet ername + '.....')

      exec (@sqlstring)

      or

      set @sqlstring = ('select * into ' + @mytableparamet ername + ' from blanktemplateta ble')
      exec (@sqlstring)

      end


      this is obviously a pseudo-code...but i hope you get the idea...

      -- CK
      Yes, I understand thank you!

      Comment

      Working...