Examples Of Create Trigger - HP Neoview SQL Reference Manual

Hide thumbs Also See for Neoview SQL:
Table of Contents

Advertisement

As a workaround for this issue, change column c1 of table t1 from varchar(2040) to
char(2000). The primary key length is now 2000 bytes, and the CREATE TRIGGER statement
completes successfully.

Examples of CREATE TRIGGER

Before and After Triggers
Suppose that you have a database to record patients' vital signs and drugs prescribed for them.
The database consists of these tables:
vital_signs, which records vital signs at each visit
prescription, which records prescriptions written for each patient
generic_drugs, which lists generic drug equivalents for brand-name drugs
The prescription table is created like this:
CREATE TABLE prescription
( id
pat_id
issuing_phys_id
date_prescribed
drug
record_id
dosage
frequency
refills_remaining
instructions
primary key (id));
You can create a BEFORE trigger on prescription so that when a prescription is entered, if
the prescribed drug is found in generic_drugs, a generic drug is substituted for the brand-name
drug, and the instructions for the drugs are updated:
CREATE TRIGGER alternate_drug
BEFORE INSERT ON prescription
REFERENCING NEW AS newdrug
FOR EACH ROW
WHEN (upshift(newdrug.drug) IN
(SELECT upshift(generic_drugs.drug) FROM generic_drugs))
SET newdrug.drug = (SELECT
upshift(generic_drugs.alternate_drug)
upshift(generic_drugs.drug)),
newdrug.instructions = newdrug.instructions ||
' Prescribed drug changes to alternative drug.';
You can create an AFTER trigger on vital_signs so that when that table is updated, SQL
checks the patient's weight and height. Based on their values, this trigger might add a record to
prescription to create a new prescription for a weight-loss drug with instructions that indicate
that this is a free sample:
CREATE TRIGGER free_sample
AFTER INSERT ON vital_signs
REFERENCING NEW AS sample
FOR EACH ROW
WHEN (sample.weight > 299 and sample.height < 69)
INSERT INTO prescription
(id, pat_id, issuing_phys_id, record_id, date_prescribed,
drug, dosage,
frequency, refills_remaining, instructions)
VALUES
100
SQL Statements
INTEGER
NOT NULL,
INTEGER
NOT NULL,
INTEGER
NOT NULL,
DATE
DEFAULT NULL,
VARCHAR(80)
DEFAULT NULL,
INTEGER
NOT NULL,
VARCHAR(30)
NOT NULL,
VARCHAR(30)
DEFAULT NULL,
INTEGER
DEFAULT NULL,
VARCHAR(255)
DEFAULT NULL,
FROM generic_drugs
WHERE upshift(newdrug.drug) =

Advertisement

Table of Contents
loading

Table of Contents