Sometimes you will find that it's better to write a database view instead of a select statement.
Why? For ease of maintenance and reusability.
After develop you application and a requirement came from customer to make a requirement change.
This modification may leads to modify a select statement at all views that use this select.
It will be difficult to go to each view and make the change to the select statement requirement change,
and if the requirement change came after deploy the application you will need to redeploy the application which wast a lot of time.
So it's better to write your queries inside the database as function or procedure or as a database view.
A view may needs to have a parameter such as:
20000 can be a variable that needs to be passed to the view.
Views doesn't accept parameters so as a workaround we will use the database session to store that variable. The database view sql will be:
We need to create a context with name 'Emp_CTX' to set the Salary value,
So I created a procedure called from a package:
Next, call the procedure to set the Salary_Session value :
Why? For ease of maintenance and reusability.
After develop you application and a requirement came from customer to make a requirement change.
This modification may leads to modify a select statement at all views that use this select.
It will be difficult to go to each view and make the change to the select statement requirement change,
and if the requirement change came after deploy the application you will need to redeploy the application which wast a lot of time.
So it's better to write your queries inside the database as function or procedure or as a database view.
A view may needs to have a parameter such as:
CREATE OR REPLACE FORCE VIEW EMPLOYEESVIEW
AS
SELECT ID,NAME FROM EMP WHERE SALARY > 20000;
20000 can be a variable that needs to be passed to the view.
Views doesn't accept parameters so as a workaround we will use the database session to store that variable. The database view sql will be:
CREATE OR REPLACE FORCE VIEW EMPLOYEESVIEW
AS
SELECT ID,NAME FROM EMP WHERE SALARY > SYS_CONTEXT ('Emp_CTX', 'Salary_Session');
We need to create a context with name 'Emp_CTX' to set the Salary value,
So I created a procedure called from a package:
CREATE OR REPLACE
PACKAGE Emp_Session_CTX_PKG
IS
PROCEDURE SET_CONTEXT(
IN_NAME VARCHAR2,
IN_VALUE VARCHAR2);
END Emp_Session_CTX_PKG;
create or replace
PACKAGE BODY Emp_Session_CTX_PKG
IS
GC$SESSION_ID VARCHAR2 (100);
PROCEDURE SET_CONTEXT(
IN_NAME VARCHAR2,
IN_VALUE VARCHAR2)
AS
BEGIN
SELECT SYS_CONTEXT('USERENV', 'SESSIONID') INTO GC$SESSION_ID FROM dual;
DBMS_SESSION.SET_IDENTIFIER (GC$SESSION_ID);
DBMS_SESSION.SET_CONTEXT ('Emp_CTX', IN_NAME, IN_VALUE, USER, GC$SESSION_ID);
END;
Next, call the procedure to set the Salary_Session value :
Emp_Session_CTX_PKG.set_context('Salary_Session', '20000') ;
Know you can make a select on the view after setting the parameter ( Salary_Session) :
SELECT * FROM EMPLOYEESVIEW;
No comments:
Post a Comment