Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Beginner Using Procedures Expand / Collapse
Author
Message
Posted Friday, August 16, 2013 7:05 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 6:59 AM
Points: 43, 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.
Post #1485179
Posted Friday, August 16, 2013 7:12 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 12:45 PM
Points: 17,947, Visits: 15,941
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
Post #1485188
Posted Friday, August 16, 2013 7:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 6:59 AM
Points: 43, Visits: 130
So there is more input than just dbo.table? How does this work?
Post #1485193
Posted Friday, August 16, 2013 7:26 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 12:45 PM
Points: 17,947, Visits: 15,941
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
Post #1485199
Posted Friday, August 16, 2013 7:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 6:59 AM
Points: 43, 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?
Post #1485205
Posted Friday, August 16, 2013 7:54 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 12:45 PM
Points: 17,947, Visits: 15,941
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
Post #1485213
Posted Friday, August 16, 2013 7:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 6:59 AM
Points: 43, Visits: 130
Thank you very much
Post #1485215
Posted Friday, August 16, 2013 9:11 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 12:45 PM
Points: 17,947, Visits: 15,941
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
Post #1485253
Posted Friday, August 16, 2013 6:33 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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.

 
Post #1485426
Posted Friday, August 16, 2013 7:19 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 6:59 AM
Points: 43, Visits: 130
Thank you very much.
Post #1485427
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse