Lecture2 DBP2024
Lecture2 DBP2024
-- OR
EXECUTE spAddTwoNumbers @no1=10, @no2=20
-- To Execute Procedure
DECLARE @Result INT
EXECUTE spGetResult 10, 20, @Result OUT
PRINT @Result
Stored Procedure in SQL Server
Example2:Create a stored procedure to get the total number of
employees in the Employee table by Gender.
CREATE PROCEDURE spGetEmployeeCountByGender
@Gender VARCHAR(30), @EmployeeCount INT
OUTPUT
AS
BEGIN
SELECT @EmployeeCount = COUNT(ID)
FROM Employee
WHERE Gender = @Gender
END
-- To Execute Procedure
DECLARE @EmployeeTotal INT
EXECUTE spGetEmployeeCountByGender 'Male', @EmployeeTotal
OUTPUT
PRINT @EmployeeTotal
Stored Procedure in SQL Server
Stored Procedure with Default Values:
Let’s see an example of how to use the stored procedure with default values.
CREATE PROCEDURE spAddNumber(@No1 INT= 100,
@No2 INT)
AS
BEGIN
DECLARE @Result INT
SET @Result = @No1 + @No2
PRINT 'The SUM of the 2 Numbers is: '+
CAST(@Result AS VARCHAR)
END
-- Executing the above procedure:
EXEC spAddNumber 3200, 25
EXEC spAddNumber @No1=200, @No2=25
EXEC spAddNumber @No1=DEFAULT, @No2=25
EXEC spAddNumber @No2=25
Stored Procedure in SQL Server
What are the advantages of using a Stored Procedure in an SQL
Server?
• Execution Plan Retention which Improves the Application
Performance.
• Reduces the Network Traffic.
• Code Re-usability and Better Maintainability.
• Better Security .
Stored Procedure in SQL Server
SQL Server Stored Procedure Return Value
• What is Return Value in SQL Server Stored Procedure?
Whenever we execute a stored procedure in SQL Server, it always
returns an integer status variable indicating the status, usually, zero
indicates success, and non-zero indicates the failure.
Stored Procedure in SQL Server
• Example1:Create a procedure that will count the total number of
employees in the Employee table using return status.
CREATE PROCEDURE spGetTotalCountOfEmployee2
AS
BEGIN
RETURN (SELECT COUNT(ID) FROM Employee)
END
When we execute the spGetEmplloyeeNameById2 Stored Procedure it returns an error stating ‘Conversion failed
when converting the nvarchar value Anurag to data type int.
Stored Procedure in SQL Server
What are the differences between Return Status Value and Output
Parameters in SQL Server Stored Procedure?
Return Status Variable Output parameters
Only integer data type it can Any data type value it can
return return
Only one value More than one value
Use to indicate success or Use to return values like name,
failure age, salary, count, etc.
Stored Procedure in SQL Server
SQL Server Temporary Stored Procedure
• The stored procedures which are created temporarily in a database i.e. the
stored procedures which are not stored permanently in a database are called
temporary stored procedures.
The SQL Server Temporary Stored Procedures are of two types such as
1. Private/Local Temporary Stored Procedure
2. Public/Global Temporary Stored Procedure.
Stored Procedure in SQL Server
What is a Private/Local Temporary Stored Procedure?
When we created the stored procedure by using the # prefix before
the stored procedure name then it is called Local or Private
Temporary Stored Procedure
The Private/Local stored procedures are executed by the connection
which has created it.
These are automatically deleted when the connection created is
closed
Stored Procedure in SQL Server
Example: Creating a Local Temporary Stored Procedure in SQL
Server.
CREATE PROCEDURE #LocalProcedure
AS
BEGIN
PRINT 'This is Local Temporary Procedure'
END
we created a stored procedure or function then the content or text of the function or procedure is going to be saved under
the syscomments system table
Stored Procedure in SQL Server
SQL Server Stored Procedure Encryption and Recompile Attribute:
There are two types of attributes that can be used while creating a
stored procedure in SQL Server, they are as follows
1. With Encryption Attribute
2. With Recompile Attribute
Stored Procedure in SQL Server
The With Encryption Attribute in SQL Server Stored Procedure:
Create the stored procedure using the “With Encryption” option then
you cannot view the text or content of the stored procedure by using
the sp_helptext
Stored Procedure in SQL Server
Example :
--Stored Procedure without using the With Encryption
attribute
SELECT * FROM SYSCOMMENTS WHERE ID =
OBJECT_ID('spDisplayWelcome')
sp_helptext spDisplayWelcome
Stored Procedure in SQL Server
--Stored Procedure using the With Encryption attribute
Alter PROCEDURE spDisplayWelcome
With Encryption
AS
BEGIN
PRINT 'WELCOME TO PROCEDURE in SQL Server'
END
SELECT * FROM SYSCOMMENTS WHERE ID =
OBJECT_ID('spDisplayWelcome')
sp_helptext spDisplayWelcome
Stored Procedure in SQL Server
With Recompiled Attribute in SQL Server Stored Procedure:
• Whenever a procedure is compiled for the first time it prepares the best execution plan
according to the current state of the database. Then it executes that query plan when the
procedure is called.
• If the procedure is created by using the Recompile Attribute. Then it is forced to be compiled
each time it is executed and whenever it compiles it prepares the execution plan.
• Forcing a procedure for recompilation and prepared the execution plan is required when the
database undergoes significant changes to its data or structure.
• Another reason to force a procedure to recompile is if at all the tables are added with new
indexes from which the procedure might be benefited forcing for recompilation is very
important because we cannot wait until the server is restarted for preparing a new query plan.
Stored Procedure in SQL Server
Example
--With Recompiled Attribute in SQL Server Stored
Procedure: