CREATE FUNCTION UDF_MALEEMPLOYEES(@GENDER NVARCHAR(4))
RETURNS TABLE
AS RETURN
IF (@GENDER='NULL')
(SELECT CONCAT(FIRSTNAME, CASE WHEN MIDDLENAME IS NULL THEN ' ' ELSE ' ' +MIDDLENAME+' ' END,LASTNAME)
AS FULLNAME, CAST(BIRTHDATE AS DATE) AS BIRTHDATE,GENDER FROM PERSON.PERSON
A INNER JOIN [Sales].[vPersonDemographics] B ON A.BUSINESSENTITYID = B.BUSINESSENTITYID
WHERE GENDER IS NULL)
ELSE
(SELECT CONCAT(FIRSTNAME, CASE WHEN MIDDLENAME IS NULL THEN ' ' ELSE ' ' +MIDDLENAME+' ' END,LASTNAME)
AS FULLNAME, CAST(BIRTHDATE AS DATE) AS BIRTHDATE,GENDER FROM PERSON.PERSON
A INNER JOIN [Sales].[vPersonDemographics] B ON A.BUSINESSENTITYID = B.BUSINESSENTITYID
WHERE GENDER =@GENDER)
Null不能等于null。您必须使用IS NULL,如下所示
CREATE FUNCTION UDF_MALEEMPLOYEES(@GENDER NVARCHAR(4))
RETURNS TABLE
AS RETURN
IF (@GENDER IS NULL)
(SELECT CONCAT(FIRSTNAME, CASE WHEN MIDDLENAME IS NULL THEN ' ' ELSE ' ' +MIDDLENAME+' ' END,LASTNAME)
AS FULLNAME, CAST(BIRTHDATE AS DATE) AS BIRTHDATE,GENDER FROM PERSON.PERSON
A INNER JOIN [Sales].[vPersonDemographics] B ON A.BUSINESSENTITYID = B.BUSINESSENTITYID
WHERE GENDER IS NULL)
ELSE
(SELECT CONCAT(FIRSTNAME, CASE WHEN MIDDLENAME IS NULL THEN ' ' ELSE ' ' +MIDDLENAME+' ' END,LASTNAME)
AS FULLNAME, CAST(BIRTHDATE AS DATE) AS BIRTHDATE,GENDER FROM PERSON.PERSON
A INNER JOIN [Sales].[vPersonDemographics] B ON A.BUSINESSENTITYID = B.BUSINESSENTITYID
WHERE GENDER =@GENDER)
当与NULL
比较时,你使用IS NULL
,而不是='NULL'
,因为最后一个是varchar文字。
您还可以使您的功能更容易,更易于维护,如下所示:
CREATE FUNCTION UDF_MALEEMPLOYEES(@GENDER NVARCHAR(4))
RETURNS TABLE
AS RETURN
SELECT
CONCAT(FIRSTNAME, CASE WHEN MIDDLENAME IS NULL THEN ' ' ELSE ' ' +MIDDLENAME+' ' END,LASTNAME) AS FULLNAME,
CAST(BIRTHDATE AS DATE) AS BIRTHDATE,
GENDER
FROM
PERSON.PERSON A
INNER JOIN [Sales].[vPersonDemographics] B ON A.BUSINESSENTITYID = B.BUSINESSENTITYID
WHERE
ISNULL(GENDER, '') = ISNULL(@GENDER, '')