Passing a nullable local variable to a where condition - sql

I have the following SQL function:
CREATE or ALTER FUNCTION [dbo].[GET_BOOK_CODE_BY_AUTHOR_ID]
(
#AUTHOR_ID int
)
RETURNS varchar(50)
AS
BEGIN
DECLARE
#MOT_N_ID int,
#SCO_N_ID int,
#BOOK_CODE varchar(50);
select
#MOT_N_ID = AUT.MOT_N_ID,
#SCO_N_ID = AUT.SCO_N_ID
from AUTHOR AUT
where AUT.AUT_ID = #AUTHOR_ID
SELECT
#BOOK_CODE = (
select BOO.BOO_CH_CODE
from BOOK BOO
where
and BOO.MOT_N_ID = #MOT_N_ID
and BOO.SCO_N_ID = #SCO_N_ID
)
RETURN #BOOK_CODE
END;
GO
The variable #SCO_N_ID can be a null value however and when it returns a null value SQL interprets the condition as:
BOO.SCO_N_ID = null
It is not interpreting it as:
BOO.SCO_N_ID is null
Any idea of how to do this?

I would do this check
"the variable is null or the field is equal to the variable"
and (#SCO_N_ID is null or BOO.SCO_N_ID = #SCO_N_ID)

How about something like this:
CREATE or ALTER FUNCTION [dbo].[GET_BOOK_CODE_BY_AUTHOR_ID]
(
#AUTHOR_ID int
)
RETURNS varchar(50)
AS
BEGIN
DECLARE
#BOOK_CODE varchar(50);
SELECT TOP 1 #BOOK_CODE = VEN_CH_CODE
FROM BOOK BOO
INNER JOIN AUTHOR AUT
ON AUT.MOT_N_ID = BOO.MOT_N_ID
WHERE
BOO.SCO_N_ID = AUT.SCO_N_ID OR
(BOO.SCO_N_ID IS NULL AND AUT.SCO_N_ID IS NULL)
ORDER BY BOO.SCO_N_ID, BOO.MOT_N_ID
RETURN #BOOK_CODE
END;
Note I have joined the tables to make one query, also note I have put a TOP 1 because it might be possible for the query to return more than one result set. If you use TOP 1 then you should have an ORDER BY clause to make sure you return the result you want

There are a few important points to note here:
Firstly, to compare while taking nulls into account, instead of a = b you can use this syntax exists (select a intersect select b). This will compile down to an IS comparison, as shown here, and is very efficient
You can dispense with all the variables and just do a simple join
User-defined scalar functions are very slow. You will get much better performance from an inline Table-Valued Function
CREATE or ALTER FUNCTION [dbo].[GET_BOOK_CODE_BY_AUTHOR_ID]
(
#AUTHOR_ID int
)
RETURNS TABLE
AS RETURN (
select VEN.VEN_CH_CODE
from BOOK BOO
join AUTHOR AUT on BOO.MOT_N_ID = AUT.MOT_N_ID
and exists (select BOO.SCO_N_ID
intersect
select AUT.SCO_N_ID)
where AUT.AUT_ID = #AUTHOR_ID
);
GO
You can use it like this
SELECT *
FROM GET_BOOK_CODE_BY_AUTHOR_ID(101) b;
Or like this
SELECT *
FROM OtherTable t
CROSS APPLY GET_BOOK_CODE_BY_AUTHOR_ID(t.authorId) b;
Or this
SELECT *,
(SELECT * FROM GET_BOOK_CODE_BY_AUTHOR_ID(t.authorId))
FROM OtherTable t;

I like to set both sides to an unavailable code, I will use -99 in this case.
ISNULL(BOO.SCO_N_ID,-99) = ISNULL(#SCO_N_ID,-99)

Related

Create a function that accepts list of ids?

I would like to do create a SQL function like this (pseudocode):
function PeopleInCompanies(companyIds)
SELECT * from Person WHERE CompanyId IN (companyIds)
end function
and call it like this:
define companyIds = 1,2,3
select * from PeopleInCompanies(companyIds)
is it even possible?
You would need to use a table type parameter. Assuming that CompanyID is an int:
CREATE TYPE dbo.ints AS TABLE ([value] int);
GO
CREATE FUNCTION dbo.PeopleInCompanies (#CompanyID dbo.ints READONLY)
RETURNS TABLE
AS RETURN
SELECT P.* --This should be replaced by the columns you need.
FROM dbo.Person P
JOIN #CompanyID CI ON P.CompanyID = CI.[Value];
Then you would call the function using:
DECLARE #CompanyID dbo.ints;
INSERT INTO #CompanyID
VALUES (1),(2),(3);
SELECT *
FROM dbo.PeopleInCompanies(#CompanyID);
SQL Server does not support macro substitution. That said, you have the table type as Gordon and Larnu mentioned, or you can simply parse/split the delimited string
Just another option
Declare #companyIds varchar(max) = '1,2,3'
Select A.*
From Person A
Join string_split(#companyIds,',') B
on A.CompanyID = B.Value

SQL Table Valued Function in Select Statement

SQL is not my best thing but I have been trying to optimize this stored procedure. It had multiple scalar-valued functions that I tried to change to table-valued functions because I read in many places that it's a more efficient way of doing it. And now I have them made but not real sure how to implement or if I maybe just didn't create them correctly.
This is the function I'm calling.
Alter FUNCTION [IsNotSenateActivityTableValue]
(
#ActivityCode int,
#BillId int,
#TextToDisplay varchar(max)
)
returns #T table(result varchar(max))
as
begin
DECLARE #result varchar(max);
declare #countcodes int;
declare #ishousebill int;
select #ishousebill = count(billid)
from BillMaster
where BillID = #BillID and Chamber = 'H'
If (#ishousebill = 0)
begin
SELECT #countcodes = count([ActivityCode])
FROM [HouseCoreData].[dbo].[ActivityCode]
where ActivityDescription not like '%(H)%' and ActivityType = 'S'
and [ActivityCode] = #ActivityCode
if (#countcodes = 0)
begin
set #result = 'test'
end
else
begin
set #result = 'test2'
end
end
else
begin
set #result = #TextToDisplay
end
RETURN
END
And this is how I was trying to call them like this. I would prefer just being able to put them in the top but really anything that works would be good.
SELECT distinct
ActionDates.result as ActionDate
,ActivityDescriptions.result as ActivityDescription
FROM BillWebReporting.vwBillDetailWithSubjectIndex as vw
left outer join [BillWebReporting].[HasHouseSummary] as HasSummary on vw.BillID = HasSummary.BillID
outer APPLY dbo.IsNotSenateActivityDateTableValue(ActivityCode,vw.BillID,[ActionDate]) ActionDates
OUTER APPLY dbo.IsNotSenateActivityTableValue(ActivityCode,vw.BillID,[ActivityDescription]) as ActivityDescriptions
Getting a count just to see if at least one row exists is very expensive. You should use EXISTS instead, which can potentially short circuit without materializing the entire count.
Here is a more efficient way using an inline table-valued function instead of a multi-statement table-valued function.
ALTER FUNCTION dbo.[IsNotSenateActivityTableValue] -- always use schema prefix!
(
#ActivityCode int,
#BillId int,
#TextToDisplay varchar(max)
)
RETURNS TABLE
AS
RETURN (SELECT result = CASE WHEN EXISTS
(SELECT 1 FROM dbo.BillMaster
WHERE BillID = #BillID AND Chamber = 'H'
) THEN #TextToDisplay ELSE CASE WHEN EXISTS
(SELECT 1 FROM [HouseCoreData].[dbo].[ActivityCode]
where ActivityDescription not like '%(H)%'
and ActivityType = 'S'
and [ActivityCode] = #ActivityCode
) THEN 'test2' ELSE 'test' END
END);
GO
Of course it could also just be a scalar UDF...
ALTER FUNCTION dbo.[IsNotSenateActivityScalar] -- always use schema prefix!
(
#ActivityCode int,
#BillId int,
#TextToDisplay varchar(max)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE #result VARCHAR(MAX);
SELECT #result = CASE WHEN EXISTS
(SELECT 1 FROM dbo.BillMaster
WHERE BillID = #BillID AND Chamber = 'H'
) THEN #TextToDisplay ELSE CASE WHEN EXISTS
(SELECT 1 FROM [HouseCoreData].[dbo].[ActivityCode]
where ActivityDescription not like '%(H)%'
and ActivityType = 'S'
and [ActivityCode] = #ActivityCode
) THEN 'test2' ELSE 'test' END
END;
RETURN (#result);
END
GO
Table-valued functions return a table, in which, like any other table, rows have to be inserted.
Instead of doing set #result = ....., do:
INSERT INTO #T (result) VALUES ( ..... )
EDIT: As a side note, I don't really understand the reason for this function to be table-valued. You are essentially returning one value.
First of all UDFs generally are very non-performant. I am not sure about MySQL, but in Sql Server a UDF is recompiled every time (FOR EACH ROW OF OUTPUT) it is executed, except for what are called inline UDFs, which only have a single select statement, which is folded into the SQL of the outer query it is included in... and so is only compiled once.
MySQL does have inline table-valued functions, use it instead... in SQL Server, the syntax would be:
CREATE FUNCTION IsNotSenateActivityTableValue
(
#ActivityCode int,
#BillId int,
#TextToDisplay varchar(max)
)
RETURNS TABLE
AS
RETURN
(
Select case
When y.bilCnt + z.actCnt = 0 Then 'test'
when y.bilCnt = 0 then 'test2'
else #TextToDisplay end result
From (Select Count(billId) bilCnt
From BillMaster
Where BillID = #BillID
And Chamber = 'H') y
Full Join
(Select count([ActivityCode]) actCnt
From [HouseCoreData].[dbo].[ActivityCode]
Where ActivityDescription not like '%(H)%'
And ActivityType = 'S'
And [ActivityCode] = #ActivityCode) z
)
GO

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.

SQL with clause dynamic where parameter

I have a tree-style database with the following structure:
Table fields:
NodeID int
ParentID int
Name varchar(40)
TreeLevel int
I would like to use a variable #NodeID in the first part of the with clause to don't get all the table just start from the piece I'm interested in (see where Parent=#ParentID and comment).
with RecursionTest (NodeID,ParentID,ThemeName)
as
(
--if i remove the where from here it spends too much time (the tree is big)--
select Nodeid,ParentID,Name from TreeTable where ParentID=#ParentID
union all
select T0.Nodeid,
T0.ParentID,
T0.Name
from
TreeTable T0
inner join RecursionTest as R on T0.ParentID = R.NodeID
)
select * from RecursionTest
This throws some errors, but my question is:
Is possible to pass a variable to a with clause ?
Thanks a lot in advance.
Best regards.
Jose
Yes.
declare #ParentID int
set #ParentID = 10;
with RecursionTest (NodeID,ParentID,ThemeName) ....
You could wrap the whole thing up in a parameterised inline TVF as well. Example of this last approach.
CREATE FUNCTION dbo.RecursionTest (#ParentId INT)
RETURNS TABLE
AS
RETURN
(
WITH RecursionTest (NodeID,ParentID,ThemeName)
AS
(
/*... CTE definition goes here*/
)
SELECT NodeID,ParentID,ThemeName
FROM RecursionTest
)
GO
SELECT NodeID,ParentID,ThemeName
FROM dbo.RecursionTest(10)
OPTION (MAXRECURSION 0)
Unfortunately <11g this will throw an ORA-32033 - unsupported column aliasing, as this functionality is not supported < that version

how to return a cell into a variable in sql functions

I want to define a scaler function which in that I'm going to return the result into a variable but I do not know how to do this.
CREATE FUNCTION dbo.Funname ( #param int )
RETURNS INT
AS
declare #returnvar int
select #returnvar = select colname from tablename where someconditions = something
return(#returnvar)
I want to make a function something like the top. I mean the result of the select statement which is:
select colname from tablename where someconditions = something
Is only a single cell and we are sure about it. I want to store it into a variable and return it from the function. How can I implement this thing?
I should probably mention that scalar UDFs do come with a considerable health warning and can cause performance issues depending upon how you use them.
Here's an example though.
CREATE FUNCTION dbo.Funname ( #param INT )
RETURNS INT
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN (SELECT number FROM master.dbo.spt_values WHERE number < #param)
END
In the above example I didn't use a variable as it is redundant. The version with variable is
BEGIN
DECLARE #Result int
SET #Result = (SELECT number FROM master.dbo.spt_values WHERE number < #param)
RETURN #Result
END
For both of the above you would need to be sure the Query returned at most one row to avoid an error at runtime. For example
select dbo.Funname(-1) Returns -32768
select dbo.Funname(0) Returns error "Subquery returned more than 1 value."
An alternative syntax would be
BEGIN
DECLARE #Result int
SELECT #Result = number FROM master.dbo.spt_values WHERE number < #param
RETURN #Result
END
This would no longer raise the error if the subquery returned more than one value but you would just end up with an arbitrary result with no warning - which is worse.
Following Comments I think this is what you need
CREATE FUNCTION dbo.getcustgrade(#custid CHAR(200))
RETURNS INT
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN
( SELECT [cust grade]
FROM ( SELECT customerid,
DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) AS [cust grade]
FROM Orders
GROUP BY CustomerID
)
d
WHERE customerid = #custid
)
END