Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Iron Speed
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old July 13th, 2004, 05:15 AM
penknife penknife is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 penknife User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oracle 9i SQL*Plus problem

Using Oracle 9i SQL*Plus I am trying to write a trigger which will fire if more than 20 entities are "posted" into a particular table. Here is the code I have so far:

CREATE OR REPLACE TRIGGER q2
BEFORE UPDATE OR INSERT ON BED

DECLARE
num_of_beds VARCHAR2(2);

begin

SELECT COUNT(*) INTO num_of_beds FROM BED WHERE Ward_name = ????;

IF num_of_beds >= 20 THEN

DBMS_OUTPUT.PUT_LINE('Trigger fired');

END IF;

end;

Does anyone know the SQL needed to replace the question marks so that the trigger would detect which ward has been specified if a user runs an INSERT command?

Is it :new.ward_name?

I am doing a university assignment, so DB interaction will be slight (no more than one user at a time!) and the tables I am using are as follows:

DROP TABLE STAFF;
DROP TABLE BED;
DROP TABLE WARD;

CREATE TABLE STAFF
(
Staff_id varchar2(10) NOT NULL,
Name varchar2(35) NOT NULL,
address VARCHAR2(50) NOT NULL,
teleno varchar2(10) NOT NULL,
NIno varchar2(10) NOT NULL,
Notes VARCHAR2(500) NOT NULL,
Department Varchar2(20),
Staff_responsibility Varchar2(20),
Date_started Date,
Date_left Date,
CONSTRAINT STAFF_PK
PRIMARY KEY (Staff_id)
);

CREATE TABLE BED
(
Bed_no INTEGER NOT NULL,
Ward_name varchar2(10) NOT NULL,
Centre_name varchar2(10) NOT NULL,
CONSTRAINT BED_PK
PRIMARY KEY (Bed_no,Ward_name,Centre_name)
);

CREATE TABLE WARD
(
Ward_name varchar2(10) NOT NULL,
Centre_name varchar2(10) NOT NULL,
CONSTRAINT WARD_PK
PRIMARY KEY (Ward_name,Centre_name)
);

And I have to write a trigger to enforce the following constraint:

There must be no more than 20 beds to a ward.

I am planning to implement 3 wards for test purposes.

Any help would be much appreciated!

Cheers :-)

Reply With Quote
  #2  
Old July 13th, 2004, 07:47 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
You cannot user new or old option if the defined trigger is table level, you have to make followoing modification in your trigger to convert it into row level trigger then use old and new keyword:

CREATE OR REPLACE TRIGGER q2
BEFORE UPDATE OR INSERT ON BED
FOR EACH ROW
DECLARE
num_of_beds VARCHAR2(2);

begin

SELECT COUNT(*) INTO num_of_beds FROM BED WHERE Ward_name = :new.ward_name;

IF num_of_beds >= 20 THEN

DBMS_OUTPUT.PUT_LINE('Trigger fired');

END IF;

end;



Regards,

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Oracle 9i SQL*Plus problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway