在SQL中的where子句中追加字符串

时间:2016-02-10 15:02:38

标签: sql sql-server sql-function

我在SQL中有一个函数返回where子句结果,如此

CREATE FUNCTION dwd.GetSearchCriteria 
(
@pValue NVARCHAR(50),
@pCriteriaOption NVARCHAR(3)
) 
RETURNS varchar(30)
 AS
 BEGIN 
 declare 
 @WHERE varchar(2000) 
IF(@pValue IS NOT NULL)
BEGIN
SET @WHERE = CASE   WHEN @pCriteriaOption = 'ST' THEN   ' like ''' + @pValue + '%''' 
                    WHEN @pCriteriaOption = 'ET' THEN  ' like ''' + '%' + @pValue + '''' 
                    WHEN @pCriteriaOption = 'CT' THEN   ' like ' + '''%' + @pValue + '%''' 

                    ELSE '' 
                    END
END

ELSE
BEGIN
SET @WHERE = '= NULL';
END


RETURN @WHERE
END 

我有一个像这样调用此函数的查询

 declare 
 @WHERE varchar(2000) 
 SET @WHERE=dwd.GetSearchCriteria ('A','ET')
 SELECT FirstName FROM dwd.[User] WHERE FirstName + @WHERE 

我的函数返回假设{like%A%}。我需要将它附加到名字。 它返回此错误

  

在上下文中指定的非布尔类型的表达式   条件是预期的,靠近'@WHERE'。

我喜欢这样的

   ALTER PROCEDURE [dwd].[ADMIN_USER_DETAIL]
(
@pApplicationClientID   bigint,
@pUserID                bigint,
/* Optional Filters for Dynamic Search*/
@pFirstName             nvarchar(50) = NULL,
@pLastName              nvarchar(50) = NULL,
@pEmail                 nvarchar(255) = NULL,
@pLocation              nchar(10) = NULL,
@pPhoneNumber           nvarchar(50) = NULL,
@pPseudoName            nvarchar(500) = NULL,
/*– Pagination Parameters */
@pPageNo INT = 1,
@pPageSize INT = 10,
/*– Sorting Parameters */
@pSortColumn        nvarchar(50) = 'FirstName',
@pSortOrder         nvarchar(4)='ASC',
@pTotalCount int output
)
AS
BEGIN
    /*–Declaring Local Variables corresponding to parameters for modification */


 DECLARE 
    @lFirstName             nvarchar(50),
    @lLastName              nvarchar(50),
    @lEmail                 nvarchar(255),
    @lLocation              nchar(10),
    @lPhoneNumber           nvarchar(50),
    @lPseudoName            nvarchar(500),

        @lPageNbr INT,
        @lPageSize INT,
        @lSortCol NVARCHAR(50),
        @lFirstRec INT,
        @lLastRec INT,
        @lTotalRows INT,
        @TotalCount INT

        /*Setting Local Variables*/

        SET @lFirstName     =   LTRIM(RTRIM(@pFirstName))
        SET @lLastName      =   LTRIM(RTRIM(@pLastName))
        SET @lEmail         =   LTRIM(RTRIM(@pEmail))
        SET @lLocation      =   LTRIM(RTRIM(@pLocation))    
        SET @lPhoneNumber   =   LTRIM(RTRIM(@pPhoneNumber)) 
        SET @lPseudoName    =   LTRIM(RTRIM(@pPseudoName))  


        SET @lPageNbr = @pPageNo
        SET @lPageSize = @pPageSize
        SET @lSortCol = LTRIM(RTRIM(@pSortColumn))

        SET @lFirstRec = ( @lPageNbr - 1 ) * @lPageSize
        SET @lLastRec = ( @lPageNbr * @lPageSize + 1 )
        SET @lTotalRows = @lFirstRec - @lLastRec + 1

       ; WITH User_Results
        AS (
        SELECT ROW_NUMBER() OVER (ORDER BY
            CASE WHEN (@lSortCol = 'FirstName' AND @pSortOrder='ASC')
                        THEN U1.FirstName
            END ASC,
            CASE WHEN (@lSortCol = 'FirstName' AND @pSortOrder='DESC')
                       THEN U1.FirstName
            END DESC,

            CASE WHEN (@lSortCol = 'LastName' AND @pSortOrder='ASC')
                      THEN U1.LastName
            END ASC,
            CASE WHEN @lSortCol = 'LastName' AND @pSortOrder='DESC'
                     THEN U1.LastName
            END DESC,

            CASE WHEN @lSortCol = 'Email' AND @pSortOrder='ASC'
                    THEN U1.Email
            END ASC,
            CASE WHEN @lSortCol = 'Email' AND @pSortOrder='DESC'
                    THEN U1.Email
            END DESC,

            CASE WHEN @lSortCol = 'Location' AND @pSortOrder='ASC'
                    THEN U1.LocationID
            END ASC,
            CASE WHEN @lSortCol = 'Location' AND @pSortOrder='DESC'
                   THEN U1.LocationID
            END DESC,

            CASE WHEN @lSortCol = 'PhoneNumber' AND @pSortOrder='ASC'
                  THEN U1.PhoneNumber
            END ASC,
            CASE WHEN @lSortCol = 'PhoneNumber' AND @pSortOrder='DESC'
                  THEN U1.PhoneNumber
            END DESC,

            CASE WHEN @lSortCol = 'PseudoName' AND @pSortOrder='ASC'
                 THEN U1.PseudoName
            END ASC,
            CASE WHEN @lSortCol = 'PseudoName' AND @pSortOrder='DESC'
                 THEN U1.PseudoName
            END DESC

       ) AS ROWNUM,
       Count(*) over () AS TotalCount,
       U1.ID,
       U1.FirstName,
       U1.LastName,
       U1.Email,
       DPD.Value as Location,
       U1.PhoneNumber,
       U2.FirstName+' '+U2.LastName as ReportTo,
       U1.PseudoName,
       U1.IsActive,
       US.ID AS SId,
       US.SkillID AS SkillId,
       UR.ID AS RId,
       UR.RoleID AS RoleId,
       U1.Created,
       U1.Deleted
     FROM dwd.[User] U1 with (nolock)
     INNER JOIN dwd.[User] U2 with (nolock) ON  U2.ID=U1.ReportTo
     INNER JOIN dwd.[DomainParameterDetail] DPD  with (nolock) ON DPD.ID=U1.LocationID
     INNER JOIN dwd.[UserSkill] US WITH(NOLOCK)  ON U1.ID = US.UserID
     INNER JOIN dwd.[UserRole] UR WITH(NOLOCK) ON U1.ID = UR.UserID
     WHERE
        (@lFirstName IS NULL OR U1.FirstName = @lFirstName)
        AND(@lLastName IS NULL OR U1.LastName LIKE '%' + @lLastName + '%')
        AND(@lEmail IS NULL OR U1.Email LIKE '%' + @lEmail + '%')
        AND (@lLocation IS NULL OR U1.LocationID LIKE '%' + @lLocation + '%')
        AND (@lPhoneNumber IS NULL OR U1.PhoneNumber = @lPhoneNumber)
        AND (@lPseudoName IS NULL OR U1.PseudoName  LIKE '%' +@lPseudoName+ '%')
        AND U1.IsActive=1 AND U1.Deleted=0 AND U1.ApplicationClientID=@pApplicationClientID
        AND U1.ID<>1

    )
    SELECT
        TotalCount,
        ROWNUM,
        ID,
        FirstName,
        LastName,
        Email,
        Location,
        PhoneNumber,
       ReportTo,
        PseudoName,
        IsActive,
        S

Id,
    SkillId,
    RId,
    RoleId,
    Created,
    Deleted
FROM User_Results AS USER_GET_RESULT
WHERE
         ROWNUM > @lFirstRec
               AND ROWNUM < @lLastRec
 ORDER BY ROWNUM ASC
Select @TotalCount = Count(*) from dwd.[User] 
WHERE IsActive=1 AND Deleted=0 AND ID<>1 
 Set @pTotalCount = @TotalCount

END

所以我必须在SP的where子句中得到这个。

1 个答案:

答案 0 :(得分:3)

评论时间有点长。

您只能使用动态SQL执行所需操作。但是,这似乎完全没有必要,因为所有的比较都使用LIKE。因此,最好将函数编写为:

CREATE FUNCTION dwd.GetSearchCriteria (
    @pValue NVARCHAR(50),
    @pCriteriaOption NVARCHAR(3)
) 
RETURNS varchar(2000)
AS
BEGIN 
    declare @LIKEPATTERN varchar(2000) ;

    SET @LIKEPATTERN = (CASE WHEN @PValue IS NULL THEN ''''''
                             WHEN @pCriteriaOption = 'ST' THEN '''' + @pValue + '%''' 
                             WHEN @pCriteriaOption = 'ET' THEN '''' + '%' + @pValue + '''' 
                             WHEN @pCriteriaOption = 'CT' THEN '''%' + @pValue + '%''' 
                             ELSE ''''''
                       END)
    RETURN @LIKEPATTERN
END ;

注意:使用IFCASE是多余的。逻辑可以组合成单个表达式。我还认为返回类型应该与返回的变量的类型相匹配。

然后您可以将呼叫表达为:

DECLARE @LIKEPATTERN varchar(2000);
SET @LIKEPATTERN = dwd.GetSearchCriteria('A','ET');

SELECT FirstName FROM dwd.[User] WHERE FirstName LIKE @LIKEPATTERN;