SlideShare uma empresa Scribd logo
1 de 13
/* web example
CREATE OR REPLACE TRIGGER TEST_SEQ_TRIGGER
BEFORE INSERT ON TESTUSER.EMPLOYEE
FOR EACH ROW
BEGIN
IF :new.SSN IS NULL THEN
SELECT TEST_SEQUENCE.nextval INTO :new.SSN FROM DUAL;
END IF;
END;
web example end
*/
create or replace trigger Work_Center_Insert_Tr
before insert on Work_Center
for each row
DECLARE
v_Work_Center_Id
Work_Center.Work_Center_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Work_Center_Id
FROM dual;
:NEW.Work_Center_Id:=v_Work_Center_Id;
END;
CREATE OR REPLACE
TRIGGER NEW_EVALUATION_TRIGGER
BEFORE INSERT ON EVALUATIONS
FOR EACH ROW
BEGIN
:NEW.evaluation_id := evaluations_sequence.NEXTVAL;
END;
-- The title of the NEW_EVALUATION_TRIGGER pane is in italic
font, indicating that
--the trigger is not yet saved in the database.
--In the CREATE TRIGGER statement, replace NULL with this:
--:NEW.evaluation_id := evaluations_sequence.NEXTVAL
CREATE OR REPLACE TRIGGER TEST_SEQ_TRIGGER
BEFORE INSERT ON TESTUSER.EMPLOYEE
FOR EACH ROW
BEGIN IF :new.SSN IS NULL
THEN SELECT TEST_SEQUENCE.nextval
INTO :new.SSN
FROM DUAL;
END IF;
END;
describe customer;
-- Sample trigger
CREATE OR REPLACE TRIGGER employee_tr
BEFORE INSERT ON employee
FOR EACH ROW
DECLARE v_employee_id employee.employee_id%TYPE;
BEGIN
SELECT employee_ID_SEQ.NEXTVAL
INTO v_employee_id
FROM dual;
:NEW.employee_id:=v_employee_id;
END;
CREATE OR REPLACE TRIGGER dept_tr
BEFORE INSERT ON departments
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
--/*Test it using the automatic and manual population methods.
/*SQL> INSERT INTO departments (description)
2 VALUES ('Development');
1 row created.
*/
SELECT * FROM departments;
ID DESCRIPTION
---------- --------------------------------------------------
1 Development
1 row selected.
SQL> INSERT INTO departments (id, description)
2 VALUES (dept_seq.NEXTVAL, 'Accounting');
1 row created.
SQL> SELECT * FROM departments;
ID DESCRIPTION
---------- --------------------------------------------------
1 Development
2 Accounting
*/
--
=================================================================
===========================
-- Trigger 1
==
describe Customer;
insert into Customer
(
NAME
,streET_ADDRESS
,CITY
,STATE
,ZIP
,YTD_PURCHASES
,CREDIT_LIMIT
,OUTSTANDING_BALANCE
,DISCOUNT_PERCENT
,LAST_UPDATED_BY
,LAST_UPDATE_DATE
)
values
(
'Amy Cashier'
,'548 Main Street'
,'Louisville'
,'KY'
,'50404'
,123.45
,5000.00
,123.45
,10
,USER
,SYSDATE
);
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
--MODEL FOLLOWS
create or replace trigger Work_Center_Insert_Tr
before insert on Work_Center
for each row
DECLARE
v_Work_Center_Id Work_Center.Work_Center_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Work_Center_Id
FROM dual;
:NEW.Work_Center_Id:=v_Work_Center_Id;
END;
--MODEL ABOVE
ALTER Customer
DISABLE ALL TRIGGERS;
DROP Customer_Insert_Tr;
create or replace trigger Customer_Insert_Tr
before insert on Customer
for each row
declare v_Customer_Id Customer.Customer_Id%type;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO :v_CUSTOMER_ID
FROM dual;
:new.Customer_id := v_customer_Id;
END;
-- Trigger 2
describe Order_Master;
drop trigger Order_Archive_Tr;
create or replace trigger Order_Master_Insert_Tr
before insert on Order_Master
for each row
--when (new.Order_Id is null);
DECLARE
v_Order_Id Order_Master.Order_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO :NEW.Order_Id
--v_Order_Id
FROM dual;
-- :NEW.Order_Id:=v_Order_Id;
END;
insert into order_Master
(Order_Date
,
Customer_Id
,
LAST_UPDATED_By
,
LAST_UPDATE_DATE
)
values
(
SYSDATE
,
55
,
'APPAS'
,SYSDATE
);
-- Trigger 3
select * from Product;
describe Product;
create or replace trigger Product_Insert_Tr
before insert on Product
for each row
DECLARE v_Product_Id Product.Product_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Product_Id
FROM dual;
:NEW.Product_Id:=v_Product_Id;
END;
-- Trigger 4
SELECT * FROM wORK_cENTER;
describe Work_Center;
create or replace trigger Work_Center_Insert_Tr
before insert on Work_Center
for each row
DECLARE
v_Work_Center_Id Work_Center.Work_Center_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Work_Center_Id
FROM dual;
:NEW.Work_Center_Id:=v_Work_Center_Id;
END;
insert into Work_Center
(name
,Capacity
,Last_updated_by
,
Last_Update_date
)
values
('Wausau, WI)'
,
250
,
USER
,
SYSDATE
);
-- Trigger 5
describe Customer;
describe Customer_History;
create or replace trigger Customer_Archive_Tr
before insert on Customer
for each row
DECLARE
v_Customer_History_Id Customer.Customer_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Customer_History_Id
FROM dual;
insert into Customer_History
values
(v_Customer_History_Id
,
USER,
SYSDATE,
:old.Customer_Id,
:old.Name,
:old.Street_address
,
:old.City,
:old.State,
:old.Zip,
:old.Ytd_Purchases
,
:old.credit_limit
,
:old.OUTSTANDING_BALANCE
,
:old.Discount_Percent
,
:old.last_updated_by
,
:old.LAST_UPDATE_DATE
) ;
end;
-- Trigger 6
describe Order_Master;
describe Order_History;
create or replace trigger Order_Archive_Tr
before insert on Order_Master
for each row
DECLARE
v_Order_History_Id
Order_Master.Order_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Order_History_Id
FROM dual;
insert into Order_History
values
(v_Order_History_Id
,
USER
,
SYSDATE
,
:old.Order_Id
,
:old.Order_Date
,
:old.Customer_Id
,
:old.Last_updated_by
,
:old.Last_Update_Date
) ;
end;
-- Trigger 7
describe Order_Detail;
describe Order_Dtl_History;
create or replace trigger Order_Dtl_Archive_Tr
before insert on Order_Detail
for each row
DECLARE
v_Order_Dtl_History_Id
Order_Detail.Order_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_Order_Dtl_History_Id
FROM dual;
insert into Order_Dtl_History
values
(v_Order_Dtl_History_Id
,
USER
,
SYSDATE
,
:old.Order_Id
,
:old.Product_Id
,
:old.Qty_Ordered
,
:old.Last_updated_by
,
:old.Last_Update_Date
) ;
end;
-- Trigger 8
describe Product ;
describe Product_History;
create or replace trigger Product_Archive_Tr
before insert ON Product
for each row
DECLARE
v_History_Id Product_History.History_Id%TYPE;
begin
SELECT Pvfc_Seq.NEXTVAL
INTO v_History_Id
FROM dual;
insert into Product_History
values
(v_History_Id
,
USER
,
SYSDATE
,
:old.Product_Id
,
:old.Product_Line_Cd
,
:old.Location_Cd
,
:old.Finish_Cd
,
:old.Description
,
:old.Unit_Price
,
:old.Qty_On_Hand
,
:old.Qty_Backordered
,
:old.Reorder_Point
,
:old.Reorder_Qty
,
:old.Scheduled_Receipts
,
:old.Last_Updated_By
,
:old.Last_Update_Date
);
end;

