August 16, 2013 at 7:05 am
CREATE PROCEDURE [dbo].[Procedure_table]
(@records INT OUTPUT,
@location nvarchar(18) = NULL,
@exclude tinyint = NULL,
@exclude2 tinyint = NULL,
@locationName nvarchar(50) = NULL,
@Adult tinyint = NULL, ,
@Youth nvarchar(8) = NULL,
@Case nvarchar(50) = NULL,
@explanation nvarchar(50) = NULL,
@FirstName nvarchar(50) = NULL,
@LastName nvarchar(50) = NULL,
@PhoneNumber nvarchar(50) = NULL,
@orderField nvarchar(50) = NULL,
@orderDir nvarchar(4) = NULL)
AS
If @orderField IS NULL
BEGIN
SET @orderField = 'ID'
END
If @orderDir IS NULL
BEGIN
SET @orderDir = 'ASC'
END
BEGIN
SELECT
ID,
FirstName,
LastName,
PhoneNumber,
Adult,
Youth,
DateParticipation,
DateExit,
Code,
LocationName,
ReportDate,
Explanation
FROM dbo.table
WHERE 1 = 1 AND
Code = ISNULL(@location, Code) AND
Exclusion <= ISNULL(@exclude, Exclusion) AND
Exclusion >= ISNULL(@exclude2, Exclusion) AND
Adult = ISNULL(@Adult, Adult) AND
Youth LIKE ISNULL('%' + @Youth + '%', Youth) AND
LocationName LIKE ISNULL ('%' + @LocationName + '%', LocationName) AND
......
I am currently rewriting a database to learn more about sql, I have hit a point in procedures that I can't piece together.
Any direction much appreciated,
Essentially what this procedure is saying pull ID through explanation from dbo.table, and placing the data in procedure_table. I don't understand what the @variables are after Create Procedure? Once I understand that I can better interpret what the 'WHERE' is truly doing. Thank you.
August 16, 2013 at 7:12 am
The variables after the create statement are input parameters. The user will call the procedure with values assigned to these parameters which are then passed to your query to help return results specific to the values the user seeks.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 16, 2013 at 7:19 am
So there is more input than just dbo.table? How does this work?
August 16, 2013 at 7:26 am
Ryan1 (8/16/2013)
So there is more input than just dbo.table? How does this work?
dbo.Table is not input. That is the data source you are querying.
The input parameters are very simple. I would call the stored procedure in a manner similar to the following.
Execute [dbo].[Procedure_table] @Adult = 1
But you should understand if any of the parameters are required first from the person(s) that wrote it.
Also, the definition of the procedure should be changed to remove the duplicate comma in the following
@Adult tinyint = NULL, ,
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 16, 2013 at 7:35 am
Thank you.
So essentially this procedure is querying dbo.table and whenever it finds a null value it replaces the null value with one of the call variables we initiated the procedure with?
August 16, 2013 at 7:54 am
Ryan1 (8/16/2013)
Thank you.So essentially this procedure is querying dbo.table and whenever it finds a null value it replaces the null value with one of the call variables we initiated the procedure with?
Yes the procedure is querying that table. But no on the null replacement.
Your where clause is taking advantage of the ISNULL to determine if the input parameter is null. If the input is null, then it uses the column value for the relative condition.
If the input parameter is not null, then it uses the input parameter in the relative condition.
for instance
where adult = isnull(@adult,adult) could be rewritten as this pseudo-code)
If @adult is null
then
Where adult = adult
If @adult is not null
then
where adult = @adult
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 16, 2013 at 7:56 am
Thank you very much:w00t:
August 16, 2013 at 9:11 am
You are welcome.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 16, 2013 at 6:33 pm
Let me add to the conversation. Since the procedure's parameters (except for the first one) all have NULL assigned as a default doesn't mean that the parameter will be processed AS IF a null was explicitly passed in (like @Adult = NULL when calling the procedure).
If you SPECIFY the input parameters explicitly as in Jason's example (@Adult = 1, @exclude = 0) it doesn't matter what order they are in. But you can call the procedure with JUST the values IF THEY ARE IN ORDER.
EXEC dbo.Procedure_table 1, 2, 'A', 'b',...etc.
The wrinkle here is that IF the parameters have NULL as the default you can drop them off from the end of the list. So for example, this would work because the values all have a NULL default:
EXEC dbo.Procedure_table NULL,'downtown',1
In such a case the NULLable parameters are just ignored starting from the end of the line. If you need to pick and choose other parameters out of order you will have to do so explicitly or provide values for all even if that value itself is a blank or null.
Now you also present another interesting point you may have overlooked. The first parameter in the list is an OUTPUT parameter. An OUTPUT parameter is just the opposite of all the others. It defines a value that you will use to RECEIVE a value from the stored procedure's process which you can then use as a variable itself for some other process. Getting that value from the output parameter and making use of it is not difficult, but it isn't very obvious how to do that. Google 'using a stored procedure for output' and you'll surely find a tutorial on that topic.
August 16, 2013 at 7:19 pm
Thank you very much.
August 22, 2013 at 7:11 pm
This really helped with understanding. Thanks!
Is there some place you can find stored procedures or queries similar to this.. to help learn by analysis?
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply