March 19, 2019 at 9:59 am
I need to create a stored procedure with an optional where clause and I'm just not sure how to get it right. Some help would be appreciated.
Here's the guts of it;
CREATE PROCEDURE [dbo].[AppTracker_Milestone_Dates_Browse_SP] @UserName VARCHAR(51), @State VARCHAR(11) AS
SELECT
<pkColumn>
<[Completeness Reviewer Name]>
<[Technical Reviewer Name]>
<[Decision Maker Name]>
<[Decision Date]>
<Other columns...>
FROM (
WHERE
<Optional conditions>
I want to search for the same user name in all three columns. The user name could have a value, be null or it could be an empty string. Additionally, the value supplied for Decision Date would either be NULL or NOT NULL
Thanks in advance for any assistance.
March 19, 2019 at 10:23 am
Something like this:
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE PROCEDURE [dbo].[AppTracker_Milestone_Dates_Browse_SP]
@UserName VARCHAR(51),
@DecisionDate DATE,
@State VARCHAR(11)
AS
SELECT
<pkColumn>
<[Completeness Reviewer Name]>
<[Technical Reviewer Name]>
<[Decision Maker Name]>
<[Decision Date]>
<Other columns...>
FROM (
WHERE
(@UserName IN (<[Completeness Reviewer Name]>, <[Technical Reviewer Name]>, <[Decision Maker Name]>) AND
(@DecisionDate IS NULL OR <[Decision Date]> = @DecisionDate)) <AND|OR>
<Optional conditions>
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
March 19, 2019 at 10:45 am
You should read the Catch-all-query section from this article (and the whole article) to learn the best ways to do what you need.
March 19, 2019 at 8:48 pm
Thank you both for your responses. Perhaps a little bit of back story will help explain what I'm trying to do.
I have an MS Access database (2010) connected to SQL Server (2016).
This is a better pseudo code representation of the view I'm working withSELECT
[DB_Key]
,[Received Date]
,[Assignment Date]
,[Completeness Reviewer Name]
,[Assure Completeness End Date]
,[Technical Reviewer Name]
,[Technical Review End Date]
,[Decision Maker Name]
,[Decision Date]
<Other columns...>
FROM [Milestone_Dates_View]
The default behavior I want for the main form, so as to limit the amount of data retrieved, is to return rows where the user's name is in one or more of the three [XXX Name] columns and the value of [Decision Date] is NULL.
Users need to be able to change the filter to:
On my form I have;
All this to say...
Scott's solution above works in the case where the user's name is supplied but not for the case where the name is NULL or blank.
Neither does it address the;
cases.
I am not committed to the solution being a stored procedure if there's another solution. I'm also going to explore the idea of using a passthrough query from MS Access.
If anyone has any other ideas I'm wide open.
Again; thank you for your time to read and respond to this!
March 20, 2019 at 7:30 am
Sounds a little like you have a cascading parameters issue, in that the available values for a given parameter may be dependent on the choices for a different parameter. That's not going to be very easy in MS Access. SSRS would give you that possibility, but only if the dependency is in a straight line, and that something early in the sequence of dependency CAN'T be dependent on something later in the sequence. Mind you, you CAN do it in MS Access with VBA, but it's gonna be a pain and be a LOT of extra code to re-check all the other parameters a given parameter is dependent on. The key for the resulting query is the technique Scott mentioned, e.g. checking a parameter value in a stored procedure for either NULL or a match to a column. What may not be so easy in the stored procedure is determining how to get the parameters to interact. Although, in the sproc, you don't EVER have to worry about a parameter changing values on you, as by the time you are executing the sproc, the parameter values are set in stone. Thus the sproc only has to contend with a single set of parameter values, and has to just figure it out from their values. Hope that helps...
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
March 20, 2019 at 8:35 am
Steve;
Thanks for your response. I'm not sure I completely understand what you're trying to say...
only if the dependency is in a straight line, and that something early in the sequence of dependency CAN'T be dependent on something later in the sequence.
I'm also not sure what you mean by parameter interaction.
To give you a better understanding of my current approach - here's the code of the subroutine which currently builds the WHERE clause for the query based on the user's choices above.
Private Sub SetFormFilter()
Dim strFilterString As String
'set the user value
If cboUser = "<All Users>" Then 'We aren't using a name filter
strFilterString = ""
ElseIf cboUser = "<No Name>" Then 'This case lists applications where all name fields are NULL or empty strings
strFilterString = "([Completeness Reviewer Name] IS NULL OR LTRIM(RTRIM([Completeness Reviewer Name])) = '')" _
& " AND ([Technical Reviewer Name] IS NULL OR LTRIM(RTRIM([Technical Reviewer Name])) = '')" _
& " AND ([Decision Maker Name] IS NULL OR LTRIM(RTRIM([Decision Maker Name])) = '')"
Else
strFilterString = "([Completeness Reviewer Name] = '" & cboUser _
& "' OR [Technical Reviewer Name] = '" & cboUser _
& "' OR [Decision Maker Name] = '" & cboUser & "')"
End If
'Set the state value
If cboUser = "<All Users>" Then 'We aren't using a name filter so we don't need an AND operator in the filter string
Select Case fraState.Value
Case 2
strFilterString = "[Decision Date] IS NULL"
Case 3
strFilterString = "[Decision Date] IS NOT NULL"
Case Else
strFilterString = ""
End Select
Else
Select Case fraState.Value
Case 2
strFilterString = strFilterString & " AND [Decision Date] IS NULL"
Case 3
strFilterString = strFilterString & " AND [Decision Date] IS NOT NULL"
Case Else
strFilterString = strFilterString
End Select
End If
If Len(strFilterString) = 0 Then
Me.FilterOn = False
Else
Me.Filter = strFilterString
Me.FilterOn = True
End If
End Sub
Here's what the user sees;
Here's the WHERE clause returned by the VBA code corresponding to the case above;
([Completeness Reviewer Name] = 'Scott Fallows' OR [Technical Reviewer Name] = 'Scott Fallows' OR [Decision Maker Name] = 'Scott Fallows') AND [Decision Date] IS NOT NULL
When the user clicks the filter-by control (The down-pointing arrow in the header row) of any of the date columns, the only values listed for filtering are those from rows where the name is the same as the searched name - in this case the list of dates shows those for rows with Scott but not for Jennifer.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply