Sql Server 2012 Stored procedure query with where clause is null or input parameter - sql

I want to create a stored procedure that uses a an input parameter in the where clause. The parameter will either be text or null.
I.e. suppose I have a table [tbl]
Status val
NULL 158
Acc 155
Acc 152
Null 24
Rej 34
In SQL server the null values can be found by using
WHERE Status is NULL
I also want the stored procedure to be able to handle a case when the parameter equals a specific value.
WHERE Status = #parameter
How do I combine both of these cases? I want something like this.
CREATE PROC test #parameter VARCHAR(10) = ''
SELECT *
FROM [tbl]
IF (#parameter is NULL OR #parameter = '') THEN WHERE STATUS is Null
ELSE WHERE STATUS = #parameter

WHERE (ISNULL(#parameter,'') = '' AND Status IS NULL)
OR Status = #parameter
The top half can only ever evaluate to true if #Status isn't NULL. Otherwise it falls back to the bottom half.
ISNULL(#Status,'') = '' is just the same as saying #Status IS NULL OR #Status = '', and if that's the case then it matches rows where Status IS NULL.
Looking at your pseudocode, you were pretty much there (things I added are in bold):
IF( (#parameter is NULL OR #parameter = '')THEN WHEREANDSTATUS is Null)
ELSE WHERE OR STATUS = #parameter
Just realised this might be better:
WHERE ISNULL(#parameter, '') = ISNULL(Status, '')
It will work the same, but I'm not sure if the ISNULL(Status, '') will stop indexes from being used, so it'd be worth checking the execution plan.

Just include both the condition in your WHERE clause such that it will fetch rows where status having null's or status having specific value
WHERE Status is NULL
OR Status = #parameter

Related

Multi Columns filtration in stored procedure in SQL Server

I am having more controls (assume 10 controls with textbox, dropdown, radio buttons) in my Windows forms application for filtering data which all are not a mandatory, hence user may filter data with 1 control or more.
Now I have to create a stored procedure for filtering the data based on their inputs.
Ex: if user enters some text in 1 textbox control, and left remaining 9 controls with empty data, I have to filter data based on only that textbox which user entered.
If user enters some text in 1 textbox control and 1 dropdown, and left remaining 8 controls with empty data, I have to filter data based on only that textbox and dropdown which user entered.
What am I supposed to do?
In source code:
If user entered/selected text on any control, I am passing values as parameters else i am passing as "null" to remaining all other parameters .
In stored procedure:
I gave all 10 controls parameters to get value from Source Code,based on parameters I am filtering data.
if #Param1=null && #Param2=null && #Param3='SomeText'
begin
sELECT * FROM tABLE1 wHERE TableCOLUMN3=#Param3
END
if #Param1=null && #Param2='SomeText' && #Param3='SomeText'
begin
sELECT * FROM tABLE1 wHERE TableCOLUMN2=#Param2 AND TableCOLUMN3=#Param3
END
Note: I need to filter data with each table column to each parameter , Simply assume #Param1--TableCOLUMN1, #param2--TableCOLUMN2, filter varies depend on parameters text.
If I do like this my stored procedure will be more enormous and very big because I have 10 parameters to check (just for your reference I gave 3 parameters above sample).
What I want is :
Since I gave 10 parameters, based on parameters which having values (some text other than NULL) only I have to filter data by using where condition.
Is there any other way to do this, or does anyone have any other ways for this to do?
As long as you make your params default to null and either don't pass in a value for the params you dont need or pass in dbnull value then you can filter like this
CREATE PROC dbo.SAMPLE
(
#Param1 VARCHAR(255) = NULL,
#Param2 VARCHAR(255) = NULL,
#Param3 VARCHAR(255) = NULL,
#Param4 VARCHAR(255) = NULL,
#Param5 VARCHAR(255) = NULL,
#Param6 VARCHAR(255) = NULL
)
AS
BEGIN
SELECT *
FROM Table1
WHERE (#Param1 IS NULL OR TableCOLUMN1 = #Param1)
AND (#Param2 IS NULL OR TableCOLUMN2 = #Param2)
AND (#Param3 IS NULL OR TableCOLUMN3 = #Param3)
AND (#Param4 IS NULL OR TableCOLUMN4 = #Param4)
AND (#Param5 IS NULL OR TableCOLUMN5 = #Param5)
OPTION (RECOMPILE) -- as JamesZ suggested to prevent caching
END
EXEC dbo.SAMPLE #Param2 = 'SomeText' -- only filter where TableCOLUMN2 = #Param2
I would suggest something like that:
SELECT *
FROM TABLE1
WHERE TableCOLUMN1=ISNULL(#Param1,TableCOLUMN1)
AND TableCOLUMN2=ISNULL(#Param2,TableCOLUMN2)
AND TableCOLUMN3=ISNULL(#Param3,TableCOLUMN3)
AND TableCOLUMN4=ISNULL(#Param4,TableCOLUMN4)
... and so on...
This will filter column1 on a value if you specify param1 otherwise it will use the columnvalue itself which will always be true.
But this will only work if your #Param values were NULL in each case if you won't use them.
If the table is big / you need to use indexes for fetching the rows, the problem with this kind of logic is, that indexes can't really be used. There's basically two ways how you can do that:
Add option (recompile) to the end of the select statement by #Ionic or #user1221684. This will cause the statement to be recompiled every time it is executed, which might be a lot of CPU overhead if it's called often.
Create dynamic SQL and call it using sp_executesql
Example:
set #sql = 'SELECT * FROM TABLE1 WHERE '
if (#Param1 is not NULL) set #sql = #sql + 'TableCOLUMN1=#Param1 AND '
if (#Param2 is not NULL) set #sql = #sql + 'TableCOLUMN2=#Param2 AND '
if (#Param3 is not NULL) set #sql = #sql + 'TableCOLUMN3=#Param3 AND '
-- Note: You're not concatenating the value of the parameter, just it's name
set #sql = #sql + ' 1=1' -- This handles the last 'and'
EXEC sp_executesql #sql,
N'#Param1 varchar(10), #Param2 varchar(10), #Param3 varchar(10)',
#Param1, #Param2, #Param3
As an extra option, you could do some kind of mix between your original idea and totally dynamic one, so that it would have at least the most common search criteria handled so that in can be fetched efficiently.
Normally every parameter will have a default value, for example int will have the default value as zero. So using this you can have the condition. See the exam sql below.
create procedure [dbo].[sp_report_test](
#pParam1 int,
#pParam2 int ,
#pParam3 int,
#pParam4 varchar(50)
)
AS
SELECT
*
FROM [vw_report]
where
(#pParam1 <= 0 or Column1 = #pParam1) and
(#pParam2 <= 0 or Column2 = #pParam2) and
(#pParam3 <= 0 or Column3 = #pParam3) and
(#pParam4 is null or len(#pParam4) <= 0 or Column4 = #pParam4);
GO

Dynamic where clause with set number of columns

I am bringing in arguments from an arbitrary source and there may be 1,2 or 3 args passed that need to go as a where clause in an sql query.
How can I write this query so no matter what args are passed the query will run with as many args as it can. I was thinking:
Passed in: x=1,y=null,z=5
//Do some simple checks and assign local variables when the value are not null.
if(arg == null)
{
arg = (/*some wild card that allows any value to be returned*/)
}
else{
arg = arg
}
Generated clause: WHERE X=localx AND Y=(ANY VALUE) AND Z=localz
What can I use for Y in this example so I can avoid dynamically creating a string with a variable amount of args? I found myself having a really hard time trying to articulate this problem.
I'm going to assume SQL Server. Use the ISNULL command and make all incoming parameters null when not provided.
SELECT *
FROM Table
WHERE ISNULL(#Param1,Field1)=Field1
AND ISNULL(#Param2,Field2)=Field2
AND ISNULL(#Param3,Field3)=Field3
AND ISNULL(#Param4,Field4)=Field4
AND ISNULL(#Param5,Field5)=Field5
A better solution, if any of the fields might have NULL values is:
SELECT *
FROM Table
WHERE (#Param1 is null or #Param1 = Field1) and
(#Param2 is null or #Param2 = Field2) and
(#Param3 is null or #Param3 = Field3) and
(#Param4 is null or #Param4 = Field4) and
(#Param5 is null or #Param5 = Field5)
The main difference between this and the solution that uses isnull is that this version handles NULL values in the fields. The expression isnull(#param1, field1) = field1 returns false when #param1 is NULL and field1 is NULL. The expression #Param1 is null or #Param1 = Field1 returns true when both are NULL.

How do I incorporate "where [binary] is not null" in a T-SQL select statement?

I have a stored procedure that I need to filter rows that have a binary value and return only rows that are not null in the binary column.
When I execute this stored procedure:
create procedure sp_GetGraduatingStudentDataByYear
(
#year nvarchar(4)
)
as
select * from Cohort_Graduation_Student_Data where Exp_Grad_Year = #year and Off_Track != null
go
I get no results.
How can I alter this script to return the rows with a null value in the binary column?
This is not because it's a binary column, but because null in SQL should not be compared to anything. Essentially, Off_Track != null condition filters out all rows - all checks for column = null and column != null always evaluate to false.
Use is not null instead:
select *
from Cohort_Graduation_Student_Data
where Exp_Grad_Year = #year and Off_Track is not null

Returning integer value that represents the first non-null field

I've created the following stored procedure:
ALTER PROCEDURE [dbo].[ExampleSP]
(
#SearchText NVARCHAR(4000),
#ID INT = NULL
)
AS
BEGIN
SET NOCOUNT ON;
SELECT
deID,
deTitle
FROM tblDemo As de
LEFT JOIN tblLinkTable As lnk ON (lnk.ID = de.deID)
WHERE CONTAINS(cstKeywords, #SearchText)
AND ((#ID IS NULL) OR (lnk.ID = #ID))
GROUP BY deID,Title
ORDER BY de.Title
But I also need to be able to find the first field that is not null out of the following table columns:
deIntroText, deCompanyText, deTimetableText and deExampleText
And i need to do this for each record that is returned from the SELECT.
So I realise that i'd need to create a temporary column to store it in and i guess you'd need to use an IF statement like this:
IF deIntroText IS NOT Null
THEN TempFieldToReturn = 1
ELSE IF deCompanyText IS NOT Null
THEN TempFieldToReturn = 2
ELSE IF deTimetableText IS NOT Null
THEN TempFieldToReturn = 3
ELSE IF deExampleText IS NOT Null
THEN TempFieldToReturn = 4
So my question is - what is the best way to achieve this? Any examples would be appreciated.
No real shortcut - just use a CASE expression:
SELECT
/* Other Columns */
CASE
WHEN deIntroText IS NOT Null THEN 1
WHEN deCompanyText IS NOT Null THEN 2
WHEN deTimetableText IS NOT Null THEN 3
WHEN deExampleText IS NOT Null THEN 4
ELSE 5 END as OtherColumn
FROM
/* Rest of query */
This is a Searched CASE - there are actually two variants of CASE. I guessed at 5 if all of the columns are NULL - you might leave off the ELSE 5 portion, if you want a NULL result in such a case.

SQL Data Filtering approach

I have a stored procedure that receives 3 parameters that are used to dynamically filter the result set
create proc MyProc
#Parameter1 int,
#Parameter2 int,
#Paremeter3 int
as
select * from My_table
where
1 = case when #Parameter1 = 0 then 1 when #Parameter1 = Column1 then 1 else 0 end
and
1 = case when #Parameter2 = 0 then 1 when #Parameter2 = Column2 then 1 else 0 end
and
1 = case when #Parameter3 = 0 then 1 when #Parameter3 = Column3 then 1 else 0 end
return
The values passed for each parameter can be 0 (for all items) or non-zero for items matching on specific column.
I may have upwards of 20 parameters (example shown only has 3). Is there a more elegant approach to allow this to scale when the database gets large?
I am using something similar to your idea:
select *
from TableA
where
(#Param1 is null or Column1 = #Param1)
AND (#Param2 is null or Column2 = #Param2)
AND (#Param3 is null or Column3 = #Param3)
It is generally the same, but I used NULLs as neutral value instead of 0. It is more flexible in a sense that it doesn't matter what is the data type of the #Param variables.
I use a slightly different method to some of the ones listed above and I've not noticed any performance hit. Instead of passing in 0 for no filter I would use null and I would force it to be the default value of the parameter.
As we give the parameters default values it makes them optional which lends itself to better readability when your calling the procedure.
create proc myProc
#Parameter1 int = null,
#Parameter2 int = null,
#Paremeter3 int = null
AS
select
*
from
TableA
where
column1 = coalesce(#Parameter1,column1)
and
column2 = coalesce(#Parameter2, column2)
and
column3 = coalesce(#Parameter3,column3)
That said I may well try out the dynamic sql method next time to see if I notice any performance difference
Unfortunately dynamic SQL is the best solution performance/stability wise. While all the other methods commonly used ( #param is not or Col = #param, COALESCE, CASE... ) work, they are unpredictable and unreliable, execution plan can (and will) vary for each execution and you may find yourself spending lots of hours trying to figure out, why your query performs really bad when it was working fine yesterday.