What are triggers?
A trigger is a special stored procedure attached to a specific table. Managed by DBMS, supported by most RDBMS
What are the differences between triggers and stored procedures?

Give examples of triggers.
Why use triggers?
What is the downside of a trigger?
Downside: will slow down triggering operation
(INSERT, UPDATE or DELETE)
What are stored procedures?
Pieces of executable code stored in the database (small programs) Managed by DBMS and supported by most RDBMS
Give examples of stored procedures.
What are the main features of stored procedures?
What is the preferred database access method?
C#, VB.NET, Java, … can form SQL and execute it
This is not recommended
Explain stored procedure body statements.
What are the main features of Selected T-SQL?
How do you interact with stored procedures?
Explain system stored procedures.
Give an example of system stored procedures.
EXEC sp_columns ‘patients’
EXEC sp_columns @table_name=’patients’
EXEC sp_help
sp_help ‘patients’
sp_server_info
What is the structure of a stored procedure?
Give a sample procedure.
Increase and vendor are hard coded, not very flixible.
CREATE PROCEDURE update_items_item_cost
AS
UPDATE items
SET item_cost = item_cost * (1 + 0.25)
WHERE primary_vendor_id = 1;
How would you improve procedure flexibility?
(NOT FLIXIBLE)
PRINT ‘**********Before Update**********’
SELECT * FROM items ORDER BY primary_vendor_id;
EXEC update_items_item_cost;
PRINT ‘**********After Update**********’
SELECT * FROM items ORDER BY primary_vendor_id;
VERSUS
(FLIXIBLE) Increase and vendor are parameters,
What is the ALTER SQL Statement?
Give an example of 3rd parameter conditional updates.
ALTER PROCEDURE update_items_item_cost
@increase DECIMAL(3, 2),
@vendor_id INT,
@update INT = 0 – If @update = 1, perform update, else show what would be updated
AS
BEGIN
IF @update = 1
UPDATE items
SET item_cost = item_cost * (1 + @increase)
WHERE primary_vendor_id = @vendor_id;
ELSE
SELECT item_id, vendor_name, item_cost AS [existing_cost], item_cost * (1 + @increase) AS [proposed_cost]
FROM items
JOIN vendors
ON primary_vendor_id = vendors.vendor_id
WHERE primary_vendor_id = @vendor_id;
END
To test;
PRINT ‘**********Before Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
PRINT ‘**********No Update**********’
EXEC update_items_item_cost @increase=0.25, @vendor_id=1;
PRINT ‘**********After No Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
PRINT ‘**********Update**********’
EXEC update_items_item_cost @increase=0.25, @vendor_id=1, @update=1;
PRINT ‘**********After Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
Give an example of a 4th parameter procedure.
ALTER PROCEDURE update_items_item_cost
@increase DECIMAL(3, 2),
@vendor_id INT,
@update INT = 0, – If @update = 1, perform update, else show what would be updated
@number_of_records INT OUTPUT
AS
DECLARE items_cursor CURSOR
FOR SELECT item_cost
FROM items
WHERE primary_vendor_id = @vendor_id
FOR UPDATE;
DECLARE @item_cost DECIMAL(9, 2);
BEGIN
IF @update = 1
BEGIN
SET @number_of_records = 0;
OPEN items_cursor;
FETCH NEXT FROM items_cursor
INTO @item_cost;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @item_cost = @item_cost * (1 + @increase);
UPDATE items
SET item_cost = @item_cost
WHERE CURRENT OF items_cursor;
SET @number_of_records = @number_of_records + 1;
FETCH NEXT FROM items_cursor
INTO @item_cost;
END
CLOSE items_cursor;
END
ELSE – No Update
SELECT item_id, vendor_name, item_cost AS [existing_cost], item_cost * (1 + @increase) AS [proposed_cost]
FROM items
JOIN vendors
ON primary_vendor_id = vendors.vendor_id
WHERE primary_vendor_id = @vendor_id;
DEALLOCATE items_cursor;
END
Give an example of the previous 4 param sample procedure.
PRINT ‘**********Before Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
– Need to declare a variable to receive output
DECLARE @num_of_rows INT;
PRINT ‘**********No Update**********’
EXEC update_items_item_cost @increase=0.25, @vendor_id=1, @update=0, @number_of_records=@num_of_rows OUTPUT;
SELECT @num_of_rows AS [number_of_rows];
PRINT ‘**********After No Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
PRINT ‘**********Update**********’
EXEC update_items_item_cost @increase=0.25, @vendor_id=1, @update=1, @number_of_records=@num_of_rows OUTPUT;
SELECT @num_of_rows AS [number_of_rows];
PRINT ‘**********After Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
What are variable declarations?
eg.
DECLARE @total_sales
DECIMAL(11,2);
DECLARE @number_customers INT;
DECLARE @error_msg CHAR(10);
DECLARE @order_date DATETIME;
DECLARE @picture VARBINARY(MAX);
How do you assign values to variables declared?
With the Assignment Statement SET.
eg.
SET @record_count = @record_count + 1;
SET @credit_limit = @credit_limit * 1.20;
SET @num_orders = NULL;
SET @max_credit_limit = (SELECT MAX(credit_limit) FROM
customers);
What are the conditional controls?