I wrote a stored procedure that filters my table. It is working fine, and selects the correct records, but in the result for example if query have to return one record.
Instead of one record return too many record is it possible my stored procedure executes several times?
I use this code:
ALTER PROCEDURE [dbo].[propertyFilterByCity]
#city NVARCHAR(150) = NULL,
#DefaultPageIndex smallint = NULL,
#DefaultPageSize smallint = NULL,
#RecordCount bit = NULL,
#MinPrice decimal(18, 0) = NULL,
#MaxPrice decimal(18, 0) = NULL,
#bedRoom smallint = NULL,
#PropertyType NVARCHAR(20) = NULL,
#requestType NVARCHAR(20) = NULL,
#Parking bit = NULL,
#RemoteParking bit = NULL,
#Lobby bit = NULL,
#AssemblyHall bit = NULL,
#Gym bit = NULL,
#Surveillance bit = NULL,
#FireAlarm bit = NULL,
#FireFighting bit = NULL,
#Pool bit = NULL,
#Sauna bit = NULL,
#Jacuzzi bit = NULL,
#Carwash bit = NULL,
#Laundry bit = NULL,
#Custodian bit = NULL,
#Shooting bit = NULL,
#TheftAlarm bit = NULL,
#PanelFit bit = NULL,
#Sentry bit = NULL,
#CentralSatellite bit = NULL,
#CentralAntenna bit = NULL,
#Fireplaces bit = NULL,
#MasterRoom bit = NULL,
#Patio bit = NULL,
#Barbecue bit = NULL,
#UPS bit = NULL,
#RoofGarden bit = NULL
AS
BEGIN
BEGIN TRY
SET NOCOUNT ON ;
BEGIN TRANSACTION;
DECLARE #CityId int;
IF EXISTS(SELECT Estate_CityId FROM [^Estate_City] WHERE Estate_CityName = #city)
BEGIN
SET #CityId= (SELECT Estate_CityId FROM [^Estate_City] WHERE Estate_CityName = #city);
END
IF(#MinPrice IS NOT NULL AND #MaxPrice IS NOT NULL)
BEGIN
IF (#MaxPrice>0 AND #MaxPrice>#MinPrice)
BEGIN
DECLARE #results TABLE (RowNum INT,Estate_Code BIGINT,Estate_propertyType NVARCHAR(50),Estate_buildingArea NVARCHAR(20),Estate_floor NVARCHAR(10),Estate_neighborhoodProperty NVARCHAR(100),Estate_street1 NVARCHAR(100),
Estate_street2 NVARCHAR(100),Estate_visits BIGINT,houseImage NVARCHAR(MAX),houseImage1 NVARCHAR(MAX),houseImage2 NVARCHAR(MAX),houseImage3 NVARCHAR(MAX),houseImage4 NVARCHAR(MAX),Owner_requestType NVARCHAR(MAX),
Facility_Description NVARCHAR(MAX),Estate_BedRoomCount NVARCHAR(2))
INSERT INTO #results(RowNum,Estate_Code,Estate_propertyType,Estate_buildingArea,Estate_floor,Estate_neighborhoodProperty,Estate_street1,Estate_street2,Estate_visits,houseImage,houseImage1,
houseImage2,houseImage3,houseImage4,Owner_requestType,Facility_Description,Estate_BedRoomCount)
SELECT ROW_NUMBER() OVER(ORDER BY [^Estate].Estate_Code) AS [Row],[^Estate].Estate_Code,[^Estate].Estate_propertyType, [^Estate].Estate_buildingArea,[^Estate].Estate_floor,[^Estate].Estate_neighborhoodProperty,[^Estate].Estate_street1,[^Estate].Estate_street2,[^Estate].Estate_visits,[^Estate_Images].houseImage,[^Estate_Images].houseImage1,[^Estate_Images].houseImage2,[^Estate_Images].houseImage3,[^Estate_Images].houseImage4,[^Estate_OwnerInfo].RequestType_Type,[^Estate_Facility].Facility_descText,[^Estate].Estate_bedRoomCount
FROM [^Estate]
INNER JOIN [^Estate_Images] ON [^Estate].Estate_Code=[^Estate_Images].Estate_Code
INNER JOIN [^Estate_OwnerInfo] ON [^Estate].Owner_code=[^Estate_OwnerInfo].Owner_code
INNER JOIN [^Estate_Facility] ON [^Estate].Estate_Code=[^Estate_Facility].Estate_Code
INNER JOIN [^Estate_City] ON [^Estate].Estate_CityId =[^Estate].Estate_CityId
WHERE
(#CityId IS NULL OR [^Estate].Estate_CityId =#CityId ) AND
(#requestType IS NULL OR [^Estate_OwnerInfo].RequestType_Type = #requestType) AND
([^Estate].Estate_totalPrice BETWEEN #MinPrice AND #MaxPrice) AND
(#bedRoom IS NULL OR [^Estate].Estate_bedRoomCount>#bedRoom) AND
(#PropertyType IS NULL OR [^Estate].Estate_propertyType = #PropertyType) AND
(#requestType IS NULL OR [^Estate_OwnerInfo].RequestType_Type = #requestType) AND
(#bedRoom IS NULL OR [^Estate].Estate_bedRoomCount = #bedRoom) AND
(#Parking IS NULL OR [^Estate_Facility].Facility_Parking = #Parking) AND
(#RemoteParking IS NULL OR [^Estate_Facility].Facility_RemoteParking = #RemoteParking) AND
(#Lobby IS NULL OR [^Estate_Facility].Facility_Lobby = #Lobby)AND
(#Gym IS NULL OR [^Estate_Facility].Facility_Gym = #Gym) AND
(#Surveillance IS NULL OR [^Estate_Facility].Facility_Surveillance = #Surveillance) AND
(#FireAlarm IS NULL OR [^Estate_Facility].Facility_FireAlarm = #FireAlarm) AND
(#FireFighting IS NULL OR [^Estate_Facility].Facility_FireFighting = #FireFighting) AND
(#Pool IS NULL OR [^Estate_Facility].Facility_Pool = #Pool) AND
(#Sauna IS NULL OR [^Estate_Facility].Facility_Sauna = #Sauna) AND
(#Jacuzzi IS NULL OR [^Estate_Facility].Facility_Jacuzzi = #Jacuzzi) AND
(#Carwash IS NULL OR [^Estate_Facility].Facility_Carwash = #Carwash) AND
(#Laundry IS NULL OR [^Estate_Facility].Facility_Laundry = #Laundry) AND
(#Custodian IS NULL OR [^Estate_Facility].Facility_Custodian = #Custodian) AND
(#Shooting IS NULL OR [^Estate_Facility].Facility_Shooting = #Shooting) AND
(#TheftAlarm IS NULL OR [^Estate_Facility].Facility_TheftAlarm = #TheftAlarm) AND
(#PanelFit IS NULL OR [^Estate_Facility].Facility_PanelFit = #PanelFit) AND
(#Sentry IS NULL OR [^Estate_Facility].Facility_Sentry = #Sentry) AND
(#CentralSatellite IS NULL OR [^Estate_Facility].Facility_CentralSatellite = #CentralSatellite) AND
(#CentralAntenna IS NULL OR [^Estate_Facility].Facility_CentralAntenna = #CentralAntenna) AND
(#Fireplaces IS NULL OR [^Estate_Facility].Facility_Fireplaces = #Fireplaces) AND
(#MasterRoom IS NULL OR [^Estate_Facility].Facility_MasterRoom = #MasterRoom) AND
(#Patio IS NULL OR [^Estate_Facility].Facility_Patio = #Patio) AND
(#Barbecue IS NULL OR [^Estate_Facility].Facility_Barbecue = #Barbecue) AND
(#UPS IS NULL OR [^Estate_Facility].Facility_UPS= #UPS) AND
(#RoofGarden IS NULL OR [^Estate_Facility].Facility_RoofGarden = #RoofGarden)
END
END
select * from #results order by Estate_Code;
DECLARE #ErrorCode INT;
DECLARE #Result INT;
SET #ErrorCode = ##ERROR;
IF (#ErrorCode <> 0)
BEGIN
ROLLBACK TRANSACTION;
SET #Result=0;
EXEC Debug_InsertSQLErrorDetails
END
ELSE
BEGIN
COMMIT TRANSACTION;
SET #Result=SCOPE_IDENTITY();
END
END TRY
BEGIN CATCH
SET NOCOUNT OFF
IF ##TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END
EXEC Debug_InsertSQLErrorDetails
SET #Result=0;
END CATCH
END
Please help me to solve this problem thanks!
First of all, a stored procedure will not run multiple times.
The reason could be thatfrom the Estate table you join multiple tables. If these joins are not 1 to 1, but 1 to n, you'll get a result row for each possible join.
Eg, if an Estate has multiple images, you'll get a row for each estate image in your result.
Related
I'm trying to create this stored procedure but it's not working as expected, so when I pass parameters into it I'm not getting results back.
Here is my stored procedure, which is returning results:
BEGIN
DECLARE #sql NVARCHAR(MAX)
SET NOCOUNT ON;
SELECT TOP 100 *
FROM [Test].[dbo].[Order] WITH (NOLOCK)
WHERE [Deleted] = 0
AND ([Id] = #OrderId OR #OrderId = 0)
AND ([StoreId] = #StoreId OR #StoreId = 0)
AND ([WarehouseId] = #WarehouseId OR #WarehouseId = 0)
// if I insert payment status and statement here no results are returned
AND [CreatedOnUtc] >= ISNULL(#CreatedFromUtc, '1/1/1900')
AND [CreatedOnUtc] < ISNULL(#CreatedToUtc, '1/1/2999')
END
If I add this after the warehouseid and statement it stops returning results:
AND ([PaymentStatusId] = #PaymentStatusId OR #PaymentStatusId = 0)
Does anyone know why that is?
its safe use coalesce() function to include those null values.
and iif(#PaymentStatusId = 0, 1, coalesce([PaymentStatusId], 0))
= iif(#PaymentStatusId = 0, 1, #PaymentStatusId)
I'm trying to write a procedure that executes another procedure within it, but I'm getting this error
Incorrect syntax near '#PRIMARY_AM'
but I get this error only with the variables that have a CAST() OR ISNULL() OR REPLACE() function. If I comment out the line with #PRIMARY_AM it will then say
Incorrect syntax near 'ISNULL'
CREATE PROCEDURE [dbo].[RUN_PROCESS]
#PRIMARY_NO VARCHAR(20) = NULL,
#COMBINED_AM INT = NULL,
#PRIMARY_BOR VARCHAR(40) = NULL,
#PRIMARY_AM INT = NULL,
#SECONDARY_AM INT = NULL,
#SECONDARY_DT SMALLDATETIME = NULL,
#PRIMARY_CD VARCHAR(10) = NULL,
#O_ID INT OUTPUT
AS
EXEC dbo.LINK_PROCESS
#PRIMARY_NO = #PRIMARY_NO,
#COMBINED_AM = CAST(#PRIMARY_AM + #SECONDARY_AM AS VARCHAR),
#PRIMARY_BOR = REPLACE(ISNULL(#PRIMARY_BOR, ''), '''',' '),
#PRIMARY_AM = CAST(ISNULL(#PRIMARY_AM, 0) AS VARCHAR),
#SECONDARY_AM = CAST(ISNULL(#SECONDARY_AM, 0) AS VARCHAR),
#SECONDARY_DT = CAST(#SECONDARY_DT AS VARCHAR),
#PRIMARY_CD = ISNULL(#PRIMARY_CD, ''),
#O_ID = #O_ID OUTPUT;
If I remove those functions CAST(), REPLACE() and ISNULL() then I can execute the query without a problem. I don't know why this isn't working.
I'm using SQL Server 2005
This is the solution to my problem, thanks to Alex and his comment under my question
Stored procedures, accept parameters (variables and constants) but not
expressions. You need to do all casting before calling your SP. – Alex
CREATE PROCEDURE [dbo].[RUN_PROCESS]
#PRIMARY_NO VARCHAR(20) = NULL,
#COMBINED_AM INT = NULL,
#PRIMARY_BOR VARCHAR(40) = NULL,
#PRIMARY_AM INT = NULL,
#SECONDARY_AM INT = NULL,
#SECONDARY_DT SMALLDATETIME = NULL,
#PRIMARY_CD VARCHAR(10) = NULL,
#O_ID INT OUTPUT
AS
DECLARE #COMBINED VARCHAR
SET #COMBINED = CAST(#PRIMARY_AM + #SECONDARY_AM AS VARCHAR)
DECLARE #PRIM_BOR VARCHAR(40)
SET #PRIM_BOR = REPLACE(ISNULL(#PRIMARY_BOR, ''), '''',' ')
DECLARE #PRIM_AM VARCHAR
SET #PRIM_AM = CAST(ISNULL(#PRIMARY_AM, 0) AS VARCHAR)
DECLARE #SEC_AM VARCHAR
SET #SEC_AM = CAST(ISNULL(#SECONDARY_AM, 0) AS VARCHAR)
DECLARE #SEC_DT VARCHAR
SET #SEC_DT = CAST(#SECONDARY_DT AS VARCHAR)
DECLARE #PRIM_CD VARCHAR(10)
SET #PRIM_CD = ISNULL(#PRIMARY_CD, '')
EXEC dbo.LINK_PROCESS
#PRIMARY_NO = #PRIMARY_NO,
#COMBINED_AM = #COMBINED,
#PRIMARY_BOR = #PRIM_BOR,
#PRIMARY_AM = #PRIM_AM,
#SECONDARY_AM = #SEC_AM,
#SECONDARY_DT = #SEC_DT,
#PRIMARY_CD = #PRIM_CD,
#O_ID = #O_ID OUTPUT;
I am trying to see if my stored procedure can return -1 some values based on a if condition, snipped stored proc is as follows.
CREATE PROCEDURE [StateMachine].[UpdateSnapshots]
(#SystemName [nvarchar](128)= 'test_system',
#IntrestingEvents VARCHAR(128)='test_event',
#StateMachine_JSON [nvarchar] (max) = 'test_json',
#StateMachine_Object [nvarchar] (max)='test_object',
#CrossbarRouter VARCHAR(128) = 'text_xbar1') AS
BEGIN
DECLARE
#SystemNameValue [nvarchar](128),
#CrossbarValue [nvarchar](128),
#IsCrossbarNotNull int,
#IsCrossbarNull int,
#CrossbarLock int
select #SystemNameValue=count(systemname) from [StateMachine].[Snapshots] with (nolock) where SystemName=#SystemName
IF #SystemNameValue=0
BEGIN
INSERT INTO [StateMachine].[Snapshots] (SystemName, IntrestingEvents, StateMachine_JSON, StateMachine_Object, CrossbarRouter) VALUES (#SystemName, #IntrestingEvents, #StateMachine_JSON, #StateMachine_Object, #CrossbarRouter)
END
select #CrossbarValue = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where (CrossbarRouter=#CrossbarRouter and SystemName = #SystemName)
select #IsCrossbarNotNull = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where CrossbarRouter is not null and SystemName = #SystemName
select #IsCrossbarNull = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where CrossbarRouter is null and SystemName = #SystemName
select #CrossbarLock = isnull(count(CrossbarRouter),0) from [StateMachine].[Snapshots] with (nolock) where (CrossbarRouter != #CrossbarRouter and SystemName = #SystemName)
IF #CrossbarLock = 1
RETURN -1
IF (#CrossbarValue = 0 and #IsCrossbarNotNull = 0) OR #IsCrossbarNull = 0
BEGIN
UPDATE [StateMachine].[Snapshots] SET IntrestingEvents=#IntrestingEvents, StateMachine_JSON=#StateMachine_JSON, StateMachine_Object=#StateMachine_Object, CrossbarRouter = #CrossbarRouter WHERE SystemName=#SystemName;
END
IF #CrossbarValue = 1
BEGIN
UPDATE [StateMachine].[Snapshots] SET IntrestingEvents=#IntrestingEvents, StateMachine_JSON=#StateMachine_JSON, StateMachine_Object=#StateMachine_Object WHERE SystemName=#SystemName and CrossbarRouter=#CrossbarRouter;
END
END
GO
GRANT EXECUTE ON [StateMachine].[UpdateSnapshots] TO sp_executor
If I am trying to do this I am getting following error,
A RETURN statement with a return value cannot be used in this context.
Thanks
So I am making a very simple update procedure, like I have done before, but when I run the execution through it, it does not update. I have looked this over several times and do not see any error in why it wouldn't cause the update to pass through it. My manual updates are working fine, so I am thinking I am either missing something super-obvious or this is something going on with the coalesce function. Any help would be great. The more eyes the merrier.
USE AdventureWorks2012
GO
CREATE PROCEDURE UpdateCreditCard
#CreditCardID INT,
#CardType nvarchar(50) = NULL,
#CardNumber nvarchar(25) = NULL,
#ExpMonth tinyint = NULL,
#ExpYear smallint = NULL
AS
BEGIN
UPDATE [Sales].[CreditCard]
SET
#CardType = COALESCE(#CardType,CardType),
#CardNumber = COALESCE(#CardNumber,CardNumber),
#ExpMonth = COALESCE(#ExpMonth,ExpMonth),
#ExpYear = COALESCE(#ExpYear,ExpYear)
FROM Sales.CreditCard
WHERE #CreditCardID = CreditCardID
END
EXECUTE UpdateCreditCard
#CreditCardID = 19267,
#CardType = 'MasterCard',
#CardNumber = '99999999',
#ExpMonth = 4,
#ExpYear = 2025
You are updating the variables not the columns in CreditCard table.
.....
SET
#CardType = COALESCE(#CardType,CardType), -- here you are updating the #CardType variable
.....
Try this.
CREATE PROCEDURE UpdateCreditCard
#CreditCardID INT,
#CardType nvarchar(50) = NULL,
#CardNumber nvarchar(25) = NULL,
#ExpMonth tinyint = NULL,
#ExpYear smallint = NULL
AS
BEGIN
UPDATE [Sales].[CreditCard]
SET
CardType = COALESCE(#CardType,CardType),
CardNumber = COALESCE(#CardNumber,CardNumber),
ExpMonth = COALESCE(#ExpMonth,ExpMonth),
ExpYear = COALESCE(#ExpYear,ExpYear)
FROM Sales.CreditCard
WHERE CreditCardID=#CreditCardID
END
Ok, I created a Stored Procedure that, among other things, is searching 5 columns for a particular keyword. To accomplish this, I have the keywords parameter being split out by a function and returned as a table. Then I do a Left Join on that table, using a LIKE constraint.
So, I had this working beautifully, and then all of the sudden it stops working. Now it is returning every row, instead of just the rows it needs.
The other caveat, is that if the keyword parameter is empty, it should ignore it.
Given what's below, is there A) a glaring mistake, or B) a more efficient way to approach this?
Here is what I have currently:
ALTER PROCEDURE [dbo].[usp_getOppsPaged]
#startRowIndex int,
#maximumRows int,
#city varchar(100) = NULL,
#state char(2) = NULL,
#zip varchar(10) = NULL,
#classification varchar(15) = NULL,
#startDateMin date = NULL,
#startDateMax date = NULL,
#endDateMin date = NULL,
#endDateMax date = NULL,
#keywords varchar(400) = NULL
AS
BEGIN
SET NOCOUNT ON;
;WITH Results_CTE AS
(
SELECT opportunities.*,
organizations.*,
departments.dept_name,
departments.dept_address,
departments.dept_building_name,
departments.dept_suite_num,
departments.dept_city,
departments.dept_state,
departments.dept_zip,
departments.dept_international_address,
departments.dept_phone,
departments.dept_website,
departments.dept_gen_list,
ROW_NUMBER() OVER (ORDER BY opp_id) AS RowNum
FROM opportunities
JOIN departments ON opportunities.dept_id = departments.dept_id
JOIN organizations ON departments.org_id=organizations.org_id
LEFT JOIN Split(',',#keywords) AS kw ON
(title LIKE '%'+kw.s+'%' OR
[description] LIKE '%'+kw.s+'%' OR
tasks LIKE '%'+kw.s+'%' OR
requirements LIKE '%'+kw.s+'%' OR
comments LIKE '%'+kw.s+'%')
WHERE
(
(#city IS NOT NULL AND (city LIKE '%'+#city+'%' OR dept_city LIKE '%'+#city+'%' OR org_city LIKE '%'+#city+'%'))
OR
(#state IS NOT NULL AND ([state] = #state OR dept_state = #state OR org_state = #state))
OR
(#zip IS NOT NULL AND (zip = #zip OR dept_zip = #zip OR org_zip = #zip))
OR
(#classification IS NOT NULL AND (classification LIKE '%'+#classification+'%'))
OR
((#startDateMin IS NOT NULL AND #startDateMax IS NOT NULL) AND ([start_date] BETWEEN #startDateMin AND #startDateMax))
OR
((#endDateMin IS NOT NULL AND #endDateMax IS NOT NULL) AND ([end_date] BETWEEN #endDateMin AND #endDateMax))
OR
(
(#city IS NULL AND
#state IS NULL AND
#zip IS NULL AND
#classification IS NULL AND
#startDateMin IS NULL AND
#startDateMax IS NULL AND
#endDateMin IS NULL AND
#endDateMin IS NULL)
)
)
)
SELECT *
FROM Results_CTE
WHERE RowNum >= #startRowIndex
AND RowNum < #startRowIndex + #maximumRows;
END
Query -
Can you show us the Previous version that was working?
Are you using any source control to keep track of the changes made?
My Suggestions
Like Predicate with %% seems very expensive and slow process. Is it possible for you to user Full Text Index ?
Declare local variable for each Input Parameter. Assign the corresponding column name to this variable. In case of City, as three columns are being used, so declare three variables for City and assign the corresponding column names, Like below...
Set #Local_city = 'city'
Set #Local_dept_city = 'dept_city'
finally you can use it in the Where clause. Doing this will exclude the below lines of code.
OR
(
(#city IS NULL AND
#state IS NULL AND
#zip IS NULL AND
#classification IS NULL AND
#startDateMin IS NULL AND
#startDateMax IS NULL AND
#endDateMin IS NULL AND
#endDateMin IS NULL)
So, I had this working beautifully, and then all of the sudden it
stops working. Now it is returning every row, instead of just the rows
it needs.
Are you sure about the operators being used in the query? I mean everywhere you have OR operator.
The other caveat, is that if the keyword parameter is empty, it should
ignore it.
Please check my above suggestion.
Final Query
ALTER PROCEDURE [dbo].[usp_getOppsPaged]
#startRowIndex int,
#maximumRows int,
#city varchar(100) = NULL,
#state char(2) = NULL,
#zip varchar(10) = NULL,
#classification varchar(15) = NULL,
#startDateMin date = NULL,
#startDateMax date = NULL,
#endDateMin date = NULL,
#endDateMax date = NULL,
#keywords varchar(400) = NULL
AS
BEGIN
SET NOCOUNT ON;
Declare #Local_city varchar(100)
declare #Local_dept_city varchar(100)
declare #Local_org_city varchar(100)
Declare #Local_state char(2)
Declare #Local_dept_state char(2)
Declare #Local_org_state char(2)
Declare #Local_zip varchar(10)
Declare #Local_dept_zip varchar(10)
Declare #Local_org_zip varchar(10)
Declare #Local_classification varchar(15)
Declare #Local_startDateMin date
Declare #Local_startDateMax date
Declare #Local_endDateMin date
Declare #Local_endDateMin date
Declare #Local_endDateMax date
Set #Local_city = 'city'
Set #Local_dept_city = 'dept_city'
Set #Local_org_city = 'org_city'
Set #Local_state = 'state'
Set #Local_dept_state = 'dept_state'
Set #Local_org_state = 'org_state'
Set #Local_zip = 'zip'
Set #Local_dept_zip = 'dept_zip'
Set #Local_org_zip = 'org_zip'
Set #Local_classification = 'classification'
Set #Local_startDateMax = 'startDateMax'
Set #Local_endDateMin = 'endDateMin'
Set #Local_endDateMin = 'endDateMin'
Set #Local_endDateMax = 'endDateMax'
;WITH Results_CTE AS
(
SELECT opportunities.*,
organizations.*,
departments.dept_name,
departments.dept_address,
departments.dept_building_name,
departments.dept_suite_num,
departments.dept_city,
departments.dept_state,
departments.dept_zip,
departments.dept_international_address,
departments.dept_phone,
departments.dept_website,
departments.dept_gen_list,
ROW_NUMBER() OVER (ORDER BY opp_id) AS RowNum
FROM opportunities
JOIN departments ON opportunities.dept_id = departments.dept_id
JOIN organizations ON departments.org_id=organizations.org_id
LEFT JOIN Split(',',#keywords) AS kw ON
(title LIKE '%'+kw.s+'%' OR
[description] LIKE '%'+kw.s+'%' OR
tasks LIKE '%'+kw.s+'%' OR
requirements LIKE '%'+kw.s+'%' OR
comments LIKE '%'+kw.s+'%')
WHERE
(
(#city IS NOT NULL AND (city LIKE '%'+#Local_city+'%' OR dept_city LIKE '%'+#Local_dept_city+'%' OR org_city LIKE '%'+#Local_org_city+'%'))
OR
(#state IS NOT NULL AND ([state] = #Local_state OR dept_state = #Local_dept_state OR org_state = #Local_org_state))
OR
(#zip IS NOT NULL AND (zip = #Local_zip OR dept_zip = #Local_dept_zip OR org_zip = #Local_org_zip))
OR
(#classification IS NOT NULL AND (classification LIKE '%'+#Local_classification+'%'))
OR
((#startDateMin IS NOT NULL AND #Local_startDateMax IS NOT NULL) AND ([start_date] BETWEEN #Local_startDateMax AND #Local_startDateMax))
OR
((#endDateMin IS NOT NULL AND #Local_endDateMax IS NOT NULL) AND ([end_date] BETWEEN #Local_endDateMin AND #Local_endDateMax))
)
)
SELECT *
FROM Results_CTE
WHERE RowNum >= #startRowIndex
AND RowNum < #startRowIndex + #maximumRows;
END