Explain this behavior with NULL return value from select inside if boolean_expression

Explain this behavior with NULL return value from select inside if boolean_expression

Post by scott a kin » Thu, 29 Jan 1998 04:00:00



create table #foo (ID int not null, val int null)
insert into #foo values (1, 1)
insert into #foo values (2, NULL)
select * from #foo




if ( (select val from #foo where ID = 1) != (select val from #foo where
ID =2))
    print "Values are different 1"
if ( (select val from #foo where ID = 1) != NULL)
    print "Values are different 2"
if ( (select val from #foo where ID = 2) is NULL)
    print "Values are different 3"


    print "Values are different 4"

    print "Values are different 5"

(2 rows affected)
(1 row affected)
(1 row affected)
 ID          val        
 ----------- -----------
           1           1
           2        NULL

(2 rows affected)
(1 row affected)
(1 row affected)
(0 rows affected)
(0 rows affected)
Values are different 2
(0 rows affected)
Values are different 3

 ----------- -----------
           1        NULL

(1 row affected)
Values are different 4
(0 rows affected)

I'm running sybase 10. something (early version)

why does

    print "Values are different 5"
work buy

    print "Values are different 4"
does not?

And while we are at why do I get
1> select NULL
2> go

 -----------
        NULL

(1 row affected)

 if (1 != NULL) print "okay"
2> go
okay
1> if (1 != (select 2)) print "okay"
2> go
(0 rows affected)
okay
1> if (1 != select NULL) print "okay"
2> go
Msg 156, Level 15, State 1:
Line 1:
Incorrect syntax near the keyword 'select'.

--
Scott King                                 395 Dreese Lab
V: 614 292-0092 F: 614 292-2911            2015 Neil Avenue


http://www.cis.ohio-state.edu/~sking/