I don't know how exactly I should frame my Question as even I don't know what exactly I have to search. May be this is the best place to put my question. I want to use If condition in where clause this can be done using case but my problem is I don't want to use it after a column name instead I want to avoid entire execution of that column. You can understand it by going through my procedure in the last I have commented, So I want to achieve something like that as I want to write the same procedure again for that condition which i think can be achievable by doing something like that. I searched on internet what I got is to use case in where which doesn't satisfy my purpose. Please look the last lines to understand the problem. Thanx
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
You could use dynamic sql execution where you create your select statement as varchar to your liking, executing it at the end using sp_executesql
http://msdn.microsoft.com/en-us/library/ms188001.aspx
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
Related
I have following stored procedure and I have identified the issue with the stored procedure is that using select to set the value of #DeliveryAddress variable and so for all the Letter Requests raised it is only retaining the last value which is in Requests. I tried using SET by explicitly setting each value used in #DeliveryAddresss Calculation but of no use as I ended of getting error that subquery returns multiple rows.
I am unable to understand what shall I change in this SP, so that for each value in #Requests, a different #deliveryAddress value is set and used for insertion in LetterRequest table. Please help.
Note #WorkFlowAcct is a temp table having around 10 unique AccountIds and for each account we have atleast one debtorid.
ALTER PROCEDURE [dbo].[WorkFlow_Action_RequestLetterPref]
AS
DECLARE #LetterID INTEGER;
DECLARE #LetterType CHAR(3);
DECLARE #LetterDescription VARCHAR(50);
DECLARE #JobName VARCHAR(256);
DECLARE #DateCreated DATETIME;
DECLARE #DeliveryMethod VARCHAR(7);
DECLARE #DeliveryAddress VARCHAR(1023);
SELECT #LetterID = [LetterID],
#LetterType = CASE
WHEN [type] IN ('SIF', 'PIF', 'PPS', 'PDC', 'ATT', 'CUS') THEN [type]
ELSE 'DUN'
END,
#LetterDescription = ISNULL([Description], ''),
FROM [dbo].[letter]
WHERE [code] = #LetterCode;
DECLARE #Requests TABLE (
[AccountID] INTEGER NOT NULL,
[DebtorID] INTEGER NOT NULL,
[Seq] INTEGER NOT NULL,
[ErrorMessage] VARCHAR(500) NULL
);
IF #PrimaryDebtor = 1 OR #LetterType IN ('CUS', 'ATT') BEGIN
INSERT INTO #Requests ([AccountID], [DebtorID], [Seq])
SELECT DISTINCT [master].[number] AS [AccountID],
[Debtors].[DebtorID] AS [DebtorID],
[Debtors].[Seq] AS [Seq],
FROM #WorkFlowAcct AS [WorkFlowAcct]
INNER JOIN [dbo].[master] WITH (NOLOCK)
ON [WorkFlowAcct].[AccountID] = [master].[number]
INNER JOIN [dbo].[customer] WITH (NOLOCK)
ON [master].[customer] = [customer].[customer]
INNER JOIN [dbo].[Debtors] WITH (NOLOCK)
ON [master].[number] = [Debtors].[number]
AND [Debtors].[Seq] = [master].[PSeq]
LEFT OUTER JOIN [dbo].[DebtorAttorneys]
ON [Debtors].[DebtorID] = [DebtorAttorneys].[DebtorID];
END;
DECLARE #Street1 VARCHAR(512);
DECLARE #Street2 VARCHAR(512);
DECLARE #City VARCHAR(512);
DECLARE #Country VARCHAR(512);
DECLARE #Zip VARCHAR(512);
IF #Pref = 'Letter'
BEGIN
SET #DeliveryMethod = 'Letter';
SELECT #DeliveryAddress = [Street1] + ' ' + [Street2] + ' ' + [City] + ' ' + [Zipcode] + ' ' + [Country], #Street1 = [Street1], #Street2 = [Street2], #City = [City], #Country = [Country], #Zip = [ZipCode] FROM [Debtors] inner join #Requests AS Requests on [Debtors].[DebtorID] = Requests.[DebtorID] AND [Street1] IS NOT NULL;
IF #Street2 IS NULL
BEGIN
SET #DeliveryAddress = #Street1 + ' ' + #City + ' ' + #Zip + ' ' + #Country;
END
END
SET #DateCreated = GETDATE();
SET #JobName = 'WorkFlow_' + CAST(NEWID() AS CHAR(36)) + CAST(NEWID() AS CHAR(36)) + CAST(NEWID() AS CHAR(36)) + CONVERT(VARCHAR(50), GETDATE(), 126);
BEGIN TRANSACTION;
--Updated params for current Letter Request Table
INSERT INTO [dbo].[LetterRequest] ([AccountID], [LetterID], [LetterCode], [DeliveryMethod], [DeliveryAddress])
SELECT [Requests].[AccountID],
[Requests].[CustomerCode],
#LetterID AS [LetterID],
#LetterCode AS [LetterCode],
#DeliveryMethod AS [DeliveryMethod],
#DeliveryAddress AS [DeliveryAddress]
FROM #Requests AS [Requests]
INNER JOIN #AllowedCustomers AS [AllowedCustomers]
ON [Requests].[CustomerCode] = [AllowedCustomers].[CustomerCode]
WHERE [Requests].[ErrorMessage] IS NULL;
COMMIT TRANSACTION;
RETURN 0;
The "Where" clause of my SQL will vary depending on the values provided. I wrote 2 versions (Dynamic and static). I read that Dynamic is the way to go if we have multiple parameters with a variable where clause. Also, this is just a sample, my real SQL has many more conditions, but something along these lines. Which among the two is the right choice for this scenario?
Dynamic SQL:
DECLARE #SQL NVARCHAR(max)='SELECT ID,Name,sex,street,city,state,zip,phone FROM Test';
DECLARE #whereSql VARCHAR(2000) = 'WHERE city= #City';
DECLARE #OrderBy VARCHAR(2000) = 'order by name';
IF(#Name IS NOT NULL)
BEGIN
SET #Name = #Name + '%'
SET #whereSql = #whereSql + ' ' + 'AND name like #Name';
end
IF(#Gender IS NOT NULL)
BEGIN
SET #whereSql = #whereSql + ' ' + 'AND sex =' + '#Gender';
END
IF(#Address IS NOT NULL)
BEGIN
SET #Address = '%' + #Address + '%'
SET #whereSql = #whereSql + ' ' + 'AND street like ' + '#Address';
END
SET #SQL = #SQL +' ' + #whereSql + ' '+ #OrderBy;
EXEC sp_executesql #SQL, N'#Gender VARCHAR(10), #Name VARCHAR(200), #Address VARCHAR(250)', #Gender,#Name,#Address;
Static SQL:
SELECT ID,Name,sex,street,city,state,zip,phone FROM Test T1 WHERE
(#Name IS NULL OR (T1.Name LIKE +'%'+ #Name + '%'))
AND
(#Gender is null OR (T1.sex = #Gender))
AND
(#Address is null or (T1.street LIKE +'%'+ #Address + '%'))
I have a function in SQL that returns where clause result like this
CREATE FUNCTION dwd.GetSearchCriteria
(
#pValue NVARCHAR(50),
#pCriteriaOption NVARCHAR(3)
)
RETURNS varchar(30)
AS
BEGIN
declare
#WHERE varchar(2000)
IF(#pValue IS NOT NULL)
BEGIN
SET #WHERE = CASE WHEN #pCriteriaOption = 'ST' THEN ' like ''' + #pValue + '%'''
WHEN #pCriteriaOption = 'ET' THEN ' like ''' + '%' + #pValue + ''''
WHEN #pCriteriaOption = 'CT' THEN ' like ' + '''%' + #pValue + '%'''
ELSE ''
END
END
ELSE
BEGIN
SET #WHERE = '= NULL';
END
RETURN #WHERE
END
I have a query that like this which calls this function
declare
#WHERE varchar(2000)
SET #WHERE=dwd.GetSearchCriteria ('A','ET')
SELECT FirstName FROM dwd.[User] WHERE FirstName + #WHERE
My function returns suppose { like %A% } . I need to append it to first name .
It returns this error
An expression of non-boolean type specified in a context where a
condition is expected, near '#WHERE'.
I have along SP like this
ALTER PROCEDURE [dwd].[ADMIN_USER_DETAIL]
(
#pApplicationClientID bigint,
#pUserID bigint,
/* Optional Filters for Dynamic Search*/
#pFirstName nvarchar(50) = NULL,
#pLastName nvarchar(50) = NULL,
#pEmail nvarchar(255) = NULL,
#pLocation nchar(10) = NULL,
#pPhoneNumber nvarchar(50) = NULL,
#pPseudoName nvarchar(500) = NULL,
/*– Pagination Parameters */
#pPageNo INT = 1,
#pPageSize INT = 10,
/*– Sorting Parameters */
#pSortColumn nvarchar(50) = 'FirstName',
#pSortOrder nvarchar(4)='ASC',
#pTotalCount int output
)
AS
BEGIN
/*–Declaring Local Variables corresponding to parameters for modification */
DECLARE
#lFirstName nvarchar(50),
#lLastName nvarchar(50),
#lEmail nvarchar(255),
#lLocation nchar(10),
#lPhoneNumber nvarchar(50),
#lPseudoName nvarchar(500),
#lPageNbr INT,
#lPageSize INT,
#lSortCol NVARCHAR(50),
#lFirstRec INT,
#lLastRec INT,
#lTotalRows INT,
#TotalCount INT
/*Setting Local Variables*/
SET #lFirstName = LTRIM(RTRIM(#pFirstName))
SET #lLastName = LTRIM(RTRIM(#pLastName))
SET #lEmail = LTRIM(RTRIM(#pEmail))
SET #lLocation = LTRIM(RTRIM(#pLocation))
SET #lPhoneNumber = LTRIM(RTRIM(#pPhoneNumber))
SET #lPseudoName = LTRIM(RTRIM(#pPseudoName))
SET #lPageNbr = #pPageNo
SET #lPageSize = #pPageSize
SET #lSortCol = LTRIM(RTRIM(#pSortColumn))
SET #lFirstRec = ( #lPageNbr - 1 ) * #lPageSize
SET #lLastRec = ( #lPageNbr * #lPageSize + 1 )
SET #lTotalRows = #lFirstRec - #lLastRec + 1
; WITH User_Results
AS (
SELECT ROW_NUMBER() OVER (ORDER BY
CASE WHEN (#lSortCol = 'FirstName' AND #pSortOrder='ASC')
THEN U1.FirstName
END ASC,
CASE WHEN (#lSortCol = 'FirstName' AND #pSortOrder='DESC')
THEN U1.FirstName
END DESC,
CASE WHEN (#lSortCol = 'LastName' AND #pSortOrder='ASC')
THEN U1.LastName
END ASC,
CASE WHEN #lSortCol = 'LastName' AND #pSortOrder='DESC'
THEN U1.LastName
END DESC,
CASE WHEN #lSortCol = 'Email' AND #pSortOrder='ASC'
THEN U1.Email
END ASC,
CASE WHEN #lSortCol = 'Email' AND #pSortOrder='DESC'
THEN U1.Email
END DESC,
CASE WHEN #lSortCol = 'Location' AND #pSortOrder='ASC'
THEN U1.LocationID
END ASC,
CASE WHEN #lSortCol = 'Location' AND #pSortOrder='DESC'
THEN U1.LocationID
END DESC,
CASE WHEN #lSortCol = 'PhoneNumber' AND #pSortOrder='ASC'
THEN U1.PhoneNumber
END ASC,
CASE WHEN #lSortCol = 'PhoneNumber' AND #pSortOrder='DESC'
THEN U1.PhoneNumber
END DESC,
CASE WHEN #lSortCol = 'PseudoName' AND #pSortOrder='ASC'
THEN U1.PseudoName
END ASC,
CASE WHEN #lSortCol = 'PseudoName' AND #pSortOrder='DESC'
THEN U1.PseudoName
END DESC
) AS ROWNUM,
Count(*) over () AS TotalCount,
U1.ID,
U1.FirstName,
U1.LastName,
U1.Email,
DPD.Value as Location,
U1.PhoneNumber,
U2.FirstName+' '+U2.LastName as ReportTo,
U1.PseudoName,
U1.IsActive,
US.ID AS SId,
US.SkillID AS SkillId,
UR.ID AS RId,
UR.RoleID AS RoleId,
U1.Created,
U1.Deleted
FROM dwd.[User] U1 with (nolock)
INNER JOIN dwd.[User] U2 with (nolock) ON U2.ID=U1.ReportTo
INNER JOIN dwd.[DomainParameterDetail] DPD with (nolock) ON DPD.ID=U1.LocationID
INNER JOIN dwd.[UserSkill] US WITH(NOLOCK) ON U1.ID = US.UserID
INNER JOIN dwd.[UserRole] UR WITH(NOLOCK) ON U1.ID = UR.UserID
WHERE
(#lFirstName IS NULL OR U1.FirstName = #lFirstName)
AND(#lLastName IS NULL OR U1.LastName LIKE '%' + #lLastName + '%')
AND(#lEmail IS NULL OR U1.Email LIKE '%' + #lEmail + '%')
AND (#lLocation IS NULL OR U1.LocationID LIKE '%' + #lLocation + '%')
AND (#lPhoneNumber IS NULL OR U1.PhoneNumber = #lPhoneNumber)
AND (#lPseudoName IS NULL OR U1.PseudoName LIKE '%' +#lPseudoName+ '%')
AND U1.IsActive=1 AND U1.Deleted=0 AND U1.ApplicationClientID=#pApplicationClientID
AND U1.ID<>1
)
SELECT
TotalCount,
ROWNUM,
ID,
FirstName,
LastName,
Email,
Location,
PhoneNumber,
ReportTo,
PseudoName,
IsActive,
S
Id,
SkillId,
RId,
RoleId,
Created,
Deleted
FROM User_Results AS USER_GET_RESULT
WHERE
ROWNUM > #lFirstRec
AND ROWNUM < #lLastRec
ORDER BY ROWNUM ASC
Select #TotalCount = Count(*) from dwd.[User]
WHERE IsActive=1 AND Deleted=0 AND ID<>1
Set #pTotalCount = #TotalCount
END
SO I have to get this donw in where clause of SP.
This is a bit long for a comment.
You can only do what you want using dynamic SQL. But, that seems entirely unnecessary, because all the comparisons use LIKE. So, it is better to write the function as:
CREATE FUNCTION dwd.GetSearchCriteria (
#pValue NVARCHAR(50),
#pCriteriaOption NVARCHAR(3)
)
RETURNS varchar(2000)
AS
BEGIN
declare #LIKEPATTERN varchar(2000) ;
SET #LIKEPATTERN = (CASE WHEN #PValue IS NULL THEN ''''''
WHEN #pCriteriaOption = 'ST' THEN '''' + #pValue + '%'''
WHEN #pCriteriaOption = 'ET' THEN '''' + '%' + #pValue + ''''
WHEN #pCriteriaOption = 'CT' THEN '''%' + #pValue + '%'''
ELSE ''''''
END)
RETURN #LIKEPATTERN
END ;
Note: Using an IF and CASE is redundant. The logic can be combined into a single expression. I also think return types should match the type of the variable being returned.
Then you can express the call as:
DECLARE #LIKEPATTERN varchar(2000);
SET #LIKEPATTERN = dwd.GetSearchCriteria('A','ET');
SELECT FirstName FROM dwd.[User] WHERE FirstName LIKE #LIKEPATTERN;
I want to select particular elements if my condition is fulfilled. Please, look below at my code. How can I select the data in CASE clause? My way does not work
CREATE PROCEDURE [dbo].[GetUsers]
#firstName varchar(100) = '',
#isCounterSelected int,
#countries dbo.tvp_stringArray READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT [PK_UserID] AS Id
,[UserNickName]
,[description]
,CASE
WHEN #isCounterSelected = 1 THEN (NULL AS MCount, NULL AS GCount, NULL AS CCount)
ELSE ''
END
FROM [dbo].[User_Existing]
WHERE (#firstName NOT LIKE '' AND firstName LIKE #firstName + '%')
AND Country IN (SELECT inputValue FROM #countries)
I think this would be with syntax errors fixed:
CREATE PROCEDURE [dbo].[GetUsers]
#firstName varchar(100) = '',
#isCounterSelected int,
#countries dbo.tvp_stringArray READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT [PK_UserID] AS Id
,[UserNickName]
,[description]
,CASE
WHEN #isCounterSelected = 1 THEN NULL
ELSE ''
END AS MCount
,CASE
WHEN #isCounterSelected = 1 THEN NULL
ELSE ''
END AS GCount
,CASE
WHEN #isCounterSelected = 1 THEN NULL AS CCount
ELSE ''
END AS CCount
FROM [dbo].[User_Existing]
WHERE (#firstName NOT LIKE '' AND firstName LIKE #firstName + '%')
AND Country IN (SELECT inputValue FROM #countries)
END
I currently have a query that will pull a bunch of information from my database based on whatever where condition that I want to use.
declare #CaseNum char(7),
#ImportId char,
#FormatId char,
#SessionId char(5)
set #CaseNum = '' --I can place the value that I want to search by in here
set #ImportId = ''
set #FormatId = ''
set #SessionId = ''
--------------------
query in here
--------------------
where
gr.[CaseNum] = #CaseNum --currently I have to comment the ones I'm not using out
--im.[ImportId] = #ImportId
--fr.[FormatId] = #FormatId
--se.[SessionId] = #SessionId
I want to be able to take the comment portion out and simply display all rows if the parameter = ''
For example if I use set #CaseNum = '1234567' then it will search by that parameter and if I use #FormatId = '12' it will search by that one.
I have tried using the following and a few other attempts but I am getting nowhere fast.
where
gr.[CaseNum] = '%' + #CaseNum + '%'
and im.[ImportId] = '%' + #ImportId + '%'
and fr.[FormatId] = '%' + #FormatId + '%'
and se.[SessionId] = '%' + #SessionId + '%'
With help from the link that #Norman posted I figured it out. I wanted to post my solution for others to see.
declare #CaseNum varchar(MAX),
#ImportId varchar(MAX)
set #CaseNum = ''
set #ImportId = ''
----------------------------------------------------------------------------
If(#CaseNum = '') --Sets the parameter to NULL for COALESCE to work
Begin
Select #CaseNum = NULL
End
If(#ImportId = '') --Sets the parameter to NULL for COALESCE to work
Begin
Select #ImportId = NULL
End
--------------------
query in here
--------------------
where
gr.[CaseNum] = COALESCE(#CaseNum, gr.[CaseNum])
and im.ImportId = COALESCE(#ImportId, im.ImportId)
This solution allows the query to use just a single parameter or all at the same time.
You might want to look into building your query.
DECLARE #Number varchar(10)
DECLARE #Where varchar(max)
DECLARE #Query varchar(max)
SET #Query = 'SELECT * FROM TestTable'
SET #Where = ''
SET #Number = '3'
IF ISNULL(#Number, '') != ''
BEGIN
SET #Where = #Where + 'and testNumber = ' + #Number
END
IF LEN(#Where) > 0
BEGIN
SET #Where = SUBSTRING(#Where, 4, LEN(#Where))
END
if ISNULL(#Where, '') != ''
BEGIN
SET #Query = #Query + ' WHERE ' + #Where
END
EXEC(#Query)
Check out this gentleman's article for reference: https://social.msdn.microsoft.com/forums/sqlserver/en-US/1ec6ddd9-754b-4d78-8d3a-2b4da90e85dc/dynamically-building-where-clauses