How to test if a table is present on a database

  • Hello all,

     

    I am starting to write T-SQL script and can't find how to test if a table is present on a database.

    Somebody could help me please ??

     

    Thanks in adv

  • Hi,

    You can check using this qry:

    USE yourDatabase

    select * from sysobjects where name = 'yourTable'

    Hello from Guatemala,

    Ricardo

  • If you are looking for tables you have to add xtype to the previous query:

    select * from sysobjects where name = 'yourTable' and xtype = 'U'

     

    Also, you can query the view information_schema.tables:

    select * from information_schema.tables where table_name = 'yourTable'

  • YAW alternative (Yet Another Way)

    -- If assumption is to check for existance first

    IF OBJECT_ID('dbo.myTable') IS NOT NULL AND OBJECTPROPERTY(OBJECT_ID('dbo.myTable'), 'IsTable') = 1

    -- If assumption is to check for non-existance first

    IF OBJECT_ID('dbo.myTable') IS NULL

    /Kenneth

  • It works very fine !! Thanks a lot 

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply