Download presentation
Presentation is loading. Please wait.
1
Stored Procedures
2
Stored Procedure A stored procedure is a function written in SQL and stored in the database. They are primarily used to store logic and protocols for making changes to the database. SQLite doesn't have stored procedures because they are only needed for databases operating on separate processes or servers from the client. In SQLite's case, the client can directly interact with the database. We will be showing stored procedure with MySQL.
3
Example DELIMITER // CREATE PROCEDURE add_now_to_log() BEGIN INSERT INTO log VALUES (datetime('now')); END // DELIMITER ; CALL add_now_to_log(); The DELIMITER statement says what marks the end of a statement. We can't use the semicolon (";") because that can be used in the procedure. It tells the mysql command line interface not to execute the lines until the delimiter is used again.
4
Variables DECLARE variable_name data_type DEFAULT default_value;
You declare variables prior to using them, and you can have an optional DEFAULT clause to specify the variable's initial state. SET variable_name = value; You can assign a value to a declared variable using the SET statement. SELECT * INTO variable_name FROM table; You can assign also assign the results of a SELECT statement by using SELECT INTO. Session variables (scope lasts for the entire connection) are preceded with = value;
5
Example Variable Use (Within a Stored Procedure)
DECLARE total_products INT DEFAULT 0; SELECT COUNT(*) INTO total_products FROM products; -- Or SET total_products = 123;
6
Parameters in Stored Procedures
Parameters for stored procedures can have one of three modes dictating how it is used: OUT The procedure can set this variable, but can not read it before being set within the procedure IN INOUT Default mode, the caller must set the variable and the procedure can't modify it Combination of the two previous The caller passes the parameter in and the procedure may modify it The procedure works on a copy
7
Example With IN Parameter
DELIMITER // CREATE PROCEDURE get_top_ten(IN section_desired INT) BEGIN UPDATE students SET name = name || '!' WHERE students.section = section_desired; END // DELIMITER ; CALL get_top_ten(1);
8
Example With OUT Parameter
DELIMITER !! CREATE PROCEDURE get_best(OUT best_student VARCHAR(255)) BEGIN SELECT name INTO best_student FROM students ORDER BY grade LIMIT 1; END !! DELIMITER ; CALL -- returns 'Josh Nahum'
9
Example With INOUT Parameter
DELIMITER $$ CREATE PROCEDURE increment (INOUT tally INT(4), IN inc INT(4)) BEGIN SET tally = tally + inc; END $$ DELIMITER ; = 0; CALL 3) -- returns 3
10
Advantages of Stored Procedures
Performance Reusable Stored procedures are compiled and stored in the database. This allows caching and faster performance on repeated calls. Stored procedures can be used by different applications and users. They can be used instead of writing repeated queries from scratch. Less traffic Secure Instead of sending many lengthy SQL queries, the connection can call the stored procedure and get the results in one go. Permissions can be granted to a stored procedure separate from tables/columns, so restricted users can use them.
11
Disadvantages of Stored Procedures
Memory and CPU usage Difficult to maintain Stored procedures add overhead to each connection, which means the database can't support as many connections. Like triggers, stored procedures are less transparent and harder to write, especially if the schema of the database changes. Difficult to debug Most DBMSs don't have good support for traceback and error reporting for stored procedures.
12
How is a stored procedure different from a function?
Stored procedures can return multiple values. Stored procedures need the CALL keyword. Stored procedures can have multiple parameters. You haven't taught us about functions yet.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.