Mais conteúdo relacionado

Mais procurados

Redux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncRedux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncArtur Szott
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive formsNir Kaufman
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
6.develop a synchronous service for caliculation of factorial of a given number
6.develop a synchronous service for caliculation of factorial of a given number6.develop a synchronous service for caliculation of factorial of a given number
6.develop a synchronous service for caliculation of factorial of a given numberxavier john
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Thuan Nguyen
 
Triggers in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsqlArun Sial
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Thuan Nguyen
 
Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018Joke Puts
 
Code review for busy people
Code review for busy peopleCode review for busy people
Code review for busy peopleDamien Seguy
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 
Cold fusion best practice
Cold fusion best practiceCold fusion best practice
Cold fusion best practiceisummation
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Thuan Nguyen
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating viewsecomputernotes
 

Mais procurados (16)

Redux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncRedux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with Async
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive forms
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
6.develop a synchronous service for caliculation of factorial of a given number
6.develop a synchronous service for caliculation of factorial of a given number6.develop a synchronous service for caliculation of factorial of a given number
6.develop a synchronous service for caliculation of factorial of a given number
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
 
Triggers in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsql
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01
 
Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018
 
Lab 07
Lab 07Lab 07
Lab 07
 
Code review for busy people
Code review for busy peopleCode review for busy people
Code review for busy people
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 
Cold fusion best practice
Cold fusion best practiceCold fusion best practice
Cold fusion best practice
 
