3

I have searched and found nothing about it (I believe it is impossible to do it). My problem is that I have to check if a temporary table exists and also if there is some specific data on that temporary table.

Did anyone faced this before? How did you managed to solve it? I would like to avoid creating milions of IF..ELSE blocks.

EDIT:

IF (OBJECT_ID('tempdb..#tempTable') IS NOT NULL 
AND EXISTS (SELECT * FROM #tempTable WHERE THIS_COLUMN = 'value'))
BEGIN
   PRINT 'Temp table and data exists'
END
ELSE
BEGIN
   PRINT 'Temp table or data does not exist'
END

This is what I want to do. The problem comes when the tempTable doesn't exist (that could happen). It throws an error because, although the first stamement returns false, it continues to execute the second statement. And the SELECT statement is not able to find the table and therefore throws the error. The solution I found was to do this:

IF OBJECT_ID('#tempTable') IS NOT NULL
BEGIN
    IF EXISTS (SELECT * FROM #tempTable WHERE THIS_COLUMN = 'value'
    BEGIN
        PRINT 'Temp table and data exists'
    END
    ELSE
    BEGIN
        PRINT 'Temp table exists but data does not exist'
    END
END
ELSE
BEGIN
    PRINT 'Temp table does not exist'
END

My question would be, is there a way of having 2 conditions and if the first condition returns false not check the second one? Kind of using && in a programming language.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Carlos Pastor
  • 979
  • 2
  • 11
  • 26

5 Answers5

3

What you are trying to do is not possible as this is a compile time failure and the whole statement needs to be compiled together.

It won't evaluate the first part of the statement then compile the second part only if that is true. You need to split the test for existence and the query referencing the table into two separate statements so they are compiled separately.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
2

See here

There is no such thing as XAND logical gate (exclusive AND). In theory XAND would mean, that both operands are true or both are false. So this means XAND is the same as Equals (=), at least for bitwise logical operations.

Please show some sample code to illustrade what you are trying to do.

Regards

Community
  • 1
  • 1
LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
2

I have searched this sometime ago and if I remember correctly Sql Server does indeed short circuit logical conditions but it is it who decides which one it will check first regardless of the order in which they appear in the if clause.

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
1

This is probably a sketchy solution, but I sometimes use COALESCE statements to control the sorts of if, else, then structure you are trying to get at. In this case, it is a little more dirty looking because we are looking for the inverse of a coalesce statement.

DECLARE @temp_message AS varchar(100)
SELECT @temp_message = COALESCE(CASE
                                 WHEN OBJECT_ID('tempdb..#tempTable') IS NOT NULL THEN NULL
                                 ELSE 'Temp table does not exist'
                                END,
                                CASE
                                 WHEN EXISTS (SELECT * FROM #tempTable WHERE THIS_COLUMN = 'value') THEN NULL
                                 ELSE 'Specified value does not exist in temp table'
                                END,
                                'Temp table and data exists')

PRINT @temp_message

The COALESCE runs one statement after another until one does not yield a NULL value. That means you can do cool things like run a series of small queries to check some values before running a large costly query. Let me know if that is really illegitimate! It worked on my machine :)

bowserm
  • 1,046
  • 10
  • 25
0

I see two ways to get close to this in MSSQL:

First. If you use sp_executesql (dynamic sql) your stored procedure will be compiled without errors. Also if #tempTable doesn't exists server will output error but continue batch execution:

exec sp_executesql N'SELECT count(*) FROM #tempTable WHERE THIS_COLUMN = ''value'''
if @@rowcount > 0 
     print 'ok'
else 
     print 'error'
end;

Second. Just create User defined function with nested IF and EXISTS (as you do it now) which output 0 and 1. And use dynamic sql to input table name and possible filter value(s) to this UDF. In this case you can use this UDF in IF.

valex
  • 23,966
  • 7
  • 43
  • 60
  • This has no advantage here. It's still two statements. If the table doesn't exist the statement is subject to deferred compile anyway so it is only a problem if that statement is actually executed. You would only need this if referencing an existing table that has been modified via an `ALTER TABLE` or performing a `CREATE` that needs to be the only item in the batch. – Martin Smith Sep 20 '12 at 12:37