Stored Procedure
A stored procedure in SQL is a group of SQL queries that can be saved and reused multiple times. It is very useful as it reduces the need for rewriting SQL queries. It enhances efficiency, reusability, and security in database management.
Users can also pass parameters to stored procedures so that the stored procedure can act on the passed parameter values.
Stored Procedures are created to perform one or more DML operations on the Database. It is nothing but a group of SQL statements that accepts some input in the form of parameters, performs some task, and may or may not return a value.
Features
Custom Constraints
Hide business Logic
execute Dynamic Query
prevent SQL Injection
CREATE PROCEDURE GetStudent
AS
SELECT * FROM StudentCalling
getstudent
EXECUTE getstudent
EXEC getstudentCREATE PROC getstdbyadd @add VARCHAR(10)
AS
SELECT st_id,st_fname
FROM Student
WHERE St_address =@addCalling
Delete stored procedure
DML With SP
check for foreign key before deleting
Dynamic Stored Procedure
Calling
Output parameter
You can define parameter in sp as Output to update it's value inside stored procedure
Using Output parameters
Built in Stored Procedure
sp_helptext sp_bindrule sp_unbind sp_addtype
Functions and SP
Function
Stored Procedure
Returns a single value, either as a table or as a scalar, always.
Can return zero, a single value, or several values.
Run-time compilation and execution occur for functions.
The database contains stored procedures that have been parsed and compiled.
Only Select statements are allowed. Updating and inserting DML statements are allowed.
Capable of carrying out any action on database objects, such as DML and select statements.
Only input parameters are permitted. Output parameters are not supported.
Both input and output parameters are supported.
Does not permit the usage of Try...Catch blocks are used to handle exceptions.
Allows the use of Try...Catch blocks are used to handle exceptions.
Transactions are not permitted within a function.
A stored procedure can contain transactions.
A function cannot call a stored procedure.
A stored procedure can be called a function.
A Select statement can invoke functions.
Stored procedures can't be accessed by Select/Where or Having statements. To run a stored procedure, use the Execute statement.
In JOIN clauses, functions can be used.
JOIN clauses can't use stored procedures.
___
# Trigger
Its a Special Type of SP
Automatic call
Can't Call
Can't Send Parameters
Fire According Action (insert , update , Delete) Two Types of Triggers
Instead Of
After Instead of prevent the query and fires the trigger instead but after the query executes as normal but trigger fire after the query
Only Trigger if you update course name
Display the server user and date after updating student
Prevent from updating course Table
Enable and Disable Trigger
Built In Tables
There are two built in tables in Microsoft SQL that only can be used inside triggers
**This will display the updated row and the row before update
Prevent From deleting in a certain condition
SP , Triggers
1
Basic
trigger is a stored procedure that runs automatically when various events happen (eg update, insert, delete)
Stored procedures are a pieces of the code in written in PL/SQL to do some specific task
2
Running Methodology
It can execute automatically based on the events
It can be invoked explicitly by the user
3
Parameter
It can not take input as parameter
It can take input as a parameter
4
Transaction statements
we can't use transaction statements inside a trigger
We can use transaction statements like begin transaction, commit transaction, and rollback inside a stored procedure
5
Return
Triggers can not return values
Stored procedures can return values
Last updated