0% found this document useful (0 votes)
47 views

SQL Procedure, Function and View

This document provides examples of SQL procedures, functions, views, indexes, and triggers. It shows how to: 1) Create procedures that perform inserts or selects with input and output parameters. 2) Define functions that return calculated values based on input parameters, with or without a WHERE clause. 3) Create views to select specific columns from tables. 4) Add indexes to columns to create unique identifiers. 5) Configure triggers to fire before or after data changes and log actions to audit tables.

Uploaded by

Jenna Coleman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

SQL Procedure, Function and View

This document provides examples of SQL procedures, functions, views, indexes, and triggers. It shows how to: 1) Create procedures that perform inserts or selects with input and output parameters. 2) Define functions that return calculated values based on input parameters, with or without a WHERE clause. 3) Create views to select specific columns from tables. 4) Add indexes to columns to create unique identifiers. 5) Configure triggers to fire before or after data changes and log actions to audit tables.

Uploaded by

Jenna Coleman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL Procedure, Function and

View
Procedure
• create procedure mypro(IN val1 varchar(100))
• Begin
• insert into first (col1) values (val1);
• END
• call mypro('Value')
DELIMITER //
CREATE PROCEDURE GetOfficeByCountry(IN UserName VARCHAR(255))
BEGIN
SELECT *
FROM newtable
WHERE user_name = UserName;
END //
DELIMITER ;
DELIMITER //
CREATE PROCEDURE InserIntoMyTableProcedure(INOUT UserName
VARCHAR(255))
BEGIN
INSERT INTO mytable(user_name) values (UserName);
END //
DELIMITER ;

CALL InserIntoMyTableProcedure('UserNameFromProcedure');
Function Without Where
• DELIMITER //
• CREATE FUNCTION CalcIncome ( starting_value INT )
• RETURNS INT
• BEGIN
• DECLARE income INT;
• SET income = 1000;
• SET income = income + starting_value;
• RETURN income;
• END; //

• DELIMITER ;
FUNCTION
DELIMITER //
CREATE FUNCTION CalcIncome ( starting_value INT )
RETURNS INT
BEGIN
DECLARE income INT;
SET income = 0;
label1: WHILE income <= 3000 DO
SET income = income + starting_value;
END WHILE label1;
RETURN income;
END; //

DELIMITER ;
call function

SELECT
user_name, MarksFunction(10)
FROM
newtable WHERE marks=10;
Views
create view <viewName> as select <columnName1>,<columnName2>
from <tableName>;
ALTER VIEW newview AS SELECT uid,uname,password FROM register
drop view ViewName
INDEX
• Create UNIQUE INDEX index_name ON table_name (column_name1,
column_name2…)

• SHOW INDEX FROM database_name.table_name


Trigger
• DELIMITER //
• CREATE TRIGGER before_employee_update
• BEFORE UPDATE ON employees
• FOR EACH ROW
• BEGIN
• INSERT INTO employees_audit
• SET action = 'update',
• employeeNumber = OLD.employeeNumber,
• lastname = OLD.lastname,
• changedat = NOW();
• END//
• DELIMITER ;

You might also like