0

我试图运行 sp 当我用“Like”替换“=”时它没有给出任何结果

我的查询在下面定义,

ALTER PROCEDURE [dbo].[SelectMentor] 
    -- Add the parameters for the stored procedure here
    @Zip varchar(20)=NULL,
    @Company varchar(200)=NULL,
    @Designation varchar(100)=NULL,
    @Interest varchar(200)=NULL
    --@JobFunc varchar(200)=NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    select ub.user_Id,ub.user_Fullname,up.Designation,up.Company from UserBasics UB 
inner join UserProfession up on  
ub.user_Id=up.Prof_ID 
where  (@Zip is null or ub.user_Zip = @Zip) and
    (@Interest is null or ub.user_Need = @Interest) and
--  (@JobFunc is null or m.mentor_jobFunction= @JobFunc) and
    (@Company is null or up.Company=@Company) and
    (@Designation is null or up.Designation=@Designation)

它工作正常但是当我用“Like”替换“=”时,

select ub.user_Id,ub.user_Fullname,up.Designation,up.Company from UserBasics UB 
inner join UserProfession up on  
ub.user_Id=up.Prof_ID 
where  (@Zip is null or ub.user_Zip like '%@Zip%') and
    (@Interest is null or ub.user_Need like '%@Interest%') and
--  (@JobFunc is null or m.mentor_jobFunction= @JobFunc) and
    (@Company is null or up.Company like '%@Company%') and
    (@Designation is null or up.Designation like '%@Designation%')

它不工作???

希望您的建议

谢谢

4

2 回答 2

4

您的 LIKE 示例正在尝试匹配那些实际的字符串值,即 user_Zip 包含文字文本“@Zip”

您需要像这样连接:

where  (@Zip is null or ub.user_Zip like '%' + @Zip + '%') and
... etc
于 2013-01-24T09:46:54.947 回答
2

尝试这个

select ub.user_Id,ub.user_Fullname,up.Designation,up.Company from UserBasics UB 
    inner join UserProfession up on  
    ub.user_Id=up.Prof_ID 
    where  (@Zip is null or ub.user_Zip like '%'+@Zip+'%') and
        (@Interest is null or ub.user_Need like '%'+@Interest+'%') and
    --  (@JobFunc is null or m.mentor_jobFunction= @JobFunc) and
        (@Company is null or up.Company like '%'+@Company+'%') and
        (@Designation is null or up.Designation like '%'+@Designation+'%')

拉吉

于 2013-01-24T09:48:08.650 回答