IsObject Function in SQL?

  • I'm trying to run code in SQL that will look for a table by name, to see if it exists; and depending on whether or not it does, perform specific tasks. Problem is, I can't find such a function to test for the table's existence. I know in VB you can use IsObject. Does a similar function exists in SQL?

  • IF EXISTS( SELECT 1 FROM sysobjects WHERE name = 'table_name' AND type = 'U' ) ...

    Type 'U' is for User-defined table.

    Alternatively, you could use the SCHEMA object.

    Edited by - mromm on 01/22/2003 6:37:52 PM

  • You can also do

    IF OBJECT_ID('tblname') IS NOT NULL

    but the IF EXISTS method is MS standard way.

  • that seems to do the trick with the table test; but what about an ELSE statement? how does SQL know where the THEN part of the IF ends?

  • IF <condition>

    <code block>

    ELSE

    <another code block>

    where <code block> is either one statement or block as below:

    BEGIN

    <some code>

    END

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

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