使用if存储过程动态查询

时间:2014-12-23 09:38:05

标签: sql-server stored-procedures

我有一个存储过程,我必须使用=<>作为参数来比较值,并将其与具有该值的另一​​个参数值等级进行比较1,2,3,4,5但不知道怎么做请帮忙。

这是我存储过程到目前为止我尝试过的内容,我有三个表userRatingReviewaccounttype

Create Procedure searchsp_LenderSearch
    @pAccountTypeId int =null,
    @pFirstName varchar(25)=null,
    @pLastName varchar(35)=null,
    @pZip varchar(10)=null,
    @pOperator varchar(2)=null,
    @pRating varchar(2)=null
AS
BEGIN

    SET NOCOUNT ON;
Declare @SQLQuery AS NVarchar(4000)
    SET @SQLQuery=
  'select 
   at.AccountName,
   U.UserId,
   U.FirstName,
   U.LastName,
   U.NMLS,
   U.[Address],
   U.PrimaryEmailId As Office,
   U.AdditionalEmail As Personal,
   U.DirectPhone As Work,
   U.Mobile,
   R.RatingStar

    from [User] U
    left outer join  RatingReview R   on R.UserId =U.UserId
    left outer join AccountType at on at.AccountTypeId = U.AccountTypeId
  where U.Deleted =0
  AND at.AccountTypeId = '+CAST(@pAccountTypeId as Varchar(10))+'
  AND U.FirstName = ''' + @pFirstName + '''
  AND U.LastName = ''' + @pLastName + '''
  AND U.Zip = ''' + @pZip + '''
  AND R.RatingStar = ''' + @pRating  + ''''


 IF(@pAccountTypeId  !=null OR @pAccountTypeId  != '')

    BEGIN
        SET @SQLQuery=@SQLQuery+' AND at.AccountTypeId='+CONVERT(VARCHAR, @pAccountTypeId )
    END

   IF(@pFirstName !=null OR @pFirstName != '')
    BEGIN
        SET @SQLQuery=@SQLQuery+' AND U.FirstName Like ''%' + @pFirstName + '%'''
    END

    IF(@pLastName !=null OR @pLastName != '')
    BEGIN
        SET @SQLQuery=@SQLQuery+' AND U.LastName Like ''%' + @pLastName + '%'''
    END

    IF(@pZip !=null OR @pZip != '')
    BEGIN
        SET @SQLQuery=@SQLQuery+' AND U.Zip Like ''%' + @pZip + '%'''
    END

    IF(@pRating !=null OR @pRating != '')
    BEGIN
        SET @SQLQuery=@SQLQuery+' AND R.RatingStar Like ''%' + @pRating + '%'''
    END

   IF(@pOperator = '=')
    BEGIN
    SET @SQLQuery += ' AND R.RatingStar = ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END

    IF(@pOperator = '>')
        BEGIN
        SET @SQLQuery += ' AND R.RatingStar > ' + CAST(@pRating AS NVARCHAR(5)) + ''
        END

    IF(@pOperator = '<')
        BEGIN
        SET @SQLQuery += ' AND R.RatingStar < ' + CAST(@pRating AS NVARCHAR(5)) + ''
        END
        SET @SQLQuery=@SQLQuery+
  'group by 
  at.AccountName,
  U.UserId,
   U.FirstName,
   U.LastName,
   U.NMLS,
   U.[Address],
   U.PrimaryEmailId,
   U.AdditionalEmail,
   U.DirectPhone,
   U.Mobile,
   R.RatingStar'

   EXEC (@SQLQuery) 
END

1 个答案:

答案 0 :(得分:1)

您需要转义传递给查询的参数,以便在@sqlstring变量中连接它们。试试这个问题:

Create PROCEDURE searchsp_LenderSearch
    @pAccountType int,
    @pFirstName varchar(25),
    @pLastName varchar(35),
    @pZip varchar(10),
    @pOperator varchar(2),
    @pRating int 
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @SQLQuery AS NVarchar(4000)
    SET @SQLQuery = '
SELECT 
    at.AccountTypeId,
    U.UserId,
    U.FirstName,
    U.LastName,
    R.RatingStar AS Rating,
    U.NMLS,
    U.[Address],
    U.PrimaryEmailId As Office,
    U.AdditionalEmail As Personal,
    U.DirectPhone As Work,
    U.Mobile
FROM [User] U
LEFT OUTER JOIN RatingReview R ON R.UserId = U.UserId
LEFT OUTER JOIN AccountType at ON at.AccountTypeId = U.AccountTypeId
WHERE U.Deleted = 0
  AND at.AccountTypeId = 1
  AND U.FirstName = ''' + @pFirstName + '''
  AND U.LastName = ''' + @pLastName + '''
  AND U.Zip = ''' + @pZip + ''''

