Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 IF boolean test doesn't check out ?????

Author  Topic 

XPXJ
Starting Member

4 Posts

Posted - 2008-08-08 : 21:41:41
What does this syntax check?:

CREATE FUNCTION dbo.xyz
(
@var nvarchar(16)
)
RETURNS int
AS
BEGIN

DECLARE @alpha bit
DECLARE @ret int

SET @alpha = 1

IF @alpha
BEGIN
SET @ret = 6
END
ELSE
BEGIN
SET @ret = 99
END
RETURN @ret
END

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-09 : 00:09:30
[code]
DECLARE @alpha bit
DECLARE @ret int

SET @alpha = 1

IF @alpha
BEGIN
SET @ret = 6
END
ELSE
BEGIN
SET @ret = 99
END
RETURN @ret
END[/code]
the IF block check for @alpha since @alpha is set to 1 so it will always return value 6


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-09 : 01:52:53
you can use a case also to achieve same thing
CREATE FUNCTION dbo.xyz
(
@var nvarchar(16)
)
RETURNS int
AS
BEGIN

DECLARE @alpha bit
DECLARE @ret int

SET @alpha = 1
SET @ret =CASE WHEN @alpha THEN 6 ELSE 99 END
RETURN @ret
END
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-09 : 05:08:46
CREATE FUNCTION dbo.xyz
(
@var nvarchar(16)
)
RETURNS int
AS
BEGIN
RETURN 6
END



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-09 : 08:37:39
I wonder if T-SQL allows boolean checks like these

IF @alpha
....


SET @ret =CASE WHEN @alpha THEN 6 ELSE 99 END



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-09 : 11:42:53
quote:
Originally posted by madhivanan

I wonder if T-SQL allows boolean checks like these

IF @alpha
....


SET @ret =CASE WHEN @alpha THEN 6 ELSE 99 END



Madhivanan

Failing to plan is Planning to fail


yeah...thats true...that was an oversight from my part. it must have been

SET @ret =CASE WHEN @alpha=1 THEN 6 ELSE 99 END
Go to Top of Page
   

- Advertisement -