90840559 stored procedure
TRANSCRIPT
-
8/22/2019 90840559 Stored Procedure
1/23
Stored Procedure
-
8/22/2019 90840559 Stored Procedure
2/23
Objective
At the end of the session you will be
able to know :
What are Stored Procedures? Create a Stored Procedure
Execute a Stored Procedure
-
8/22/2019 90840559 Stored Procedure
3/23
What are Stored Procedures
A stored procedure is an already
written SQL statement that is savedin the database.
-
8/22/2019 90840559 Stored Procedure
4/23
If you find yourself using the same query over and
over again, it would make sense to put it into a storedprocedure.
When you put this SQL statement in a stored
procedure, you can then run the stored procedurefrom the database's command environment using the
execcommand.
-
8/22/2019 90840559 Stored Procedure
5/23
An example is:
exec usp_displayallusers
The name of the stored procedure is "usp_displayallusers", and"exec" tells SQL Server to execute the code in the storedprocedure.
code inside the stored procedure can be something as simpleas:
SELECT * FROM USERLIST
This "select" statement will return all data in the USERLIST table.
-
8/22/2019 90840559 Stored Procedure
6/23
Writing Your First Stored Procedure
Syntax-
CREATE PROCEDURE [PROCEDURE NAME] AS
Procedure Name is the name of the stored procedure stored in
the database.
&
After the "AS" entry, you will simply enter SQL code as you would
in a regularly query.
-
8/22/2019 90840559 Stored Procedure
7/23
Example
Every stored procedure needs the words "CREATE
PROCEDURE" followed by the name you want to assign to the
stored procedure.
While not required, stored procedure names usually begin withthe prefix "usp_".
CREATE PROCEDURE usp_displayallusers
This tells the database that you are creating a stored procedure
named "usp_displayallusers".
-
8/22/2019 90840559 Stored Procedure
8/23
The next step is to think about variables. Since this is our first
stored procedure together, we wont deal with them yet.
Just keep in mind that they are usually added after the
"CREATE PROCEDURE" line.
Since we dont have variables, the next step is quite simple. Put
the word "AS" beneath the create procedure line.
CREATE PROCEDURE usp_displayallusersAS
-
8/22/2019 90840559 Stored Procedure
9/23
We are telling the database that we want to create a storedprocedure that is called "usp_displayallusers" that ischaracterized by the code that follows.
After the "AS" entry, you will simply enter SQL code as you wouldin a regularly query.
For our first, we will use a SELECT statement:
SELECT * FROM USERLIST
-
8/22/2019 90840559 Stored Procedure
10/23
your stored procedure should look like this:
CREATE PROCEDURE
usp_displayallusers
ASSELECT * FROM USERLIST
To run your stored procedure, use execcommand with SP:
exec usp_displayallusers
-
8/22/2019 90840559 Stored Procedure
11/23
More Sophisticated Stored Procedures
In addition to writing SELECT queries, you are going to want to
insert, update, and delete database records.
Also, you will probably want to pass information from outside thequery. Since inserts and updates require some sort of data input
to be useful, our first topic will be variables.
From there, we will use data stored in variables for inserts and
updates.
-
8/22/2019 90840559 Stored Procedure
12/23
Input Variables
If you are inserting new records, you will need to get the data
from somewhere.
Updating existing records also involves simply gettingthe data.
In both INSERT and UPDATE statements, it is necessary to passdata to the stored procedure.
For INSERT, UPDATE, and SELECT statements you can pass
the data to your stored procedure using variables.
Input variables are essentially "storage" for data that you want to
pass to your stored procedure.
-
8/22/2019 90840559 Stored Procedure
13/23
Variable Declaration
Inside your stored procedure, you will declare variables
at the top of the stored procedure.
You can name a variable most anything you want, though it is
best to stick with meaningful works and abbreviations.
The only real requirement is that you begin your variable with the
"@" symbol.
Here are some examples:@f_name ,
@l_name etc.
-
8/22/2019 90840559 Stored Procedure
14/23
For every data element you want to pass, you will needto declare a variable.
Declaring a variable is quite easy.
You decide on a name and a datatype (integer, text,etc.), and indicate the name and datatype at the top of
the procedure(below the "CREATE PROCEDURE" line).
First, lets create the header information that should be apart of every stored procedure.
-
8/22/2019 90840559 Stored Procedure
15/23
Example with Variables
Create Header information as-
CREATE PROCEDURE usp_adduser
/*We will put the variables in here, later
*/
We will need to create a variable for every value we may need to
pass.
The best way to address this issue is to create a variable forevery column in USERLIST.
-
8/22/2019 90840559 Stored Procedure
16/23
The list below shows the variable and the field with which it isassociated(In USERLIST Table):-
@loginlogin
@pswdpswd@f_namef_name@l_namel_name
@address_1address_1
@address_2address_2
@citycity
@statestate
@zipcodezipcode &@emailemail
-
8/22/2019 90840559 Stored Procedure
17/23
Now our stored procedure step look like
CREATE PROCEDURE usp_adduser
@login
@pswd@f_name@l_name@address_1@address_2@city@state@zipcode@email
-
8/22/2019 90840559 Stored Procedure
18/23
Add Datatypes to each variables
Next, add datatypes to each of the variables.
The datatype assigned to the variable should match the datatypeassigned to the corresponding column in the database,
Separate all variables (except the last one), with a comma.
CREATE PROCEDURE usp_adduser
@login varchar(20),
@pswd varchar(20),@f_name varchar(25),@l_name varchar(35),@address_1 varchar(30),
@address_2 varchar(30),@city varchar(30),
@state char(2),@zipcode char(10),@email varchar(50)
-
8/22/2019 90840559 Stored Procedure
19/23
ADD INSERT COMMAND TO SP
This stored procedure will add a new record to the USERLIST
table, so we should use an INSERT statement. The SQL should
be:
INSERT INTO USERLIST (login, pswd, f_name, l_name, address_1,
address_2, city, state, zipcode, email)
VALUES (sarang', sarang123', Sarang', Dalal', Srinagar',
Kalewadi', Pune,'MH', 411017', [email protected]')
-
8/22/2019 90840559 Stored Procedure
20/23
What does the entire stored procedure look like?Lets pull it all together.
CREATE PROCEDUREusp_adduser
@login varchar(20),@pswd varchar(20),@f_name varchar(25),@l_name varchar(35),@address_1 varchar(30),
@address_2 varchar(30),@city varchar(30),@state char(2),@zipcode char(10),@email varchar(50)
AS
INSERT INTO USERL IST(login, pswd, f_name, l_name, address_1,
address_2, city, state, zipcode, email)
VALUES(@login, @pswd, @f_name, @l_name, @address_1, @address_2,@city, @state, @zipcode, @email)
-
8/22/2019 90840559 Stored Procedure
21/23
Execute Stored Procedure
Now, we have a stored procedure that can accept external data.
What do we do with it? How do we get the data?
Using execcommand
The command will be:
exec usp_adduser
There is still the issue of how to get our data into the stored
procedure.
Otherwise, all those variables will be useless.
To get data into our stored procedure, simply add the information(in single quotes ' ') after the execute statement.
exec usp_adduser ' '
-
8/22/2019 90840559 Stored Procedure
22/23
To Pass many Parameters with execRemember to pass as many parameters as you have variables,otherwise SQL Server will throw an error.
Since we have ten variables, your execute statement should looklike this:
exec usp_adduser ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
Next, lets include the data that we will want to pass tousp_adduser.
Your execute statement will look like:
exec usp_adduser Ashish', Ashish123', Ashish', Bisen', BhootBangla Street', ' ', Pune', 'MH', '02116', [email protected]'
-
8/22/2019 90840559 Stored Procedure
23/23
Thanks
Sarang Dalal