2. T-SQL
T-SQL (Transact-SQL) is a set of
programming extensions from Sybase
and Microsoft that add several
features to the Structured Query
Language (SQL), including transaction
control, exception and error handling,
row processing and declared
variables.
T-SQL (Transact-SQL) is an extension of SQL
language.
3. 1. Stored Procedure
"A stored procedure is a set of Structured
Query Language (SQL) statements with an
assigned name, which are stored in
a relational database management system as
a group, so it can be reused and shared by
multiple programs.”
A stored procedure is simply a compiled database object that
contains one or more T-SQL statements.
T-SQL Statement Examples
4. SQL Statements vs Stored Procedure
First Time
- Check syntax
- Compile
- Execute
- Return data
Second Time
- Check syntax
- Compile
- Execute
- Return data
Creating
- Check syntax
- Compile
First Time
- Execute
- Return data
Second Time
- Execute
- Return data
6. Example 1 (Without Parameters)
Create procedure sp_customerList
AS
BEGIN
SELECT
customer_id,
customer_name,
customer_address
FROM
tbl_customer
ORDER BY
customer_name ASC
END
7. Example 2 (With Parameters)
IF (object_id (‘InsertCustomer’)) is NOT NULL
DROP PROCEDURE InsertCustomer
GO
CREATE PROCEDURE InsertCustomer
@FisrtName varchar(15),
@LastName varchar(15),
@Address varchar(50)
AS
--Sectionn 1: Define and initialize the local variable.
DECLARE @count int
SELECT @count = 0
--Section 2: Determine whether the record already exists
SELECT @count = COUNT(*)
FROM tbl_customer
WHERE FirstName = @FirstName
AND LastName = @LastName
--Section 3: Insert the record if it doesn’t exists
IF (@count = 0)
BEGIN
INSER INTO tbl_customer VALUES
(@FirsstNmae, @LastName, @Address)
PRINT ‘Customer record inserted’
END
ELSE
PRINT ‘Customer record already exists…’
GO
8. How to Execute Stored Procedures
EXEC InsertCustomer
@FirstName = ‘abc’,
@LastName = ‘xyz’,
@Address = ‘Pakistan’
9. Alter Stored Procedure
ALTER PROC[EDURE] procedure_name
[ @parameter_name data_type]
[= default] [OUTPUT]
[,...,n]
AS
t-sql_statement(s)
11. Stored Procedure Offers Many Advantages
• You can avoid having to store all your T-SQL code in files. Stored
procedures are stored in the database itself, so you never have to
search through files to find the code you want to use.
• Stored procedure allows Faster Execution.
• If you have a report that needs to be run frequently, you can create
a stored procedure that produces the report. Anyone who has
access to the database and permission to execute the stored
procedure will be able to produce the report at will.
• Stored Procedure provide better Security to your data.
• Stored procedure reduces network traffic.
13. 2. Trigger?
◦ Triggers are stored programs, which are
automatically executed or fired when some
events occur.
◦ We can define a Trigger as "A Trigger is a
Database object just like a stored procedure
or we can say it is a special kind of Stored
Procedure which fires when an event occurs
in a database.”
◦ User cannot fire the trigger explicitly, it gets
fired implicitly on the basis of certain specified
event.
14. Example of Trigger:
◦ If you write a letter to your friend and
then drop it in a mailbox without putting
enough postage on the envelope, you
trigger the post office to return the
letter to you along with a friendly
reminder that you need to add
postage. This reminder is triggered by
the lack of a stamp; otherwise, the
letter would have gone through the
mail without any delays.
15. Types of Triggers:
◦ Generally there are three types of
triggers available in sequel sever.
i. DML(Data Manipulating Language)
ii. DDL(Data Definition Language)
iii. Logon Triggers
16. DML:
◦ DML triggers run when insert, update or
delete statements modify data in the specified
table or view.
i. Insert : triggers are invoked by insert
statement
ii. Update: triggers are invoked by update
statement
iii. Delete : triggers are invoked by delete
statement
17. DDL:
◦ DDL triggers run when events such as
creating, altering or dropping an object
occurs on the server
◦ Used for database administration
tasks such as auditing and controlling
object access.
18. LOGON TRIGGERS:
◦ Logon triggers fire stored procedures
in response to a LOGON event. This
event is raised when a user session is
established with an instance of SQL
Server. Logon triggers fire after the
authentication phase of logging in
finishes, but before the user session is
actually established.
19. TRIGGERS CAN GET FIRED IN TWO
DIFFERENT MODES:
◦ For/After Trigger: It gets fired only after the sql server
completes all actions successfully on a specified table.
For example on inserting a row on a table, the trigger
defined on the INSERT operation fires only after the
row gets successfully inserted and if the insert fails sql
does not execute the trigger.
◦ Instead of Trigger: It causes the code present in the
trigger to execute instead of the operation that caused
the trigger to fire. If we define INSTEAD OF Trigger on
the above mentioned table, insert would not happen
but the trigger gets fired.
20. SYNTAX OF TRIGGERS:
◦ DML Trigger:
CREATE TRIGGER trigger_name
ON { table | view }
[ WITH ENCRYPTION ]
{
{ {FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
AS
sql_statement [...n ]
}
}
21. ARGUMENTS:
◦ Trigger_name: Is the name of the trigger.
◦ Table | view: Is the table or view on which the trigger is
executed
◦ WITH ENCRYPTION: Code in the trigger will be
encrypted when it is created
◦ AFTER: Specifies that the trigger is fired only when all
operations specified in the triggering SQL statement
have executed successfully
◦ INSTEAD OF: Specifies that the trigger is executed
instead of the triggering SQL statement
◦ { [DELETE] [,] [INSERT] [,] [UPDATE] }: Are keywords
that specify which data modification statements, when
attempted against the table or view, activate the
trigger.
22. DDL TRIGGERS SYNTAX:
CREATE TRIGGER trigger_name ON { ALL
SERVER | DATABASE }
[ WITH ENCRYPTION ]
{
{ {FOR | AFTER }
{ event_type | event_group } [ ,...n ]
AS
sql_statement [...n ]
}
}
23. ARGUMENTS:
◦ DATABASE: If specified, the trigger fires whenever
event_type or event_group occurs in the current
database.
◦ ALL SERVER: If specified, the trigger fires whenever
event_type or event_group occurs anywhere in the
current server.
◦ Event_type: Is the name of a Transact-SQL language
event that, after execution, causes a DDL trigger to fire.
(create, alter, drop)
◦ Event_group: The DDL trigger fires after execution of
any Transact-SQL language event that belongs to
event_group.