Professional Documents
Culture Documents
Implementing Stored
Procedures
Vidya Vrat Agarwal. | MCT, MCSD
Overview
Creation
Creation Parsing
Parsing
Entries
Entries into
into sysobjects
sysobjects
and
and syscomments
syscomments tables
tables
Execution
Execution Optimization
(first Optimization
(firsttime
time
or
orrecompile)
recompile)
Compiled
Compiled plan
plan placed
placed in
in
Compilation
Compilation procedure
procedure cache
cache
Advantages of Stored Procedures
Use library
GO
CREATE PROC[EDURE] overdue_books
AS
SELECT *
FROM dbo.loan
WHERE due_date < GETDATE()
GO
UPDATE statement
NOCOUNT option
CREATE
CREATE PROCEDURE
PROCEDURE p_UpdateCategory
p_UpdateCategory
((
@CategoryID
@CategoryID int
int == null,
null,
@CategoryName
@CategoryName varchar(50)
varchar(50)
))
AS
AS
SET
SET NOCOUNT
NOCOUNT ON
ON
UPDATE
UPDATE Categories
Categories
SET
SET Category
Category == @CategoryName
@CategoryName
WHERE
WHERE CategoryID
CategoryID == @CategoryID
@CategoryID
Inserting Data
INSERT Statement
CREATE
CREATE PROCEDURE
PROCEDURE p_InsertCustomer
p_InsertCustomer
((
@FName
@FName varchar(50),
varchar(50),
@LName
@LName varchar(50)
varchar(50)
))
AS
AS
SET
SET NOCOUNT
NOCOUNT ON
ON
INSERT
INSERT INTO
INTO Customers
Customers (FirstName,
(FirstName, LastName)
LastName)
VALUES
VALUES (@FName,
(@FName, @LName)
@LName)
Deleting Data
DELETE Statement
CREATE
CREATE PROCEDURE
PROCEDURE p_DeleteCategory
p_DeleteCategory
((
@CategoryID
@CategoryID int
int == null
null
))
AS
AS
SET
SET NOCOUNT
NOCOUNT ON
ON
DELETE
DELETE FROM
FROM Categories
Categories
WHERE
WHERE CategoryID
CategoryID == @CategoryID
@CategoryID
Returning Values with Output Parameters
CREATE
CREATE PROCEDURE
PROCEDURE mathtutor
mathtutor
Creating
CreatingStored
Stored @m1
@m1 smallint,
smallint,
Procedure
Procedure @m2
@m2 smallint,
smallint,
@result
@result smallint
smallint OUTPUT
OUTPUT
AS
AS
SET
SET @result
@result == @m1
@m1 ** @m2
@m2
Executing
ExecutingStored
Stored DECLARE
Procedure DECLARE @answer
@answer smallint
smallint
Procedure EXECUTE
EXECUTE mathtutor
mathtutor 5,
5, 6,
6, @answer
@answer OUTPUT
OUTPUT
SELECT
SELECT 'The
'The result
result is:
is: '' ,, @answer
@answer
Results
ResultsofofStored
Stored
Procedure
Procedure The
The result
result is:
is: 30
30
OUTPUT Parameter
create proc ps
@age int
as
if (@age > 28) return (1)
select * from student
where age >=@age
return(0)
1. Yes
2. No
Q.4. Return Value, Indicates Success or Failure.?
1. True
2. False
Q.5. The maximum number of parameters in a stored
procedure is _____________ .
Q.6. What will be the output of the following :
EXEC master..xp_cmdshell 'dir c:\mssql7’
Q.7. Name the five stored procedures available
with SQL Server.
Q.8. What are the Mistakes in the given Create Proc
command.
cREATE pROC Pname
myname varchar(20) = PIDel
as
print 'My Name is' + ' ' + @myname
Q.9. The same parameter names can be used in
other stored procedures.
1. True
2. False
Q.10. If the keyword OUTPUT is omitted when the stored
procedure is executed, the stored procedure still executes,
but it does not return a value.
1. True
2. False
Q.11. A junior SQL Server programmer asks you for help. She
executed a SQL statement on the server from her computer.
Before she could verify that it executed properly, she lost network
connectivity. From your computer, you access the server and use
the @@error system function. It returns a value of 0. What does
that indicate about the last statement executed on the server?
1. It stopped executing due to the loss of connectivity.
2. It logged an error number 0.
3. It logged an error with a severity level of 0.
4. It is executed successfully.
Q.12. You are the system administrator of a production
database server. You want to create a stored
procedure to automate certain administrative tasks on
the Finance database. You do not want the stored
procedure to be used in other databases. What type of
stored procedure should you use?
1. System
2. Local
3. Global temporary
4. Extended.
Review
Thank You.