Professional Documents
Culture Documents
VIEWS, PROCEDURES
AND TRIGGERS
Views
• Views
– Definition of View
– Creating Views
– Selecting a contents of View
– Modify data using Views
– Aggregate Functions
Definition of View
• Can be defined as virtual tables
• Contains Subset of data from a table or two
or more than two
• Does not store any data
• Picks up the information from the table and
displays it
Creation of View
• Syntax
CREATE VIEW <view_name> AS
SELECT <column_name(s) > FROM
table_name>[, table_name]
• Example
CREATE VIEW emp_view AS
SELECT ename,empno FROM emp
where deptno=30
View must not have same name as base table
or any other view
Creating View From Multiple
Tables
• Example
CREATE VIEW emp_dept_view AS
SELECT empno, ename, deptno, dname FROM
emp e, dept d
WHERE e.deptno=d.deptno
• When View is created from multiple tables
a intermediary table is created to with all
the combinations
• In the above example intermediary table
contains empno, ename, deptno, dname
Selecting View Contents
• Syntax
SELECT <column_name(s)> FROM view_name
WHERE condition
• Example
SELECT ename FROM emp_view where empno=10
•
Modification Through Views
• Data can be modified through views
• Modifications are done at table level
• Update can be done for only one table at a time
• Update cannot be done on view that has a
computed Column
• Example
UPDATE emp_view SET SAL=20 WHERE
ename=‘SMITH’
Aggregation Functions
• Aggregation functions such as MIN, MAX,
AVG etc., can be used views
• If the first select statement has distinct key
word or a column function such as MAX
then view is read only
• View with sub- queries is also ready only
• Example
– CREATE VIEW emp_view1 AS
– SELECT AVG(sal) FROM emp
– GROUP BY deptno
Control Flow Statements
• DECLARE - is used to declare a variable
– Syntax DECLARE <variable>
– Example DECLARE @name
• BEGIN … END - are used to enclose the
program statements. Usually called block
• Every Block should have BEGIN and END
• Syntax
BEGIN
<statements>
<statements>
END
If else statement
• Usually called conditional statement
• Takes Boolean Expression as as argument
• Based on the condition if block is executed
otherwise else block is executed
– Example
DECLARE @age int
SET @age=20
IF @age < 10
BEGIN
Print cast(@age as varchar(20) ) + ‘less than 10’
ELSE
Print cast(@age as varchar(20) ) + ‘greater than 10’
While Loop
• Used to execute a set of statement repeatedly
• Takes Boolean expression as argument
• Remains in the loop as long as expression is True
Syntax
WHILE boolean_expr
BEGIN
<Statements>
<Statements>
END
Break and Continue
• Break statement can be used to exit the loop
unconditionally
• Continue Statement is used to re-evaluate
Boolean Expression and start loop from the
beginning discarding the statements after
continue
WHILE Boolean_expression
BEGIN
Statements
CONTINUE
Statements
BREAK
END
DECLARE @P INT
Declare @ s int
set @s =20
while(@s <30)
begin
set @p=@s +1
continue
Goto, Return and WaitFor
• Goto
– is used to branch to a defined label
– Used for error handling
• Return
– used to stop execution of batch, trigger, procedure
– takes an integer as an argument
– 0 - zero indicates successful execution
– -1 to -99 are reserved numbers
– user should specify out of this range
• WAITFOR
– used to halt the execution for a specified delay
(WAITFOR DELAY) or specified time (WAITFOR
Procedures
• Creating Procedures
• Modification of Procedures
• Procedures with parameters
• Execution of Store Procedures
• System Stored Procedures
• Auto Execution of Store Procedures
Creating A Procedure
• Procedures are created using CREATE
• syntax
CREATE procedure <procedure_name>
AS select statements
• Always created in the current database
• It is executed using EXECUTE statement
• It is like any other high level language
procedure
• CREATE PROCEDURE empproc
AS select * from emp WHERE sal >2000
EXECUTE empproc
Modification Of Procedures
• Procedures can be modified using ALTER
• Syntax of ALTER is same as CREATE but
CREATE is replaced by ALTER
• Example
• ALTER PROCEDURE empproc
• as select * from emp where sal>3000 and
deptno=20
Deletion Of Procedure