SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Beginner Using Procedures


Beginner Using Procedures

Author
Message
Ryan1
Ryan1
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 130

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.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66607 Visits: 18570
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

Ryan1
Ryan1
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 130
So there is more input than just dbo.table? How does this work?
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66607 Visits: 18570
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

Ryan1
Ryan1
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 130
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?
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66607 Visits: 18570
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

Ryan1
Ryan1
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 130
Thank you very muchw00t
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66607 Visits: 18570
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

Steven Willis
Steven Willis
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2039 Visits: 1721
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.

 
Ryan1
Ryan1
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 130
Thank you very much.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search