SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in...
Transcript of SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in...
![Page 1: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/1.jpg)
Spring 2020 – University of Virginia 1© Praphamontripong© Praphamontripong
SQL Stored Procedures
CS 4750Database Systems
![Page 2: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/2.jpg)
Spring 2020 – University of Virginia 2© Praphamontripong
Stored Procedures• Allow business logic to be stored in the database and executed from SQL statements
• Pre-defined operations (or functions)
• Can pass input parameters to the procedure
• Can return values
Note:
• If your local phpMyAdmin is the same version as phpMyAdminon the CS server, your local DBMS also supports stored procedures.
• Syntax may vary, depending on DBMS you are using
![Page 3: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/3.jpg)
Spring 2020 – University of Virginia 3© Praphamontripong
How Do Stored Procedure Work?
CREATE PROCEDURE <procedure_name>[(IN|OUT <parameter> <datatype>)]
BEGIN<local-declarations><procedure-body>
END;
CALL <procedure-name> ( <argument-list> )
DROP PROCEDURE [IF EXISTS] <procedure-name>
![Page 4: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/4.jpg)
Spring 2020 – University of Virginia 4© Praphamontripong
Create PROCEDURE (CS Server)• Login to your MySQL account on the CS server
(http://cs4750.cs.virginia.edu/phpMyAdmin/)
• Select the database you would like to work on
• You may create a stored procedure using the Routines feature or write SQL manually
Use Routines feature
![Page 5: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/5.jpg)
Spring 2020 – University of Virginia 5© Praphamontripong
Create PROCEDURE (CS Server)• Select Add routine
![Page 6: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/6.jpg)
Spring 2020 – University of Virginia 6© Praphamontripong
Create PROCEDURE (CS Server)• Enter the
procedure
• Then, click the Gobutton
![Page 7: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/7.jpg)
Spring 2020 – University of Virginia 7© Praphamontripong
Run PROCEDURE (CS Server)• To run the procedure, under your database, select Procedures
• Locate the procedure you would like to run, select Execute
![Page 8: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/8.jpg)
Spring 2020 – University of Virginia 8© Praphamontripong
Create PROCEDURE (CS Server)• Select SQL tab
• Enter SQL statement
• Click the Go button
Use SQL statement
![Page 9: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/9.jpg)
Spring 2020 – University of Virginia 9© Praphamontripong
Run PROCEDURE (CS Server)• Select SQL tab
• Enter SQL statement
• Click the Go button
![Page 10: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/10.jpg)
Spring 2020 – University of Virginia 10© Praphamontripong
Another ExampleCreate a procedure named raiseSalary
Pass two input values and run the procedure
DELIMITER $$CREATE PROCEDURE raiseSalary (IN oldSalary DECIMAL(8,2),
IN newSalary DECIMAL(8,2))UPDATE instructorSET salary = newSalaryWHERE salary = oldSalary;$$DELIMITER ;
SET @p0='62000'; SET @p1='64000'; CALL raiseSalary(@p0, @p1);
![Page 11: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/11.jpg)
Spring 2020 – University of Virginia 11© Praphamontripong
Create PROCEDURE (GCP)• Start your GCP Cloud SQL instance
• Connect to SQL instance (assume, we are using GCP Cloud Shell)
• Login to MySQL
• Enter SQL statement
USE cs4750s20; -- (assume) use a database named cs4750s20
CREATE PROCEDURE countBoats(OUT param1 INT)
NO SQL
SELECT COUNT(*) INTO param1 FROM Boats
$$
![Page 12: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/12.jpg)
Spring 2020 – University of Virginia 12© Praphamontripong
Run PROCEDURE (GCP)
CALL countBoats(@p0); SELECT @p0 AS boats_in_inventory; $$
![Page 13: SQL Stored Procedures · 2020. 6. 3. · Stored Procedures • Allow business logic to be stored in the database and executed from SQL statements • Pre-defined operations (or functions)](https://reader036.fdocuments.in/reader036/viewer/2022081601/61266bbe39b5e6375d0b964f/html5/thumbnails/13.jpg)
Spring 2020 – University of Virginia 13© Praphamontripong
Another ExampleCreate a procedure named raiseSalary
Pass two input values and run the procedure
CREATE PROCEDURE raiseSalary (IN oldSalary DECIMAL(8,2), IN newSalary DECIMAL(8,2))
UPDATE instructorSET salary = newSalaryWHERE salary = oldSalary;$$
SET @p0='62000'; SET @p1='64000'; CALL raiseSalary(@p0, @p1); $$