Lect 4
Lect 4
Lect 4
Advanced Database II
Lecture 4
Procedures
Triggers
▪ Stored procedures can receive input parameters and they can return
results
▪ Stored procedures can be called from:
▪ Programs written in standard languages, e.g., Java, C#
▪ Scripting languages, e.g., JavaScript, VBScript
▪ SQL command prompt, e.g., SQL*Plus, Query Analyzer
▪ In addition, the output parameters can be in any valid data type e.g., integer, date,
and varying character.
Second, after the SELECT statement, we assigned the number of rows returned by
the query (@@ROWCOUNT) to the @product_count parameter.
Note: that the @@ROWCOUNT is a system variable that returns the number of rows
read by the previous statement.
✓ First, declare variables to hold the values returned by the output parameters
Variable to hold the value of the output
✓ Second, use these variables in the stored procedure call. parameter of the stored procedure
stored procedure
passing the
can call the uspFindProductByModel parameters
stored procedure as follows:
show the value of the @count variable
▪ Triggers are special types of Stored Procedures that are ran automatically by the
database whenever a certain modification (event) occurs.
▪ The modification statements may include
INSERT, UPDATE, and DELETE.
▪ User cannot fire the trigger explicitly , it gets fired implicitly on the basis of certain
specified event
▪ Trigger stored program that is executed by the DBMS whenever a specified event
occurs
▪ Not a stand alone object
▪ Data manipulation language (DML) triggers which are invoked automatically in response to
INSERT, UPDATE, and DELETE events against tables.
▪ Data definition language (DDL) triggers which fire in response to CREATE, ALTER, and
DROP statements. DDL triggers also called to some system stored procedures that perform
DDL-like operations.
▪ The CREATE TRIGGER statement allows you to create a new trigger that is running automatically
whenever an event such as INSERT, DELETE, or UPDATE occurs against a table.
After creating a trigger, we will try to add the following record into
the table1:
INSERT INTO students VALUES (6,'Peter', 'Male');
▪ These events created by the SQL statement that normally starts with one of the following
keywords CREATE, ALTER, DROP, GRANT, DENY, REVOKE, or UPDATE
STATISTICS.
▪ For example, we can write a DDL trigger to log whenever a user issues a CREATE
TABLE or ALTER TABLE statement.
• ENCRYPTION encrypts the Indicates a DDL event that causes the The event_group is a group
definition of the trigger. trigger to fire e.g., CREATE_TABLE, of event_type event such as
ALTER_TABLE DDL_TABLE_EVENTS.
• EXECUTE AS defines the
security context under which the
trigger is executed.
In the following example, DDL trigger safety will fire whenever a DROP_TABLE or ALTER_TABLE
event occurs in the database.
In the following example, a DDL trigger prints a message if any CREATE_DATABASE event occurs
on the current server instance.
▪ 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.
▪ As a result, the SQL Server error log will display all messages created by the trigger,
including error messages and the PRINT statement messages.