Changing SQL Stored Procedure for multiple search results - sql

I have some inherited code I need to modify in order to accommodate multiple #ParentFolderID parameter. At present, one ID is passed in. However I will now need to account for several ID's being passed in and returning results from each. Below is the current code. I'm not quite sure what exactly where I would start.
declare #Values xml
declare #ValueAttributeID int
declare #YearAttributeID int
declare #CategoryID int
declare #year int
declare #ParentFolderID int
declare #DealerAttributeID int
set #ParentFolderID = 10646615
set #CategoryID = 10646175
set #YearAttributeID = 3
set #ValueAttributeID = 2
set #year = 2014
set #Values = '<values><value id=''1000104'' /></values>'
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
with Parents
(
dataid
)
as
(
select #ParentFolderID
Union
select child.dataid
from DTree parent (NOLOCK)
inner join DTree child (NOLOCK)
on parent.dataid = child.parentid
where parent.dataid = #ParentFolderID
and child.subtype = 0
)
select doc.name as '#name',
doc.dataId as '#id',
(
select allAtts.AttrID as '#id',
case when ((allAtts.ValInt is null) and (allAtts.ValStr is null))
then cast(allAtts.ValDate as nvarchar(255))
when (allAtts.ValInt is null and allAtts.ValDate is null)
then cast(allAtts.ValStr as nvarchar(255))
when (allAtts.ValDate is null and allAtts.ValStr is null)
then cast(allAtts.ValInt as nvarchar(255))
end as '#val'
from LLAttrData allAtts (NOLOCK)
where allAtts.id = doc.dataid
for xml path('attribute'), TYPE
)
from DTree category (NOLOCK)
inner join LLAttrData value (NOLOCK)
on category.dataid = value.defid
--Changes per environment (value attribute)
and value.AttrID = #ValueAttributeID
--Check for values
inner join #Values.nodes('//value') as A(att)
on A.att.value('#id', 'nvarchar(255)') = value.ValStr
--Changes per environment (year attribute)
inner join LLAttrData y (NOLOCK)
on category.dataid = y.defid
--Changes per environment (year attribute)
and y.AttrID = #YearAttributeID
--Check for year
and year(y.valDate) = #year
inner join DTree doc (NOLOCK)
on value.id = doc.dataid
and y.id = doc.dataid
inner join Parents parent
on parent.dataid = doc.parentid
--Must be associated to the category
where category.dataid = #CategoryID -- This is the hard coded category ID
order by doc.dataid --, allAtts.AttrID
FOR XML PATH('document'), Root('documents')`

Related

Adding parameters to where clause sql

I am something of a beginner to SQL and I am trying to run the following SP.
DECLARE #stringStatus varchar(100)
--Check for status value
IF #Status is NULL
BEGIN
set #stringStatus = ''
END
ELSE
BEGIN
set #stringStatus = ' and ps.Status = ' + CAST(#Status as varchar)
END
select * from Projects p
join projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category) + #stringStatus
The aim of the above is to get all rows if #Status is NULL, and to filter the rows, if a parameter has been assigned to #Status.
#Category (varchar) and #Status (int) are IN paramateres
This works fine when #Status is NULL, i.e, I get all the records. But if I pass a parameter, say, #Status = 2, the execution returns no rows, even though there are a few records available.
First of all, how do I get my desired results? Secondly, is there a better way to do this without an if condition block?
Actually, your result is
select * from something where ps.Category ='some string, containing and ps.Status= inside'
So empty rowset is expected result.
You want something like this (hope status is number, not string)
select * from Projects p
join projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category) and
(#Status is NULL OR ps.Status = #Status)
Ok, here are tests for mistrustful :-)
declare #projects table
(
pid int,
name nvarchar(20),
category int
);
declare #projectstatus table
(
pid int,
Category int,
status int
);
insert into #projects values
(1,'Project 1', 1),(2,'Project 2',1),(3,'Project 3',1),(4,'Project 4',1),(5,'Project 5',1);
insert into #projectstatus values
(1,1,1),(2,1,2),(3,1,3),(4,1,2),(5,1,NULL);
declare #Category int =null;
declare #Status int;
--first of all, do not understand, what is the logic with category
--category in one table should be the same, than in other table or specified?
--ok, you said with category everything is ok, do not test category, test status
--test with null
set #Status=null
select * from #Projects p
join #projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category) and
(#Status is NULL OR ps.Status = #Status)
--test with existing status
set #Status=1
select * from #Projects p
join #projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category) and
(#Status is NULL OR ps.Status = #Status)
--test with not existing status
set #Status=10
select * from #Projects p
join #projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category) and
(#Status is NULL OR ps.Status = #Status)
You can simple put condition in the below way for your desired results
--Check for status value
IF #Status is NULL
BEGIN
select * from Projects p
join projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category)
END
ELSE
BEGIN
select * from Projects p
join projectstatus ps on p.pid = ps.pid
where ps.Category = isnull(#Category, p.Category) + #stringStatus
END
Thanks

Plain parameters slower than creating table parameters

I recently discovered the strange behavior, that a function (procedures possibly too) runs significantly slower, when I provide simple NCHAR parameters rather than table value parameters. After trying around a bit I also found, that the function with NCHAR parameters runs as fast as the table value parameter function if I select the NCHAR parameters into a table variable. Here is some code I've created with this.
Function with parameters:
ALTER FUNCTION [rpt].[ReportMaterial]
(
#dateto NCHAR(8),
#mfrnr NVARCHAR(10),
#vkorg NVARCHAR(4)
)
RETURNS #ReturnValue TABLE
(
MANDT NVARCHAR(3),
MATNR NVARCHAR(18),
MFRPN NVARCHAR(40),
MAKTX NVARCHAR(40),
WERKS NVARCHAR(4),
MTART NVARCHAR(4),
KBETR DECIMAL(11, 2),
KONWA NVARCHAR(5)
)
AS
BEGIN
-- FILL UP
SELECT #mfrnr = REPLICATE('0', 10 - LEN(#mfrnr)) + #mfrnr;
-- OUTPUT
WITH mat AS
(
-- direct assignment
SELECT
ma.MANDT,
ma.MATNR,
ma.MFRPN,
mc.WERKS,
ma.MTART
FROM sap.MARA ma
INNER JOIN sap.MARC mc ON ma.MATNR = mc.MATNR AND ma.MANDT = mc.MANDT AND mc.CDCDELETED = 0
INNER JOIN
(
-- better to use in subselect here
SELECT DISTINCT
WERKS
FROM [sap].[Werks] w
WHERE w.WERKSTYPE = 'D' AND w.VKORG = #vkorg
) w ON w.WERKS = mc.WERKS
WHERE ma.MANDT = N'100' AND ma.CDCDELETED = 0 AND ma.MFRNR = #mfrnr
)
INSERT INTO #ReturnValue (MANDT, MATNR, MFRPN, MAKTX, WERKS, MTART, KBETR, KONWA)
SELECT
t.MANDT,
t.MATNR,
t.MFRPN,
mk.MAKTX,
t.WERKS,
t.MTART,
COALESCE(pp.KBETR, 0) KBETR,
pp.KONWA
FROM
(
SELECT
m.MANDT,
m.MATNR,
m.MFRPN,
m.WERKS,
m.MTART
FROM mat m
UNION ALL
-- material selection (part of a hardbundle)
SELECT
ma.MANDT,
ma.MATNR,
ma.MFRPN,
ms.WERKS,
ma.MTART
FROM sap.MARA ma
INNER JOIN sap.MAST ms ON ms.MATNR = ma.MATNR AND ms.STLAN = N'3' AND ms.MANDT = ma.MANDT
INNER JOIN sap.STKO ko ON ko.STLNR = ms.STLNR AND ko.STLAL = ms.STLAL AND ko.MANDT = ms.MANDT
INNER JOIN sap.STPO po ON po.STLNR = ms.STLNR AND po.MANDT = ms.MANDT
INNER JOIN mat mr ON po.IDNRK = mr.MATNR AND mr.MANDT = po.MANDT -- use only relevant items...
INNER JOIN
(
-- better to use as subselect
SELECT DISTINCT
WERKS
FROM [sap].[Werks] w
WHERE w.WERKSTYPE = 'D' AND w.VKORG = #vkorg
) w ON w.WERKS = ms.WERKS
WHERE ma.MTART = N'ZFER' AND ma.MFRNR = N'0080001373' -- we use only dummy hard bundles (sign for mixed manufacturer bundles)
) t
INNER JOIN sap.MAKT mk ON mk.MATNR = t.MATNR AND mk.SPRAS = N'E' AND mk.MANDT = t.MANDT
LEFT JOIN sap.PurchasePrice pp ON pp.MATNR = t.MATNR AND pp.WERKS = t.WERKS AND pp.FLIFN = N'X' AND #dateto BETWEEN pp.VDATU AND pp.BDATU AND #dateto BETWEEN pp.DATAB AND pp.DATBI;
RETURN
END;
Funtion with parameters selected into table variables first:
ALTER FUNCTION [rpt].[ReportMaterial]
(
#dateto NCHAR(8),
#mfrnr NVARCHAR(10),
#vkorg NVARCHAR(4)
)
RETURNS #ReturnValue TABLE
(
MANDT NVARCHAR(3),
MATNR NVARCHAR(18),
MFRPN NVARCHAR(40),
MAKTX NVARCHAR(40),
WERKS NVARCHAR(4),
MTART NVARCHAR(4),
KBETR DECIMAL(11, 2),
KONWA NVARCHAR(5)
)
AS
BEGIN
-- FILL UP
SELECT #mfrnr = REPLICATE('0', 10 - LEN(#mfrnr)) + #mfrnr;
--> Better performance if using a table to join instead of directly using of parameters
-- CREATE TABLE
DECLARE #keyValue TABLE
(
MFRNR NVARCHAR(10),
VKORG NVARCHAR(4)
);
INSERT INTO #keyValue (MFRNR, VKORG) VALUES (#mfrnr, #vkorg);
-- OUTPUT
WITH mat AS
(
-- direct assignment
SELECT
ma.MANDT,
ma.MATNR,
ma.MFRPN,
mc.WERKS,
ma.MTART
FROM sap.MARA ma
INNER JOIN sap.MARC mc ON ma.MATNR = mc.MATNR AND ma.MANDT = mc.MANDT AND mc.CDCDELETED = 0
INNER JOIN
(
-- better to use in subselect here
SELECT DISTINCT
WERKS
FROM [sap].[Werks] w
INNER JOIN #keyValue kv1 ON kv1.VKORG = w.VKORG
WHERE w.WERKSTYPE = 'D'
) w ON w.WERKS = mc.WERKS
INNER JOIN #keyValue kv2 ON kv2.MFRNR = ma.MFRNR
WHERE ma.MANDT = N'100' AND ma.CDCDELETED = 0
)
INSERT INTO #ReturnValue (MANDT, MATNR, MFRPN, MAKTX, WERKS, MTART, KBETR, KONWA)
SELECT
t.MANDT,
t.MATNR,
t.MFRPN,
mk.MAKTX,
t.WERKS,
t.MTART,
COALESCE(pp.KBETR, 0) KBETR,
pp.KONWA
FROM
(
SELECT
m.MANDT,
m.MATNR,
m.MFRPN,
m.WERKS,
m.MTART
FROM mat m
UNION ALL
-- material selection (part of a hardbundle)
SELECT
ma.MANDT,
ma.MATNR,
ma.MFRPN,
ms.WERKS,
ma.MTART
FROM sap.MARA ma
INNER JOIN sap.MAST ms ON ms.MATNR = ma.MATNR AND ms.STLAN = N'3' AND ms.MANDT = ma.MANDT
INNER JOIN sap.STKO ko ON ko.STLNR = ms.STLNR AND ko.STLAL = ms.STLAL AND ko.MANDT = ms.MANDT
INNER JOIN sap.STPO po ON po.STLNR = ms.STLNR AND po.MANDT = ms.MANDT
INNER JOIN mat mr ON po.IDNRK = mr.MATNR AND mr.MANDT = po.MANDT -- use only relevant items...
INNER JOIN
(
-- better to use as subselect
SELECT DISTINCT
WERKS
FROM [sap].[Werks] w
INNER JOIN #keyValue kv1 ON kv1.VKORG = w.VKORG
WHERE w.WERKSTYPE = 'D'
) w ON w.WERKS = ms.WERKS
WHERE ma.MTART = N'ZFER' AND ma.MFRNR = N'00000000'
) t
INNER JOIN sap.MAKT mk ON mk.MATNR = t.MATNR AND mk.SPRAS = N'E' AND mk.MANDT = t.MANDT
LEFT JOIN sap.PurchasePrice pp ON pp.MATNR = t.MATNR AND pp.WERKS = t.WERKS AND pp.FLIFN = N'X' AND #dateto BETWEEN pp.VDATU AND pp.BDATU AND #dateto BETWEEN pp.DATAB AND pp.DATBI;
RETURN
END;
Does anybody have an Idea, why this is the case? Do I need to leverage this effect by allways selecting parameters into temporary tables/table variables?

Returning column with count of 0

I have a query that looks up a list of documents depending on their department and their status.
DECLARE #StatusIds NVARCHAR(MAX) = '1,2,3,4,5';
DECLARE #DepartmentId NVARCHAR(2) = 'IT';
SELECT ILDPST.name,
COUNT(*) AS TodayCount
FROM dbo.TableA ILDP
LEFT JOIN dbo.TableB ILDPS ON ILDPS.IntranetLoanDealPreStateId = ILDP.IntranetLoanDealPreStateId
LEFT JOIN dbo.TableC ILDPST ON ILDPST.IntranetLoanDealPreStateTypeId = ILDPS.CurrentStateTypeId
WHERE (ILDP.CreatedByDepartmentId = #DepartmentId OR #DepartmentId IS NULL)
AND ILDPS.CurrentStateTypeId IN (
SELECT value
FROM dbo.StringAsIntTable(#StatusIds)
)
GROUP BY ILDPST.name;
This returns the results:
However, I'd also like to be able to return statuses where the TodayCount is equal to 0 (i.e. any status with an id included in #StatusIds should be returned, regardless of TodayCount).
I've tried messing with some unions / joins / ctes but I couldn't quite get it to work. I'm not much of an SQL person so not sure what else to provide that could be useful.
Thanks!
If you want to have all the records from TableC you need to left join all other tables to it, not left join it to the other tables. Also it's best to INNER JOIN the filtering table you create from #StatusIds rather then apply it through INclause. Try this:
DECLARE #StatusIds NVARCHAR(MAX) = '1,2,3,4,5';
DECLARE #DepartmentId NVARCHAR(2) = 'IT';
SELECT ILDPST.Name, COUNT(ILDP.IntranetLoanDealPreStateId) AS TodayCount
FROM (SELECT DISTINCT value FROM dbo.StringAsIntTable(#StatusIds)) StatusIds
INNER JOIN dbo.TableC ILDPST
ON ILDPST.IntranetLoanDealPreStateTypeId = StatusIds.value
LEFT JOIN dbo.TableB ILDPS
ON ILDPS.CurrentStateTypeId = ILDPST.IntranetLoanDealPreStateTypeId
LEFT JOIN dbo.TableA ILDP
ON ILDP.IntranetLoanDealPreStateId = ILDPS.IntranetLoanDealPreStateId
AND (ILDP.CreatedByDepartmentId = #DepartmentId OR #DepartmentId IS NULL)
GROUP BY ILDPST.Name;
Try this instead:
DECLARE #StatusIds NVARCHAR(MAX) = '1,2,3,4,5';
DECLARE #DepartmentId NVARCHAR(2) = 'IT';
SELECT ILDPST.name,
COUNT(ILDP.IntranetLoanDealPreStateId) AS TodayCount
FROM
dbo.TableC ILDPST
LEFT JOIN
dbo.TableB ILDPS ON ILDPST.IntranetLoanDealPreStateTypeId = ILDPS.CurrentStateTypeId
LEFT JOIN
dbo.TableA ILDP ON ILDPS.IntranetLoanDealPreStateId = ILDP.IntranetLoanDealPreStateId
AND (ILDP.CreatedByDepartmentId = #DepartmentId OR #DepartmentId IS NULL)
WHERE
ILDPST.IntranetLoanDealPreStateTypeId
IN (
SELECT value
FROM dbo.StringAsIntTable(#StatusIds)
)
GROUP BY ILDPST.name;
You could use the following function to create a table value for your status id's.
CREATE FUNCTION [dbo].[SplitString]
(
#myString varchar(max),
#deliminator varchar(2)
)
RETURNS
#ReturnTable TABLE
(
[Part] [varchar](max) NULL
)
AS
BEGIN
Declare #iSpaces int
Declare #part varchar(max)
--initialize spaces
Select #iSpaces = charindex(#deliminator,#myString,0)
While #iSpaces > 0
Begin
Select #part = substring(#myString,0,charindex(#deliminator,#myString,0))
Insert Into #ReturnTable(Part)
Select #part
Select #myString = substring(#mystring,charindex(#deliminator,#myString,0)+ len(#deliminator),len(#myString) - charindex(' ',#myString,0))
Select #iSpaces = charindex(#deliminator,#myString,0)
end
If len(#myString) > 0
Insert Into #ReturnTable
Select #myString
RETURN
END
This can now be used as a table that you can LEFT JOIN to.
DECLARE #StatusIds NVARCHAR(MAX) = '1,2,3,4,5';
SELECT * FROM dbo.SplitString(#StatusIds, ',')
It is not tested but give it a try:
;With Cte ( Value ) As
( Select Distinct Value From dbo.StringAsIntTable( #StatusIds ) )
Select
ILDPST.name,
COUNT(*) AS TodayCount
From
dbo.TableC As ILDPST
Inner Join Cte On ( ILDPST.IntranetLoanDealPreStateTypeId = Cte.Value )
Left Join dbo.TableB As ILDPS On ( ILDPST.IntranetLoanDealPreStateTypeId = ILDPS.CurrentStateTypeId )
Left Join dbo.TableA As ILDP On ( ILDPS.IntranetLoanDealPreStateId = ILDP.IntranetLoanDealPreStateId )
And ( ( ILDP.CreatedByDepartmentId = #DepartmentId ) Or ( #DepartmentId Is Null ) )
Group By
ILDPST.name

passing multiple varchar values to parameter in procedure [duplicate]

This question already has answers here:
Passing an array of parameters to a stored procedure
(11 answers)
Closed 8 years ago.
sp_panelistid1 '585','201401','108972',''4','5''
alter procedure sp_panelistid1
(
#branch int,
#yearweak int,
#id int ,
#branchid varchar(10))
as
print #branchid
select f.lydelse as QuestionText, f.id as QuestionID, i.artal as Year, i.vecka as Week, i.id as Intervjuperson,
b.beskrivning as Branch, b.id as BranchID, v.beskrivning as Brand, v.id as BrandID, s.regperson as Buss, f.land as CountryID
, vi.NepaVikt as Weight , cp.CintPanelistId
from fraga f
inner join svar s on s.fraga = f.id
inner join bransch b on b.id = f.bransch
inner join varumarke v on v.id = s.varumarke
inner join intervjuperson i on i.id = s.intervjuperson
inner join vikt vi ON f.Bransch = vi.Bransch AND s.Intervjuperson = vi.Intervjuperson
inner join CintPanelistIntervjuperson cp on s.Intervjuperson=cp.Intervjuperson
where f.bransch = #branch
and (100*i.artal)+i.vecka > #yearweak
and f.land = 1 and f.id=#id
and v.beskrivning in (#branchid)
I need to pass multiple values in #branch id how do I pass parameters such that it works in 'IN condition ' like v.beskrivning in ('4','5','6','7 = Stämmer helt')
If you pass comma separated ids like this - '1, 2, 3' and if you want to use with "In" clause then you have to use Split() function. like this -
SELECT f.lydelse AS QuestionText
,f.id AS QuestionID
,i.artal AS Year
,i.vecka AS Week
,i.id AS Intervjuperson
,b.beskrivning AS Branch
,b.id AS BranchID
,v.beskrivning AS Brand
,v.id AS BrandID
,s.regperson AS Buss
,f.land AS CountryID
,vi.NepaVikt AS Weight
,cp.CintPanelistId
FROM fraga f
INNER JOIN svar s ON s.fraga = f.id
INNER JOIN bransch b ON b.id = f.bransch
INNER JOIN varumarke v ON v.id = s.varumarke
INNER JOIN intervjuperson i ON i.id = s.intervjuperson
INNER JOIN vikt vi ON f.Bransch = vi.Bransch
AND s.Intervjuperson = vi.Intervjuperson
INNER JOIN CintPanelistIntervjuperson cp ON s.Intervjuperson = cp.Intervjuperson
WHERE f.bransch = #branch
AND (100 * i.artal) + i.vecka > #yearweak
AND f.land = 1
AND f.id = #id
AND v.beskrivning IN (SELECT items FROM dbo.split(#branchid, ','))
Note: Split function is not in-built function. Its used defined table-valued function.
and here is the split function.
and here is the function.
CREATE FUNCTION [dbo].[Split] (
#String NVARCHAR(4000)
,#Delimiter CHAR(1)
)
RETURNS #Results TABLE (Items NVARCHAR(4000))
AS
BEGIN
DECLARE #INDEX INT
DECLARE #SLICE NVARCHAR(4000)
-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z
SELECT #INDEX = 1
IF #String IS NULL
RETURN
WHILE #INDEX != 0
BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT #INDEX = CHARINDEX(#Delimiter, #STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF #INDEX != 0
SELECT #SLICE = LEFT(#STRING, #INDEX - 1)
ELSE
SELECT #SLICE = #STRING
-- PUT THE ITEM INTO THE RESULTS SET
INSERT INTO #Results (Items)
VALUES (#SLICE)
-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT #STRING = RIGHT(#STRING, LEN(#STRING) - #INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(#STRING) = 0
BREAK
END
RETURN
END

SQL WHERE ... IN clause with possibly null parameter

I am having some problems with my WHERE clause (using SQL 2008) . I have to create a stored procedure that returns a list of results based on 7 parameters, some of which may be null. The ones which are problematic are #elements, #categories and #edu_id. They can be a list of ids, or they can be null. You can see in my where clause that my particular code works if the parameters are not null. I'm not sure how to code the sql if they are null. The fields are INT in the database.
I hope my question is clear enough. Here is my query below.
BEGIN
DECLARE #elements nvarchar(30)
DECLARE #jobtype_id INT
DECLARE #edu_id nvarchar(30)
DECLARE #categories nvarchar(30)
DECLARE #full_part bit
DECLARE #in_demand bit
DECLARE #lang char(2)
SET #jobtype_id = null
SET #lang = 'en'
SET #full_part = null -- full = 1, part = 0
SET #elements = '1,2,3'
SET #categories = '1,2,3'
SET #edu_id = '3,4,5'
select
jobs.name_en,
parttime.fulltime_only,
jc.cat_id category,
je.element_id elem,
jt.name_en jobtype,
jobs.edu_id minEdu,
education.name_en edu
from jobs
left join job_categories jc
on (jobs.job_id = jc.job_id)
left join job_elements je
on (jobs.job_id = je.job_id)
left join job_type jt
on (jobs.jobtype_id = jt.jobtype_id)
left join education
on (jobs.edu_id = education.edu_id)
left join
(select job_id, case when (jobs.parttime_en IS NULL OR jobs.parttime_en = '') then 1 else 0 end fulltime_only from jobs) as parttime
on jobs.job_id = parttime.job_id
where [disabled] = 0
and jobs.jobtype_id = isnull(#jobtype_id,jobs.jobtype_id)
and fulltime_only = isnull(#full_part,fulltime_only)
-- each of the following clauses should be validated to see if the parameter is null
-- if it is, the clause should not be used, or the SELECT * FROM ListToInt... should be replaced by
-- the field evaluated: ie if #elements is null, je.element_id in (je.element_id)
and je.element_id IN (SELECT * FROM ListToInt(#elements,','))
and jc.cat_id IN (SELECT * FROM ListToInt(#categories,','))
and education.edu_id IN (SELECT * FROM ListToInt(#edu_id,','))
order by case when #lang='fr' then jobs.name_fr else jobs.name_en end;
END
Something like
and (#elements IS NULL OR je.element_id IN
(SELECT * FROM ListToInt(#elements,',')))
and (#categories IS NULL OR
jc.cat_id IN (SELECT * FROM ListToInt(#categories,',')))
....
should do the trick
je.element_id IN (SELECT * FROM ListToInt(#elements,',')) OR #elements IS NULL
that way for each one
Have you tried explicitly comparing to NULL?
and (#elements is null or je.element_id IN (SELECT * FROM ListToInt(#elements,','))
And so on.