Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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
  #1  
Old July 12th, 2004, 04:11 PM
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
Question Oracle 9i trigger question

First post!

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?

Any help would be much appreciated!

Cheers :-)

Reply With Quote
  #2  
Old July 12th, 2004, 04:52 PM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
The only way I know to do this is to create another table, with the same access as the table BED, that keeps track of inserts. Your trigger has to maintain the table. Or create a single CHAR field in BED that is set to 'X' when the trigger fires on a given record, and left NULL until the trigger executes.

Trigger variables are not static - they do not persist after the trigger executes. Plus, even if they were they still would not work because triggers execute in the current user's session.
If you had five users hitting BED, the trigger would not fire until one of the five users inserted 20 rows.

Real answer:
What are your trying to do?

Reply With Quote
  #3  
Old July 12th, 2004, 05:21 PM
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
Yeah sorry, I didn't really explian myself very well in the first message. I am doing a university assignment, so DB interaction will be slight (no more than one user at a time!). 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 suggestions?

Reply With Quote
  #4  
Old July 13th, 2004, 07:28 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
As i understand, you need to following modification in your trigger:

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;

Reply With Quote
  #5  
Old September 4th, 2004, 10:15 AM
cerebellum cerebellum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Kuala Lumpur
Posts: 3 cerebellum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 46 sec
Reputation Power: 0
Send a message via MSN to cerebellum Send a message via Yahoo to cerebellum
Quote:
Originally Posted by shafique
As i understand, you need to following modification in your trigger:

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;


greetings..

i have the following code:

Code:
create or replace trigger eci_discount
before insert
	on eci_purchase
	for each row

declare
	v_cus_type eci_customer.cus_type%type;

begin
	select cus_type into v_cus_type from eci_customer where cus_id = :new.cus_id;
	if (v_cus_type = 'GOOD') then
		update eci_purchase set 
			amount_discount = :new.sub_total * 0.1, 
			total_price = :new.sub_total * 0.9
		where pur_id = pur_id;
		dbms_output.put_line('Customer ' || :new.cus_id || ' has got a discount');
	else
		dbms_output.put_line('Customer ' || :new.cus_id || ' has got no discount');
	end if;
end;


but even the prompt has got a discount, but why is that the record is not updated?

i'm trying to check the customer status. if it's good, then discount is given.
please advice.

thanks.

Reply With Quote
  #6  
Old September 4th, 2004, 02:00 PM
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 have some problem in your trigger (marked with red color), look at your code and my comments

create or replace trigger eci_discount
before insert -- trigger will fire before inserting any record into table
on eci_purchase
for each row

declare
v_cus_type eci_customer.cus_type%type;

begin
select cus_type into v_cus_type from eci_customer where cus_id = :new.cus_id;
if (v_cus_type = 'GOOD') then
update eci_purchase set
amount_discount = :new.sub_total * 0.1,
total_price = :new.sub_total * 0.9
where pur_id = pur_id; -- how come you update such a record that have not yet been inserted
dbms_output.put_line('Customer ' || :new.cus_id || ' has got a discount');
else
dbms_output.put_line('Customer ' || :new.cus_id || ' has got no discount');
end if;
end;

so you need to read the following code and make certain changes as required.

create or replace trigger eci_discount
before insert
on eci_purchase
for each row
declare
v_cus_type eci_customer.cus_type%type;
begin
select cus_type into v_cus_type from eci_customer where cus_id = :new.cus_id;
if (v_cus_type = 'GOOD') then
:new.amount_discount := :new.sub_total * 0.1;
:new.total_price := :new.sub_total * 0.9;
dbms_output.put_line('Customer ' || :new.cus_id || ' has got a discount');
else
dbms_output.put_line('Customer ' || :new.cus_id || ' has got no discount');
end if;
end;

Reply With Quote
  #7  
Old September 4th, 2004, 08:35 PM
cerebellum cerebellum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Kuala Lumpur
Posts: 3 cerebellum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 46 sec
Reputation Power: 0
Send a message via MSN to cerebellum Send a message via Yahoo to cerebellum
i found out about that later. and changed it into after insert.
but some problem occured saying that the table was mutating and the trigger might not see it.

how can i solve this problem?
please advice.
thanks.

Reply With Quote
  #8  
Old September 5th, 2004, 11:11 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 use the after insert trigger because you are inserting and updating the same table that cause to generate the mutating error. You did not try my solution, what about that?

let's take a look again. modification marked by red color.

create or replace trigger eci_discount
before insert
on eci_purchase
for each row
declare
v_cus_type eci_customer.cus_type%type;
begin
select cus_type into v_cus_type from eci_customer where cus_id = :new.cus_id;

-- In IF condition i did not use UPDATE clause

if (v_cus_type = 'GOOD') then
:new.amount_discount := :new.sub_total * 0.1;
:new.total_price := :new.sub_total * 0.9;
dbms_output.put_line('Customer ' || :new.cus_id || ' has got a discount');
else
dbms_output.put_line('Customer ' || :new.cus_id || ' has got no discount');
end if;
end;

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Oracle 9i trigger question


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





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