我正在尝试运行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)
它的工作正常但是当我用“喜欢”替换“=”时,
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%')
它不起作用???
希望你的建议
由于
答案 0 :(得分:4)
你的LIKE示例试图匹配那些实际的字符串值,即 user_Zip包含文字文本“@Zip”
你需要像这样连接:
where (@Zip is null or ub.user_Zip like '%' + @Zip + '%') and
... etc
答案 1 :(得分: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+'%')
拉吉