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


Passing parameter


Passing parameter

Author
Message
thaya2002
thaya2002
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 44
hi all,
I am new to Stored procedures

Create Procedure JopTransaction
@@Startyear int =null,
@@StartPeriodNo int =null,
...
as
declare @startdate datetime,
declare @enddate datetime


then follows the script

i didn't understand the @@Startyear parameter.
would any one say me what is the meaning of this and what situation do i need to use this type of parameter.

Thanks in advance

Regards,
thaya
SQLBill
SQLBill
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11611 Visits: 1085
There are two ways variables/parameters are used. Normally variables are DECLAREd and the value is supplied as the script runs. The other way is for the value to be supplied BEFORE the script runs. Let's use your example:

Create Procedure JopTransaction
@@Startyear int =null,
@@StartPeriodNo int =null,
...
as
declare @startdate datetime,
declare @enddate datetime


The variables with DECLARE are set during the run time. The other variables (with @@ although they could also have just one @) are set BEFORE the script executes. So, when the script is run, it looks for Startyear and StartPeriodNo values to be provided. If they are not provided, they are set to NULL.

EXEC JobTransaction 2007, 1

That will set @@Startyear to 2007 and @@StartPeriodNo to 1.

EXEC JobTransaction 2007

That will set @@Startyear to 2007, but since there is not a value for @@StartPeriodNo it will be set to NULL.

@Startdate and @enddate will be set to values determined by the script.

When would you use each?

Use the Parameters when you want the user to provide information. For example, the procedure queries a phone book. To find the phone number you need specific information, so you might require the user to input last name and first name. EXEC SearchPhone 'Doe', 'John' or you might require a begin and end date to use for a where clause in a database search.

Internal variables can be used when you have a value that is determined during run time and you need to reuse that value.


//example added after posting//
CREATE PROCEDURE getCompanyAddress
@Emplastname VARCHAR(100),
@Empfirstname VARCHAR(100)
AS
DECLARE @Companyname VARCHAR(100)

SET @Companyname = (SELECT CompanyName FROM TblCompany WHERE EmployeeLName = @Emplastname AND EmployeeFName = @Empfirstname

SELECT CompanyName, Street, City, State, Zipcode
FROM tblCompany
WHERE Companyname = @Company


The user would then run:
EXEC getCompanyInfo 'Doe', 'John'
and get:
Acme 121 Elm Street Chicago IL 99999

-SQLBill



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214048 Visits: 41979
Nice job, Bill.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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