FlashKeepers

SQL · SQL · College and University

SQL Stored Procedures and Triggers

Writing stored procedures, functions, and triggers, each explained with a short example.

About this set

30 basic cards for SQL SQL. It opens with "What is a stored procedure in SQL?" and "Write the basic syntax to create a stored procedure". More College and University decks are listed below.

30 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What is a stored procedure in SQL?
Show answerA reusable block of SQL statements stored in the database and executed by name
Write the basic syntax to create a stored procedure
Show answerCREATE PROCEDURE name AS BEGIN SELECT * FROM table; END;
What is a user-defined function in SQL?
Show answerA reusable code block that accepts parameters and returns a single value or result set
Difference: scalar function vs table-valued function
Show answerScalar returns one value; table-valued returns multiple rows and columns
Write basic syntax for a scalar function that doubles an integer
Show answerCREATE FUNCTION double(n INT) RETURNS INT AS BEGIN RETURN n * 2; END;
Write basic syntax for a table-valued function
Show answerCREATE FUNCTION nameOfFunction() RETURNS TABLE AS RETURN (SELECT * FROM table);
What is a trigger in SQL?
Show answerAn automatic action executed in response to a specified event (INSERT, UPDATE, DELETE) on a table
Name the three timing events when a trigger can fire
Show answerBEFORE (before the event), AFTER (after the event), INSTEAD OF (replaces the event)
What is the difference between BEFORE and AFTER triggers?
Show answerBEFORE fires before the change is applied; AFTER fires after the change is committed
What does an INSTEAD OF trigger do?
Show answerReplaces the triggering event with a different set of actions instead of executing the original event
Write basic syntax for an INSERT trigger that logs to an audit table
Show answerCREATE TRIGGER auditInsert AFTER INSERT ON table BEGIN INSERT INTO audit VALUES(NEW.id, NOW()); END;
Write basic syntax for an UPDATE trigger
Show answerCREATE TRIGGER auditUpdate AFTER UPDATE ON table BEGIN INSERT INTO log VALUES(OLD.id, NEW.value); END;
Write basic syntax for a DELETE trigger that archives deleted rows
Show answerCREATE TRIGGER archiveDelete AFTER DELETE ON table BEGIN INSERT INTO archive SELECT * FROM OLD; END;
What is a parameter in a stored procedure?
Show answerA variable passed into the procedure at execution time to customize its behavior
Difference: INPUT parameter vs OUTPUT parameter
Show answerINPUT passes data into the procedure; OUTPUT receives data from the procedure back to the caller

15 more cards in the app

Study this set free