Adding parameters to where clause sql - sql-server-2012

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

Related

Outer apply with INSERT statement

I want to do something like this
CREATE TABLE #tempFacilitiesAssociated
(
FacilityID BIGINT,
FacilityName VARCHAR(MAX),
IsPrimary BIT
)
-- Insert statements for procedure here
;WITH CTE_RESULT AS
(
SELECT
usr_id, t.name AS Title,
usr_fname, usr_lname, primaryAddress.add_suburb,
CASE
WHEN primaryAddress.add_suburb = #suburb THEN 1
WHEN t.name = #Title THEN 2
ELSE 3
END AS MatchOrder
FROM
core_users u
LEFT JOIN
RIDE_ct_title t ON t.title_id = u.usr_title
OUTER APPLY
(INSERT INTO #tempFacilitiesAssociated
EXEC dbo.[sp_Common_Get_AllFacilitiesForSupervisor] usr_id, 5
SELECT TOP 1 fa.*
FROM CORE_Facilities f
LEFT JOIN CORE_FacilityAddresses fa ON fac_id = fa.add_owner
WHERE fac_id = (SELECT TOP 1 FacilityID
FROM #tempFacilitiesAssociated
WHERE IsPrimary = 1)) primaryAddress
WHERE
u.usr_fname = #FirstName AND usr_lname = #LastName
)
So, first I want to get all facilities of that user through a stored procedure, and then use it to outer apply and select its suburb
UPDATE
I tried using function instead
CREATE FUNCTION fn_GetAddressForUserFacility
(#UserID BIGINT)
RETURNS #Address TABLE (FacilityID BIGINT,
add_address NVARCHAR(MAX),
add_addressline2 NVARCHAR(MAX),
add_suburb NVARCHAR(MAX)
)
AS
BEGIN
DECLARE #FacilitiesAssociated TABLE
(FacilityID BIGINT,
FacilityName NVARCHAR(MAX),
IsPrimary BIT)
INSERT INTO #FacilitiesAssociated
EXEC dbo.[sp_Common_Get_AllFacilitiesForSupervisor] #UserID, 5
INSERT INTO #Address
SELECT TOP 1
fa.add_owner, fa.add_address, fa.add_addressline2, fa.add_suburb
FROM
CORE_Facilities f
LEFT JOIN
CORE_FacilityAddresses fa ON f.fac_id = fa.add_owner AND add_type = 5
WHERE
fac_id = (SELECT TOP 1 FacilityID
FROM #FacilitiesAssociated
WHERE IsPrimary = 1)
RETURN
END
But now its returning
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.

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

Changing SQL Stored Procedure for multiple search results

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')`

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.

Querying Different Table If First Result is Empty

I'm writing a stored procedure to look in two tables PersonTbl, UserTbl. First search the PersonTbl for an userID and if the userID is there get an email address from the UserTbl and return both. However if the ID is not there then search two other tables (PersonsPendingTbl, UsersPendingTbl) for the ID and email. If the ID is not found once again, return null/nulls. So far this is what I've come up with, but not sure if it's the best way of writing it. Let me know if there's any changes you would recommend;
create PROCEDURE [dbo].[MyNewSP]
#ID VARCHAR(MAX)
AS
DECLARE #userID VARCHAR(50)
DECLARE #Email VARCHAR(100)
DECLARE #currentlyActive CHAR
BEGIN
SELECT
#userID = userTbl.ID ,
#Email = personTbl.EMAIL,
#currentlyActive = 'Y'
FROM
personTbl
INNER JOIN userTbl ON personTbl.person_id = userTbl.person_id
WHERE
( userTbl.ID = #ID )
IF ( #userID != #ID ) --Check to see if null
BEGIN
SELECT #currentlyActive = 'N'
SELECT
upt.ID ,
ppt.EMAIL,
#currentlyActive
FROM
PersonsPendingTbl ppt
INNER JOIN dbo.UsersPendingTbl upt ON ppt.person_id = upt.person_id
WHERE
( upt.ID = #ID )
END
ELSE
BEGIN
SELECT
#userID ,
#Email ,
#currentlyActive
END
END
Make a union of both results, but always pick the first row. If the user is registered as Active AND Inactive, it'll return the Active one:
Select *
from (
SELECT userTbl.ID AS UID, personTbl.EMAIL as email, 'Y' as active
FROM personTbl
JOIN userTbl ON personTbl.person_id = userTbl.person_id
WHERE (userTbl.ID = #ID)
union all
SELECT upt.ID AS UID, ppt.EMAIL as email, 'N' as active
FROM PersonsPendingTbl ppt
INNER JOIN dbo.UsersPendingTbl upt ON ppt.person_id = upt.person_id
WHERE (upt.ID = #ID)) user
limit 0,1
I'm not sure about uniqueness of values between your pending and non-pending table, but this should be close enough to get you going.
select
case
when p.PersonId is null and pp.personPendingId is null then null
else userid
end as userid,
case
when p.PersonId is not null then p.email
when p.PersonId is null and pp.PersonPendingID is not null then pp.email
else null
end as email,
case
when p.PersonId is not null then 'Y'
when p.PersonId is null and pp.PersonPendingID is not null then 'N'
else null
end as CurrentlyActive
from userTbl u
left join PersonTbl p on u.Person_id = p.PersonId
left join userPendingTbl up on u.UserId = up.UserPendingId
left join PersonPendingTbl pp on up.personPendingId = pp.PersonPendingID
where u.UserId = #ID