我做错了什么?使用=
,>
,<
作为参数时,我的存储过程无法正常工作请帮助
这是我的存储过程:
ALTER PROCEDURE [dbo].[storedp_Search]
@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(@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
当我使用以下exec语句时,其工作正常
Execute storedp_Search 1,'Mark','Smith','48393','=','5'
但是当使用相同的条件时,它不起作用。
Execute storedp_Search 1,'Mark','Smith','48393','>','4'
答案 0 :(得分:0)
试试这个,
我认为你在if条件下拼错了操作符,如果我没错的话
ALTER PROCEDURE [dbo].[Storedp_Search] @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 IS NOT NULL
OR @pAccountTypeId != '' )
BEGIN
SET @SQLQuery=@SQLQuery + ' AND at.AccountTypeId='
+ CONVERT(VARCHAR, @pAccountTypeId )
END
IF( @pFirstName IS NOT NULL
OR @pFirstName != '' )
BEGIN
SET @SQLQuery=@SQLQuery + ' AND U.FirstName Like ''%'
+ @pFirstName + '%'''
END
IF( @pLastName IS NOT NULL
OR @pLastName != '' )
BEGIN
SET @SQLQuery=@SQLQuery + ' AND U.LastName Like ''%'
+ @pLastName + '%'''
END
IF( @pZip IS NOT NULL
OR @pZip != '' )
BEGIN
SET @SQLQuery=@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=@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