[Show all top banners]

bhan0001
Replies to this thread:

More by bhan0001
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Oralce/SQLserver trigger help???
[VIEWED 4817 TIMES]
SAVE! for ease of future access.
Posted on 12-10-06 2:44 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Any experts in Oracle or SqlServer?

I,m trying to write a trigger, when a new department is added,
• Checks the uniqueness of the DNO
– If the value is unique then it adds the department to the
table
– Otherwise, copies the department info into an audit table
I already created 3 table emp, dept, audittable and stored procedure that checks the uniqueness of Dept no

Below is the code i wrote for trigger, it does checks the uniqueness of Deptno but doesn't copy it to audit table when wrong dept is inserted.

CREATE OR REPLACE TRIGGER trig_dept_new
BEFORE INSERT on DEPT
FOR EACH ROW
DECLARE dept_exist NUMBER;
BEGIN
SELECT Count(DEPTNO) INTO dept_exist
FROM DEPT
WHERE DEPT.DEPTNO = :NEW.DEPTNO;

IF dept_exist >= 1 THEN
INSERT INTO AUDITDEPT (DEPTNO, DNAME, LOC)
values (:NEW.DEPTNO, :NEW.DNAME, :NEW.LOC);

RAISE_APPLICATION_ERROR
(-20685, 'DEPT is not added, copied into audit table');
END IF;

END;
/
SQL> insert into DEPT (DEPTNO, DNAME, LOC) values (20,'SUPPORT','DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC) values (20,'SUPPORT','DALLAS')
*
ERROR at line 1:
ORA-20685: DEPT is not added, copied into audit table
ORA-06512: at "A.TRIG_DEPT_NEW", line 11
ORA-04088: error during execution of trigger 'A.TRIG_DEPT_NEW'


Any help would be appreciated.
 
Posted on 12-11-06 6:40 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Any expert yet??????????
 
Posted on 12-11-06 10:30 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

. Bhan0001 Looks like there is an error at values (:NEW.DEPTNO, :NEW.DNAME, :NEW.LOC);

ORA-06512 :-

// *Cause: Backtrace message as the stack is unwound by unhandled
// exceptions.
// *Action: Fix the problem causing the exception or write an exception
// handler for this condition. Or you may need to contact your
// application administrator or DBA.
 
Posted on 12-11-06 12:54 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Post it in DBASupport.com,you will get an expert advice.I do it all the time and get immediate replies and they are the best,plus it's free.Sorry bro havent done any triggers lately.Did you disable any constraints ( foreign?) before trying to audit the table?Check the alert log,you will see what exactly is happening.
 
Posted on 12-11-06 7:25 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thanks bme2005, I just posted my thread out there. Hopefully,I'will get my solution back soon.
BTW, if i take out the error handiling, it inserts it into both the table eventhough the input value dept is already in dept table. It should insert only in auditdept table.
Thanks in advance to everybody who can input some thoughts.
 
Posted on 12-11-06 8:14 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

pay freaking attention in class, and hit the books more often.
 
Posted on 12-12-06 7:50 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

You have it backward.
As far as I can see you are trying to use a trigger to check if a record exists or not in the DEPT table during insert and then raising an error if the record already exists in the table. This is not the proper way to handle it.

Use a primary key constraint instead on the DEPT Table. Define a primary key constraint on the DeptNo column. Oracle will now automatically disallow the insert of the duplicate records in the table. Integrity constraints like this are best left to the database to handle. How are you going to handle the updates?

I hope you are using stored procedures/views to do the update/insert in the table instead of directly updating/inserting in the tables. As far as I know good practices dictate that the tables should never be accessed directly and indeed most of the good DBAs disallow direct access to the table (this is easy to secure). If you are using a stored procedure, then you can trap the constraint violation (the error code escapes my mind offhand right now) in an exception block and insert the record in the audit table.

PS I suggest you grab a good book on relational database design. Try to get Joe Celko's books. Looking at the code (i mean no offense to you personally), the database can do with a revision.

Cheers
Brat
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 90 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
Toilet paper or water?
ढ्याउ गर्दा दसैँको खसी गनाउच
Mamta kafle bhatt is still missing
I hope all the fake Nepali refugee get deported
Tourist Visa - Seeking Suggestions and Guidance
Problems of Nepalese students in US
Are Nepalese cheapstakes?
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
From Trump “I will revoke TPS, and deport them back to their country.”
Travel Document for TPS (approved)
wanna be ruled by stupid or an Idiot ?
Sajha Poll: Who is your favorite Nepali actress?
अरुणिमाले दोस्रो पोई भेट्टाइछिन्
To Sajha admin
Those who are in TPS, what’s your backup plan?
seriously, when applying for tech jobs in TPS, what you guys say when they ask if you have green card?
How to Retrieve a Copy of Domestic Violence Complaint???
MAGA denaturalization proposal!!
Nepali Psycho
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters