使用if之间的where子句

时间:2014-07-04 19:48:02

标签: sql sql-server database sql-server-2008

我不知道我应该如何构建我的问题,即使我不知道究竟要搜索什么。可能这是提出问题的最佳地点。我想在where子句中使用If条件,这可以使用case来完成,但我的问题是我不想在列名之后使用它而是我想避免整个执行该列。你可以通过我最后一次评论的程序来理解它,所以我想要实现类似的东西,因为我想再次编写相同的程序,我认为可以通过做类似的事情来实现。我在互联网上搜索我得到的是在不符合我目的的地方使用案例。请查看最后一行以了解问题。感谢名单

    USE [overseascrm]
GO
/****** Object:  StoredProcedure [dbo].[candidatesearch]    Script Date: 05-07-2014 00:48:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[candidatesearch] 
@candidate_id varchar(50),
@firstname varchar(50),
@lastname varchar(50),
@emailid_1 varchar(100),
@mobile_1 varchar(20),
@from varchar(50),
@to varchar(50),
@agentid varchar(50),
@passportno varchar(50),
@profession int,
@isactive bit
as

begin
DECLARE @candidateid_var  varchar(50)
DECLARE @firstname_var varchar(50)
DECLARE @lastname_var varchar(50)
DECLARE @emailid1_var varchar(100)
DECLARE @mobile_1_var varchar(20)
DECLARE @agentid_var varchar(50)


IF(@agentid = '')
begin
SET @agentid_var = '%'
END ELSE BEGIN
SET @agentid_var = @agentid
END


IF (@candidate_id = '') BEGIN
SET @candidateid_var = '%'
END ELSE BEGIN
SET @candidateid_var = @candidate_id
END

IF (@firstname = '') BEGIN
SET @firstname_var = '%'
END ELSE BEGIN
SET @firstname_var = @firstname
END

IF (@lastname = '') BEGIN
SET @lastname_var = '%'
END ELSE BEGIN
SET @lastname_var = @lastname
END

IF (@emailid_1 = '') BEGIN
SET @emailid1_var = '%'
END ELSE BEGIN
SET @emailid1_var = @emailid_1
END

IF (@mobile_1 = '') BEGIN
SET @mobile_1_var = '%'
END ELSE BEGIN
SET @mobile_1_var = @mobile_1
END

IF (@from = '') BEGIN
SELECT
    *
FROM candidate C
LEFT JOIN candidate_profession_map CM
    ON C.candidate_id = CM.candidate_id
LEFT JOIN passport_details PD
    ON C.candidate_id = PD.candidate_id
WHERE C.candidate_id LIKE '' + @candidateid_var + '%'
AND firstname LIKE '' + @firstname_var + '%'
AND lastname LIKE '' + @lastname_var + '%'
AND emailid_1 LIKE '' + @emailid1_var + '%'
AND mobile_1 LIKE '' + @mobile_1_var + '%'
AND agent_id LIKE '' + @agentid_var + '%'
AND CM.profession_id = @profession
AND C.isactive = @isactive
AND PD.passport_no = @passportno
END ELSE BEGIN
SELECT
    *
FROM candidate C
LEFT JOIN candidate_profession_map CM
    ON C.candidate_id = CM.candidate_id
LEFT JOIN passport_details PD
    ON C.candidate_id = PD.candidate_id
WHERE C.candidate_id LIKE '' + @candidateid_var + '%'
AND firstname LIKE '' + @firstname_var + '%'
AND lastname LIKE '' + @lastname_var + '%'
AND emailid_1 LIKE '' + @emailid1_var + '%'
AND mobile_1 LIKE '' + @mobile_1_var + '%'
AND agent_id LIKE '' + @agentid_var + '%'
AND C.addedon BETWEEN CONVERT(DATETIME, @from, 103) AND CONVERT(DATETIME, @to, 103)
--IF (@profession <> 0)BEGIN
--AND CM.profession_id = @profession
--END
AND C.isactive = @isactive
OR PD.passport_no = @passportno
END

END

2 个答案:

答案 0 :(得分:1)

您可以使用动态sql执行,根据自己的喜好将select语句创建为varchar,最后使用sp_executesql执行它

http://msdn.microsoft.com/en-us/library/ms188001.aspx

答案 1 :(得分:0)

ALTER procedure [dbo].[candidatesearch] 
    @candidate_id varchar(50),
    @firstname varchar(50),
    @lastname varchar(50),
    @emailid_1 varchar(100),
    @mobile_1 varchar(20),
    @from varchar(50),
    @to varchar(50),
    @agentid varchar(50),
    @passportno varchar(50),
    @profession int,
    @isactive bit
AS Begin

    SELECT *
    FROM candidate C
    LEFT JOIN candidate_profession_map CM ON C.candidate_id = CM.candidate_id
    LEFT JOIN passport_details PD ON C.candidate_id = PD.candidate_id
    WHERE (@candidateid='' or C.candidate_id LIKE @candidateid + '%')
    AND (@firstname = '' or firstname LIKE @firstname + '%')
    AND (@lastname = '' or lastname LIKE @lastname + '%')
    AND (@emailid_1='' or emailid_1 LIKE @emailid1 + '%')
    AND (@mobile_1 = '' or mobile_1 LIKE @mobile_1 + '%')
    AND (@agentid='' agent_id LIKE @agentid + '%')
    AND C.isactive = @isactive
    And ((@from='' 
               AND CM.profession_id = @profession 
               AND PD.passport_no = @passportno) 
         or (@from<>'' 
               And C.addedon BETWEEN CONVERT(DATETIME, @from, 103) AND CONVERT(DATETIME, @to, 103) 
               OR PD.passport_no = @passportno)

End