Coding standards
Coding standardsCoding standards
Coding standards
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating views
 

Destaque

Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersAlexey Furmanov
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sqlSushil Mishra
 
MS SQL SERVER: Creating Views
MS SQL SERVER: Creating ViewsMS SQL SERVER: Creating Views
MS SQL SERVER: Creating Viewssqlserver content
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republicKaing Menglieng
 
Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012CarlosFloresRoman
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guidelineSidney Chen
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueriesecomputernotes
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLVikash Sharma
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)Ehtisham Ali
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 Andriy Krayniy
 
Sql query analyzer & maintenance
Sql query analyzer & maintenanceSql query analyzer & maintenance
Sql query analyzer & maintenancenspyrenet
 
SQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query PerformanceSQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query PerformanceMarek Maśko
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptFramgia Vietnam
 

Destaque (20)

Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML Triggers
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
MS SQL SERVER: Creating Views
MS SQL SERVER: Creating ViewsMS SQL SERVER: Creating Views
MS SQL SERVER: Creating Views
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 
Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012
 
Sql db optimization
Sql db optimizationSql db optimization
Sql db optimization
 
Joins SQL Server
Joins SQL ServerJoins SQL Server
Joins SQL Server
 
Sql xp 04
Sql xp 04Sql xp 04
Sql xp 04
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
 
Statistics
StatisticsStatistics
Statistics
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQL
 
Locking in SQL Server
Locking in SQL ServerLocking in SQL Server
Locking in SQL Server
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
 
Locking And Concurrency
Locking And ConcurrencyLocking And Concurrency
Locking And Concurrency
 
Sql query analyzer & maintenance
Sql query analyzer & maintenanceSql query analyzer & maintenance
Sql query analyzer & maintenance
 
SQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query PerformanceSQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query Performance
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
 

Semelhante a Triggers-Sequences-SQL

Semelhante a Triggers-Sequences-SQL (20)

A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database Views
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 
Oracle
OracleOracle
Oracle
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
 
How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
 
PLSQLIV.ppt
PLSQLIV.pptPLSQLIV.ppt
PLSQLIV.ppt
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
Plsql coding conventions
Plsql coding conventionsPlsql coding conventions
Plsql coding conventions
 
Der perfekte 12c trigger
Der perfekte 12c triggerDer perfekte 12c trigger
Der perfekte 12c trigger
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
 
SQl
SQlSQl
SQl
 
SQL-PL
SQL-PLSQL-PL
SQL-PL
 
Sql pl
Sql plSql pl
Sql pl
 
1 z1 051
1 z1 0511 z1 051
1 z1 051
 
