列的IF语句

时间:2014-09-15 18:31:36

标签: sql function if-statement compare case

我需要验证这样的列:

CREATE FUNCTION [dbo].[VALIDATE_GENDER]()
RETURNS BIT
AS
BEGIN
    DECLARE @FLAG BIT
    /* IF ID_gender = 1 AND name_gender = 'Female' OR ID_gender = 2 AND
       name_gender = 'Male' THEN SET @FLAG = 1 
       ELSE
           SET @FLAG = 0 */
    RETURN @FLAG
END
GO

我尝试使用CASE,但无济于事:

CREATE FUNCTION [dbo].[VALIDATE_GENDER]()
RETURNS BIT
AS
BEGIN
    DECLARE @FLAG BIT
    SELECT @FLAG = CASE WHEN (ID_gender = 1 AND name_gender = 'Female' 
    OR ID_gender = 2 AND name_gender = 'Male') THEN 1
    ELSE 0 END FROM GENDER
    RETURN @FLAG
END
GO

拜托,有人可以帮我吗?谢谢。

0 个答案:

没有答案