Syntax for SQL Server when either of parameter is passed - sql

If one of the paramater is passed / or both passed how to match that to single column in WHERE clause.
If both are passed should get result set just with the first one else with second one.
Ex:
--either will be passed
declare #processStepID int = null
declare #PROCESS_StepID int = null
if(#processStepID is null)
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #PROCESS_StepID
else
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #processStepID
How can this be written in where clause not using if statement.
thanks

WHERE PROCESS_STEP_ID = coalesce( #processStepID, #PROCESS_StepID )

Use the ISNULL function
SELECT *
FROM CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
WHERE PROCESS_STEP_ID = ISNULL( #processStepID, #PROCESS_StepID )

select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where (PROCESS_STEP_ID = #PROCESS_StepID) OR (PROCESS_STEP_ID = #processStepID)
Seems far too obvious. What am I missing?

select *
from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = ISNULL(#PROCESS_StepID,#processStepID)
This should do what you are looking for

Related

Passing a nullable local variable to a where condition

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)

SQL Server: can you execute a stored procedure with an optional parameter? (See code)

I basically want to either return just one patient that is entered through the parameter or return all patients if no value is passed in the parameter. Is this possible with my code? I keep getting this error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
My code:
create proc getOutsideLeadPatients
#patient int = null
as
begin
if #patient is not null
return (select *
from tblpatientdemographics p
where exists (select * from tblDBSSurgery s
where s.idpatient = p.idpatient
and s.blnoutside = 1
and p.idpatient = #patient))
else if #patient is null
return select *
from tblpatientdemographics p
where exists (select * from tblDBSSurgery s
where s.idpatient = p.idpatient
and s.blnoutside = 1)
end
go
Here is a little trick:
create proc getOutsideLeadPatients
#patient int = null
as
begin
select *
from tblpatientdemographics p
where exists (select 1 from tblDBSSurgery s
where s.idpatient = p.idpatient
and s.blnoutside = 1
and p.idpatient = COALESCE(#patient, p.idpatient))
end
go
That is - if #patient is NULL, set it equal to p.idpatient. Then the condition will pass every time.
Perhaps try something along this line
SELECT p.*
FROM tblpatientdemographics p
INNER JOIN tblDBSSurgery s ON s.idpatient = p.idpatient
WHERE (ISNULL(#patient,0) = 0 OR p.idpatient = #patient)
AND s.blnoutside = 1
And as Ben mentioned above, you do not have to use Return to send data back.
Wow all I needed to do was get rid of the returns. Thanks Ben!

Choose multiple cases condition

I want to get multipule choises after then in case statment as
#value
select * from [dbo].[Currency_Tbl]
WHERE [Currency_Active_YN]=
CASE WHEN #value = 1 THEN
( 1 or 0)
ELSE
#Value = 0 then 0
END
it didn't accept the first line in col1 but accept the col2
how can I select multiple numbers after THEN?
You don't use case in where clauses. Use boolean logic
select * from [dbo].[Currency_Tbl]
WHERE (#value = 1 and [Currency_Active_YN] in (0,1))
OR (#value = 0 and [Currency_Active_YN] = 0)
You dont need a case to do what you're trying to do. Assuming Currency_Active_YN is a not null bit field the following logic should suffice.
select * from [dbo].[Currency_Tbl]
WHERE (#value=1 OR [Currency_Active_YN]=#Value)

sql is there a command to check if a row is zero?

I need to check if a row is zero. For nulls I usually use isnull() but my rows are never null. If there is such a command, I would use it like this:
select * from maintable where column1 = iszero(#param,column1)
Assuming your looking for something to work in the same way as IsNull() you could define your own function
create function IsZero(#value int, #default int)
returns int
as
begin
if (#value <> 0)
return #value
return #default
end
Example of usage
select
Returns5 = dbo.Iszero(5, 10),
Returns10 = dbo.Iszero(0, 10)
or in your case
select * from maintable where column1 = IsZero(#param,column1)
SELECT *
FROM maintable
WHERE column1 = CASE #param
WHEN 0 THEN column1
ELSE #param
END
This will give you the record in the maintable with where column1 equals the #param variable. If the #param variable is 0 then it will return all records in the maintable.
Try this:
select *
from maintable
where column1 = #param or #param = 0

Null and not null values in the same parameter

I have this:
Decalre #param int
select * from table where param = #param
sometimes people would like to select records where param is null.
so, something like this:
if #param is null
begin
select * from table where param is null
end
else select * from table where param =#param
Is it possible to incorporate this in a single query?
Should be able to cover it off in a where clause:
select * from table where (param =#param) or (param is null and #param is null)
Would use the coalesce to replace the null value, will act better on performance than the or i think :
SELECT *
FROM table
WHERE Coalesce(#param, 1) = Coalesce(param, 1)
select
*
from table
where (#param is null AND param is null) OR (#param is not null AND param = #param)