Top MNC'S Interview questions and answers
Top MNC'S Interview questions and answersTop MNC'S Interview questions and answers
Top MNC'S Interview questions and answers
 

Triggers-Sequences-SQL

  • 1. /* web example CREATE OR REPLACE TRIGGER TEST_SEQ_TRIGGER BEFORE INSERT ON TESTUSER.EMPLOYEE FOR EACH ROW BEGIN IF :new.SSN IS NULL THEN SELECT TEST_SEQUENCE.nextval INTO :new.SSN FROM DUAL; END IF; END; web example end */ create or replace trigger Work_Center_Insert_Tr before insert on Work_Center for each row DECLARE v_Work_Center_Id Work_Center.Work_Center_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Work_Center_Id FROM dual; :NEW.Work_Center_Id:=v_Work_Center_Id; END; CREATE OR REPLACE TRIGGER NEW_EVALUATION_TRIGGER BEFORE INSERT ON EVALUATIONS FOR EACH ROW BEGIN :NEW.evaluation_id := evaluations_sequence.NEXTVAL; END;
  • 2. -- The title of the NEW_EVALUATION_TRIGGER pane is in italic font, indicating that --the trigger is not yet saved in the database. --In the CREATE TRIGGER statement, replace NULL with this: --:NEW.evaluation_id := evaluations_sequence.NEXTVAL CREATE OR REPLACE TRIGGER TEST_SEQ_TRIGGER BEFORE INSERT ON TESTUSER.EMPLOYEE FOR EACH ROW BEGIN IF :new.SSN IS NULL THEN SELECT TEST_SEQUENCE.nextval INTO :new.SSN FROM DUAL; END IF; END; describe customer; -- Sample trigger CREATE OR REPLACE TRIGGER employee_tr BEFORE INSERT ON employee FOR EACH ROW DECLARE v_employee_id employee.employee_id%TYPE; BEGIN SELECT employee_ID_SEQ.NEXTVAL INTO v_employee_id FROM dual;
  • 3. :NEW.employee_id:=v_employee_id; END; CREATE OR REPLACE TRIGGER dept_tr BEFORE INSERT ON departments FOR EACH ROW WHEN (new.id IS NULL) BEGIN SELECT dept_seq.NEXTVAL INTO :new.id FROM dual; END; --/*Test it using the automatic and manual population methods. /*SQL> INSERT INTO departments (description) 2 VALUES ('Development'); 1 row created. */ SELECT * FROM departments; ID DESCRIPTION ---------- -------------------------------------------------- 1 Development 1 row selected. SQL> INSERT INTO departments (id, description) 2 VALUES (dept_seq.NEXTVAL, 'Accounting');
  • 4. 1 row created. SQL> SELECT * FROM departments; ID DESCRIPTION ---------- -------------------------------------------------- 1 Development 2 Accounting */ -- ================================================================= =========================== -- Trigger 1 == describe Customer; insert into Customer ( NAME ,streET_ADDRESS ,CITY ,STATE ,ZIP ,YTD_PURCHASES ,CREDIT_LIMIT ,OUTSTANDING_BALANCE ,DISCOUNT_PERCENT ,LAST_UPDATED_BY ,LAST_UPDATE_DATE ) values ( 'Amy Cashier' ,'548 Main Street' ,'Louisville' ,'KY' ,'50404' ,123.45 ,5000.00 ,123.45 ,10 ,USER ,SYSDATE ); CREATE OR REPLACE TRIGGER dept_bir BEFORE INSERT ON departments FOR EACH ROW WHEN (new.id IS NULL) BEGIN SELECT dept_seq.NEXTVAL INTO :new.id
  • 5. FROM dual; END; --MODEL FOLLOWS create or replace trigger Work_Center_Insert_Tr before insert on Work_Center for each row DECLARE v_Work_Center_Id Work_Center.Work_Center_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Work_Center_Id FROM dual; :NEW.Work_Center_Id:=v_Work_Center_Id; END; --MODEL ABOVE ALTER Customer DISABLE ALL TRIGGERS; DROP Customer_Insert_Tr; create or replace trigger Customer_Insert_Tr before insert on Customer for each row declare v_Customer_Id Customer.Customer_Id%type; begin SELECT Pvfc_Seq.NEXTVAL INTO :v_CUSTOMER_ID FROM dual; :new.Customer_id := v_customer_Id;
  • 6. END; -- Trigger 2 describe Order_Master; drop trigger Order_Archive_Tr; create or replace trigger Order_Master_Insert_Tr before insert on Order_Master for each row --when (new.Order_Id is null); DECLARE v_Order_Id Order_Master.Order_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO :NEW.Order_Id --v_Order_Id FROM dual; -- :NEW.Order_Id:=v_Order_Id; END; insert into order_Master (Order_Date , Customer_Id , LAST_UPDATED_By , LAST_UPDATE_DATE ) values (
  • 7. SYSDATE , 55 , 'APPAS' ,SYSDATE ); -- Trigger 3 select * from Product; describe Product; create or replace trigger Product_Insert_Tr before insert on Product for each row DECLARE v_Product_Id Product.Product_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Product_Id FROM dual; :NEW.Product_Id:=v_Product_Id; END; -- Trigger 4 SELECT * FROM wORK_cENTER; describe Work_Center; create or replace trigger Work_Center_Insert_Tr before insert on Work_Center for each row
  • 8. DECLARE v_Work_Center_Id Work_Center.Work_Center_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Work_Center_Id FROM dual; :NEW.Work_Center_Id:=v_Work_Center_Id; END; insert into Work_Center (name ,Capacity ,Last_updated_by , Last_Update_date ) values ('Wausau, WI)' , 250 , USER , SYSDATE ); -- Trigger 5 describe Customer; describe Customer_History; create or replace trigger Customer_Archive_Tr before insert on Customer for each row
  • 9. DECLARE v_Customer_History_Id Customer.Customer_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Customer_History_Id FROM dual; insert into Customer_History values (v_Customer_History_Id , USER, SYSDATE, :old.Customer_Id, :old.Name, :old.Street_address , :old.City, :old.State, :old.Zip, :old.Ytd_Purchases , :old.credit_limit , :old.OUTSTANDING_BALANCE , :old.Discount_Percent , :old.last_updated_by , :old.LAST_UPDATE_DATE ) ; end; -- Trigger 6 describe Order_Master; describe Order_History;
  • 10. create or replace trigger Order_Archive_Tr before insert on Order_Master for each row DECLARE v_Order_History_Id Order_Master.Order_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Order_History_Id FROM dual; insert into Order_History values (v_Order_History_Id , USER , SYSDATE , :old.Order_Id , :old.Order_Date , :old.Customer_Id , :old.Last_updated_by , :old.Last_Update_Date ) ; end; -- Trigger 7 describe Order_Detail; describe Order_Dtl_History; create or replace trigger Order_Dtl_Archive_Tr
  • 11. before insert on Order_Detail for each row DECLARE v_Order_Dtl_History_Id Order_Detail.Order_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_Order_Dtl_History_Id FROM dual; insert into Order_Dtl_History values (v_Order_Dtl_History_Id , USER , SYSDATE , :old.Order_Id , :old.Product_Id , :old.Qty_Ordered , :old.Last_updated_by , :old.Last_Update_Date ) ; end; -- Trigger 8 describe Product ; describe Product_History;
  • 12. create or replace trigger Product_Archive_Tr before insert ON Product for each row DECLARE v_History_Id Product_History.History_Id%TYPE; begin SELECT Pvfc_Seq.NEXTVAL INTO v_History_Id FROM dual; insert into Product_History values (v_History_Id , USER , SYSDATE , :old.Product_Id , :old.Product_Line_Cd , :old.Location_Cd , :old.Finish_Cd , :old.Description , :old.Unit_Price , :old.Qty_On_Hand , :old.Qty_Backordered , :old.Reorder_Point , :old.Reorder_Qty , :old.Scheduled_Receipts , :old.Last_Updated_By , :old.Last_Update_Date