IF(@pOperator = '=')
    BEGIN
    SET @SQLQuery += ' AND r.ratingstar = ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END
IF(@pOperator = '<')
    BEGIN
    SET @SQLQuery += ' AND r.ratingstar < ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END
IF(@pOperator = '>')
    BEGIN
    SET @SQLQuery += ' AND r.ratingstar > ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END

SET @SQLQuery += '
GROUP BY 
    at.AccountTypeId,
    U.UserId,
    U.FirstName,
    U.LastName,
    U.NMLS,
    U.[Address],
    U.PrimaryEmailId,
    U.AdditionalEmail,
    U.DirectPhone,
    U.Mobile,
    R.RatingStar'
EXEC (@SQLQuery) 
END

上面的代码将生成如下查询:

SELECT 
    at.AccountTypeId,
    U.UserId,
    U.FirstName,
    U.LastName,
    R.RatingStar AS Rating,
    U.NMLS,
    U.[Address],
    U.PrimaryEmailId As Office,
    U.AdditionalEmail As Personal,
    U.DirectPhone As Work,
    U.Mobile
FROM [User] U
LEFT OUTER JOIN RatingReview R ON R.UserId = U.UserId
LEFT OUTER JOIN AccountType at ON at.AccountTypeId = U.AccountTypeId
WHERE U.Deleted = 0
  AND at.AccountTypeId = 1
  AND U.FirstName = 'John'
  AND U.LastName = 'Adams'
  AND U.Zip = '100AA' AND r.ratingstar > 1
GROUP BY 
    at.AccountTypeId,
    U.UserId,
    U.FirstName,
    U.LastName,
    U.NMLS,
    U.[Address],
    U.PrimaryEmailId,
    U.AdditionalEmail,
    U.DirectPhone,
    U.Mobile,
    R.RatingStar

如果执行exec searchsp_LenderSearch 1, 'John', 'Adams', '100AA', '>',1

我认为这就是你想要的。显然,您可能希望添加一些错误检查和参数验证等。

根据此处的要求,更改的查询更新版本与更新后的问题相符:

Create Procedure searchsp_LenderSearch
    @pAccountTypeId int       = null,
    @pFirstName varchar(25) = null,
    @pLastName      varchar(35) = null,
    @pZip           varchar(10) = null,
    @pOperator      varchar(2)  = null,
    @pRating        varchar(2)  = null
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @SQLQuery AS NVarchar(4000)
    SET @SQLQuery ='
SELECT 
    at.AccountName,
    U.UserId,
    U.FirstName,
    U.LastName,
    U.NMLS,
    U.[Address],
    U.PrimaryEmailId As Office,
    U.AdditionalEmail As Personal,
    U.DirectPhone As Work,
    U.Mobile,
    R.RatingStar
FROM [User] U
LEFT OUTER JOIN RatingReview R ON R.UserId = U.UserId
LEFT OUTER JOIN AccountType at ON at.AccountTypeId = U.AccountTypeId
WHERE U.Deleted = 0
 '
IF(@pAccountTypeId  !=null OR @pAccountTypeId  != '')
    BEGIN
        SET @SQLQuery += ' AND at.AccountTypeId = '+CONVERT(VARCHAR(10), @pAccountTypeId ) + '' 
    END

IF(@pFirstName !=null OR @pFirstName != '')
    BEGIN
        SET @SQLQuery += ' AND U.FirstName Like ''%' + @pFirstName + '%'''
    END

IF(@pLastName !=null OR @pLastName != '')
    BEGIN
        SET @SQLQuery += ' AND U.LastName Like ''%' + @pLastName + '%'''
    END

IF(@pZip !=null OR @pZip != '')
    BEGIN
        SET @SQLQuery += ' AND U.Zip Like ''%' + @pZip + '%'''
    END

IF(@pOperator = '=')
    BEGIN
        SET @SQLQuery += ' AND R.RatingStar = ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END

IF(@pOperator = '>')
    BEGIN
        SET @SQLQuery += ' AND R.RatingStar > ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END

IF(@pOperator = '<')
    BEGIN
        SET @SQLQuery += ' AND R.RatingStar < ' + CAST(@pRating AS NVARCHAR(5)) + ''
    END

SET @SQLQuery += '
GROUP BY 
    at.AccountName,
    U.UserId,
    U.FirstName,
    U.LastName,
    U.NMLS,
    U.[Address],
    U.PrimaryEmailId,
    U.AdditionalEmail,
    U.DirectPhone,
    U.Mobile,
    R.RatingStar'

    EXEC (@SQLQuery) 
   --PRINT (@SQLQuery) 
END