之間使用if我不知道我應該如何框架我的問題,因爲即使我不知道我究竟要搜索什麼。可能是這個問題最好的地方。我想在where子句中使用If條件,這可以用case來完成,但我的問題是我不想在列名後面使用它,而是希望避免整個執行該列。你可以通過我在上次我評論過的程序來了解它,所以我想實現這樣的事情,因爲我想再次爲這種情況編寫相同的過程,我認爲這可以通過做類似的事情來實現。我在互聯網上搜索我得到的是在哪些地方不符合我的目的。請看最後一行來理解問題。感謝名單如果在where子句
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
'AND(@profession = 0或CM.profession_id = @profession)'? –
Thanx!很多這是我正在尋找我的壞。我想錯誤的方式。 –