Posted by: bratboy December 12, 2006
Oralce/SQLserver trigger help???
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
Read Full Discussion Thread for this article