January 22, 2003 at 5:46 pm
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?
January 22, 2003 at 6:36 pm
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
January 22, 2003 at 6:41 pm
You can also do
IF OBJECT_ID('tblname') IS NOT NULL
but the IF EXISTS method is MS standard way.
January 23, 2003 at 4:29 pm
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?
January 23, 2003 at 6:12 pm
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