create or replace context App_Context using Set_Global_Var;
create or replace package Set_Global_Var as
procedure set_ctx (p_Name in varchar2
,p_Value in varchar2
,p_ID in number default null);
-- Only use this procedure if you access attributes against a
-- namespace globally across different schema sessions
procedure Set_Id (p_ID in number);
end Set_Global_Var;
/
create or replace package body Set_Global_Var as
-- object name represented by literal string is defined
-- by the CREATE OR REPLACE CONTEXT DDL COMMAND
v_CTX_NAME constant All_Context.Namespace%type := 'APP_CONTEXT';
procedure Set_Id (p_ID in number) as
begin
dbms_session.set_identifier(p_ID);
end Set_ID;
-- p_ID will only be offered when an identifier has
-- been set using dbms_session.set_identifier so as to
-- read an attribute from a namespace using the identifier
procedure set_ctx (p_Name in varchar2
,p_Value in varchar2
,p_ID in number default null) as
begin
dbms_session.set_context (namespace => v_CTX_NAME
,attribute => p_Name
,value => p_Value
--,client_ID => p_ID -- only set this
-- when .set_identifier is used
);
end set_ctx;
end Set_Global_Var;
/
-- Test, which adds 2 years to the existing date
declare
v_Value varchar2(10) := to_char(add_months(sysdate, 24), 'YYYY');
begin
Set_Global_Var.set_ctx('DEFAULT_YEAR', v_Value);
exception
when others then
dbms_output.put_line('Error : ' || sqlerrm);
end;
/
create or replace view Year_View as
-- The sys_context variable can occur anywhere in the view
select sys_context('APP_CONTEXT', 'DEFAULT_YEAR') as Default_Year
,to_char(sysdate, 'YYYY') as Actual_Year
from dual
/
col Actual_year format 9999
col Default_year format 9999
-- Now test if the current year and the context year differ
select *
from Year_View
/
I use this blog as a dumping ground for various scripts as well as solutions to Maths problems.
Labels
- Alter (2)
- Analytic functions (2)
- AQ (2)
- AWR (4)
- Collections (2)
- Connect By (2)
- Constraints (7)
- Date/Time calculations (3)
- Dynamic SQL (4)
- Explain Plan (1)
- Formatting (3)
- Functions (1)
- Generating Functions (1)
- Indexes (7)
- Insert (3)
- JMS (3)
- Joins (4)
- LOB (3)
- locking (5)
- Partitions (3)
- Performance (10)
- Security (1)
- SQL Plus (3)
- Tabibitosan (1)
- Triggers (1)
- Views (1)
- XML (4)
Script categories
- Performance (10)
- Constraints (7)
- Indexes (7)
- locking (5)
- AWR (4)
- Dynamic SQL (4)
- Joins (4)
- XML (4)
- Date/Time calculations (3)
- Formatting (3)
- Insert (3)
- JMS (3)
- LOB (3)
- Partitions (3)
- SQL Plus (3)
- AQ (2)
- Analytic functions (2)
- Collections (2)
- Connect By (2)
- Explain Plan (1)
- Functions (1)
- Security (1)
- Triggers (1)
- Views (1)
Thursday, 3 November 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment