Help me write a trigger : I have no idea how to  :(

Hi,
I have to design a trigger based on the following requirement:
I have an ASSET table ASSET (asset_id number(10) (primary key) , site_id (foreign key) references SITE(site_id) , cost_ctr_id (foreign key) references COST_CENTER(cost_ctr_id) )
I have a COST_CENTER table COST_CENTER (cost_center_id number(10) primary key) I have a SITE table SITE (site_id number(10) primary key)
I have a SITE_COST_CTR table SITE_COST_CTR (site_id (foreign key) references SITE(site_id ) , cost_ctr_id (foreign key) references COST_CENTER(cost_ctr_id) ) here (site_id, cost_ctr_id) is the composite primary key .
Now I want a trigger which should satisfy the following : Whenever there is an insert or update in the ASSET table, it should first check in the SITE table and if the site_id is not there it should insert that site_id in the site table, similarly for cost_center_id it should see the cost center table and insert cost_center_id there, followed by an insert into SITE_COST_CTR table (It should insert site_id, cost_center_id in SITE_COST_CTR table)
Can anyone please help me with this trigger ? I have no idea, never ever done it till date :'(. Please help me.
Here's my effort so far, and I am not even a novice in this area!
CREATE OR REPLACE TRIGGER TR_SITE_COST_CTR
BEFORE INSERT OR UPDATE ON ASSET
REFERENCING NEW AS NEW_ASSET
FOR EACH ROW
WHEN ( (SELECT COUNT(*) FROM SITE WHERE SITE_ID = NEW_ASSET.SITE_ID) > 1 AND (SELECT COUNT(*) FROM COST_CENTER WHERE COST_CENTER_ID = NEW_ASSET.COST_CENTER_ID) > 1 )
BEGIN
INSERT INTO SITE (SITE_ID) VALUES (:NEW_ASSET.SITE_ID);
INSERT INTO COST_CENTER VALUES (:NEW_ASSET.COST_CENTER_ID , :NEW_ASSET.COST_CENTER_ID); INSERT INTO SITE_COST_CENTER VALUES (:NEW_ASSET.SITE_ID, :NEW_ASSET.COST_CENTER_ID);
END
Thanks in advance :)

SQL> create table site (site_id number(10) primary key)
  2  /
Tabel is aangemaakt.
SQL> create table cost_center (cost_ctr_id number(10) primary key)
  2  /
Tabel is aangemaakt.
SQL> create table site_cost_ctr
  2  ( site_id     number(10) references site(site_id)
  3  , cost_ctr_id number(10) references cost_center(cost_ctr_id)
  4  , primary key (site_id,cost_ctr_id)
  5  )
  6  /
Tabel is aangemaakt.
SQL> create table asset
  2  ( asset_id    number(10) primary key
  3  , site_id     number(10) references site(site_id)
  4  , cost_ctr_id number(10) references cost_center(cost_ctr_id)
  5  )
  6  /
Tabel is aangemaakt.
SQL> create trigger tr_site_cost_ctr
  2  before insert or update on asset for each row
  3  declare
  4    l_dummy number;
  5  begin
  6    begin
  7      select site_id
  8        into l_dummy
  9        from site
10       where site_id = :new.site_id
11      ;
12    exception
13    when no_data_found then
14      insert into site (site_id) values (:new.site_id);
15    end
16    ;
17    begin
18      select cost_ctr_id
19        into l_dummy
20        from cost_center
21       where cost_ctr_id = :new.cost_ctr_id
22      ;
23    exception
24    when no_data_found then
25      insert into cost_center (cost_ctr_id) values (:new.cost_ctr_id);
26    end
27    ;
28    begin
29      select site_id
30        into l_dummy
31        from site_cost_ctr
32       where site_id = :new.site_id
33         and cost_ctr_id = :new.cost_ctr_id
34      ;
35    exception
36    when no_data_found then
37      insert into site_cost_ctr (site_id,cost_ctr_id) values (:new.site_id,:new.cost_ctr_id);
38    end
39    ;
40  end;
41  /
Trigger is aangemaakt.
SQL> insert into asset values (1, 11, 21)
  2  /
1 rij is aangemaakt.
SQL> insert into asset values (2, 11, 22)
  2  /
1 rij is aangemaakt.
SQL> insert into asset values (3, 12, 21)
  2  /
1 rij is aangemaakt.
SQL> insert into asset values (4, 11, 21)
  2  /
1 rij is aangemaakt.
SQL> select * from site
  2  /
                               SITE_ID
                                    11
                                    12
2 rijen zijn geselecteerd.
SQL> select * from cost_center
  2  /
                           COST_CTR_ID
                                    21
                                    22
2 rijen zijn geselecteerd.
SQL> select * from site_cost_ctr
  2  /
                               SITE_ID                            COST_CTR_ID
                                    11                                     21
                                    11                                     22
                                    12                                     21
3 rijen zijn geselecteerd.
SQL> select * from asset
  2  /
                              ASSET_ID                                SITE_ID                            COST_CTR_ID
                                     1                                     11                                 21
                                     2                                     11                                 22
                                     3                                     12                                 21
                                     4                                     11                                 21
4 rijen zijn geselecteerd.Regards,
Rob.

Similar Messages

  • Help I am a student and have no idea how to sort this program by Product

    First class:
    // CheckPoint: Inventory Program Part 1
    // Display product information
    public class Product1 //declare class
    //declare instance variables
    private int productNumber,
    inStock;
    private String productName; // List of instance variables unique to object
    private double unitPrice;
    //constructor to handle no input and set defaults
    public Product1()
    //constructor to handle input parameters
    public Product1( int number, String name, int stock, double perPrice )
    productNumber = number;
    productName = name;
    inStock = stock;
    unitPrice = perPrice;
    public void setProductNumber( int number ) //stores product number
    productNumber = number;
    public void setProductName( String name ) //stores product name
    productName = name;
    public void setInStock( int stock ) //stores amount of product in stock
    inStock = stock;
    public void setUnitPrice( double perPrice ) //stores the unit price of product
    unitPrice = perPrice;
    public int getProductNumber() //allows a call to retrieve product number
    return productNumber;
    public String getProductName() //allows a call to retrieve the product name
    return productName;
    public int getInStock() //allows a call to retrieve stock info
    return inStock;
    public double getUnitPrice() //allows a call to retrieve unit price
    return unitPrice;
    public double getInventoryValue()
    return ( (double)inStock * unitPrice );
    public String toString()
    return productName + " - " + unitPrice;
    Second Class:
    import java.text.DecimalFormat;
    public class Inventory1
    private final int maxInventory = 30;
    private Product1 items[] = new Product1[ maxInventory ];
    DecimalFormat formatter = new DecimalFormat( "$##,###.00" );
    public void addProduct( Product1 item )
    for ( int i = 0; i < maxInventory; i++ )
    if (items[i] == null)
    items[i] = item;
    return;
    public double getTotalInventoryValue()
    double sum = 0.0;
    for ( Product1 item : items )
    if ( item != null )
    sum += item.getInventoryValue();
    return sum;
    public void displayInventory()
    boolean hasItems = false;
    for ( Product1 item : items )
    if ( item != null)
    hasItems = true;
    System.out.printf( "%8d%12s%11d%13.2f%14.2f", item.getProductNumber() , item.getProductName(), item.getInStock(), item.getUnitPrice(), item.getInventoryValue() );
    System.out.println( "" ); // display space
    System.out.println("");
    Part 3:
    import java.text.DecimalFormat;
    public class InventoryTest1
    static DecimalFormat formatter = new DecimalFormat( "$##,###.00" );
    public static void main( String args[] )
    Product1 item1 = new Product1( 001, "Staples", 5, 2.99 );
    Product1 item2 = new Product1( 002, "Books", 10, 15.00 );
    Product1 item3 = new Product1( 003, "Printers", 20, 55.00 );
    Inventory1 myInventory1 = new Inventory1();
    System.out.println("");
    System.out.printf( "%s%12s%11s%13s%14s", "Product#", "Name", "In Stock", "Unit Price", "Stock Value" );
    System.out.println("");
    System.out.println("");
    myInventory1.displayInventory();
    myInventory1.addProduct( item1 );
    myInventory1.addProduct( item2 );
    myInventory1.addProduct( item3 );
    myInventory1.displayInventory();
    System.out.println( "" ); // display space
    System.out.println("");
    System.out.println( "Total value of inventory is: " );
    System.out.println ( formatter.format(myInventory1.getTotalInventoryValue() ) );
    I have no idea where to put the sort statement and how to implement it.

    Sorry about that. Before i proceed with the code I will say that I need help trying to determine where to put a sort statement and what sort statement should I use. I have three classes and have no idea how to sort the items by Product name. For instance, I have staples, books, and printers in my code. How do I get it to display these names and all of the corresponding information in alphabetical order. So with that said, here is the code.
    First program:
    // CheckPoint: Inventory Program Part 1
    // Display product information
    public class Product1 //declare class
    //declare instance variables
    private int productNumber,
    inStock;
    private String productName; // List of instance variables unique to object
    private double unitPrice;
    //constructor to handle no input and set defaults
    public Product1()
    //constructor to handle input parameters
    public Product1( int number, String name, int stock, double perPrice )
    productNumber = number;
    productName = name;
    inStock = stock;
    unitPrice = perPrice;
    public void setProductNumber( int number ) //stores product number
    productNumber = number;
    public void setProductName( String name ) //stores product name
    productName = name;
    public void setInStock( int stock ) //stores amount of product in stock
    inStock = stock;
    public void setUnitPrice( double perPrice ) //stores the unit price of product
    unitPrice = perPrice;
    public int getProductNumber() //allows a call to retrieve product number
    return productNumber;
    public String getProductName() //allows a call to retrieve the product name
    return productName;
    public int getInStock() //allows a call to retrieve stock info
    return inStock;
    public double getUnitPrice() //allows a call to retrieve unit price
    return unitPrice;
    public double getInventoryValue()
    return ( (double)inStock * unitPrice );
    }

  • Need help- have no idea how to install original airport on G3 Powerbook

    I just bought an ornignal airport card on ebay. I have no idea how to install it (i think it goes under the keyboard, but specifically where?) also, it didn't come with any software...do I need some kind of driver? I am so confused. All I know is that I spent over $100 for this thing and would love it if I could get it to work. Really want to go wireless...can anyone please help. (already checked all over the support page and called apple and asked them to point me in the right direction, but have gotten no where. thanks in advance.

    Thea,
    I get the impression you cannot return the AirPort card...this would be my first choice. Since the card is not recognized, either the card is bad or your powerbook has a hardware issue (doubtful).
    Do you have an Apple Store nearby?
    You might also contact your local User Group...there should be good help available:
    http://www.apple.com/usergroups/
    Your alternatives are these:
    - find another original AirPort card (limited to the slower 11Mbps speed) to try;
    - remove the internal AirPort card and buy a one of these wireless PC cards that run at the faster 54Mbps speed and are simply inserted in the PCMCIA card slot on the side of the 'book:
    http://www.sonnettech.com/product/aria_extreme.html
    http://www.macsense.com/product/broadband/WPE800.html
    http://www.asante.com/products/productsLvl3/AL5403_XG.asp
    http://www.buffalotech.com/wireless/products/airstation/WLICBG54A.html
    The above cards have the Broadcom chipset that Apple's AirPort software supports. If you have already installed AirPort 3.1.1, you are ready to go.

  • TS3992 I have a pre paid Wi Fi with telstra for 12 months and yet I get the message of this I pad has not been backed up I have no idea how to back it up can you help. I have the same problem with my I phone withTelstra please tell me how to back them up

    Hi I have an I pad 64 gig and internal wifi bought about 2 months ago. it keeps coming up with a message "this i pad has not been backed up" I have no idea how to do it can you help.
    I also have an I phone 4s with the same message both are with telstra the I pad has a 12 month pre paid wifi contract and the phone is on a monthly contract.

    Sounds like you actually paying for cellular service and you really need to be on wifi to backup your ipad/iphone.
    Normally you have to be connected to wifi and you can do manual backup by going to Settings- icloud- storage and backup and pressing backup now button. Start with your ipad, usually size of the backup smaller and return here if you encounter a problem.

  • My dad and I have shared an iTunes account for years and I just created a new apple id but I have no Idea how to get all those purchases to this new account. Help please?

    My dad and I have shared an iTunes account for years and I just created a new apple id but I have no Idea how to get all those purchases to this new account. Help please?

    The old Apple ID (presumably your dads) owns those songs and your new Apple ID does not.  However, your dad can authorize your iD to use the songs by going to Store > Authorize This Computer from your iTunes menu and entering his Apple ID and password.

  • When I connect my iphone to my mac it says itunes will not work with my iphone cause it needs version 10.6.3 itunes,. problem is i have no idea how to down load that to my iphone when my mac will not connet to it cause it nees the down load help please

    when I connect my iphone to my mac it says itunes will not work with my iphone cause it needs version 10.6.3 itunes,. problem is i have no idea how to down load that to my iphone when my mac will not connet to it cause it nees the down load help please

    Ive got the same problem, just got my iphone unlocked, and they told me to sync it to itunes to get it permently unlocked, well everytime i plug my iphone in it says; it wont work as it needs up dating to 10.6.3, which i have already updated, but still doesnt work, so how can i get my phone unlocked?

  • After upgrading my software i have seem to have lost all my contacts and photos, i did a back up but i have no idea how to get them back on? maybe im illiterate but i dont know how to find it in my itunes? please help!!

    After upgrading my software i have seem to have lost all my contacts and photos, i did a back up but i have no idea how to get them back on? maybe im illiterate but i dont know how to find it in my itunes? please help!!

    http://support.apple.com/kb/HT5824?viewlocale=en_US&locale=en_US

  • Ok, i have lost my ipod and i REALLY need to find it, and i have no idea how i can track it. HELP!!!!

    ok, i have lost my ipod and i REALLY need to find it, and i have no idea how i can track it. HELP!!!!

    There is no way to track a lost or stolen iPod Classic. Sorry.
    Reporting a lost or stolen Apple product
    B-rock

  • I have no idea how to use Firefox. I used to have Internet Explorer 7 but am unable to connect to it anymore. How do I use Firefox? I know how to use a computer, somewhat, but am not as savy as I'd like to be. I will appreciate your help.

    I have no idea how to use Firefox. I used to have Internet Explorer 7 but am unable to connect to it anymore. How do I use Firefox? I know how to use a computer, somewhat, but am not as good at it as I'd like to be. I will appreciate your help.

    You can look at this post in this MozillaZine forum thread about inspecting a sessionstore file and possibly extract URLs.
    *http://forums.mozillazine.org/viewtopic.php?f=38&p=12098147&start=60#p12098147
    You can open the Browser Console (Firefox/Tools > Web Developer).
    Paste the JavaScript code in the command line and press the Enter key to run the code.
    *Toggle the devtools.chrome.enabled pref on the <b>about:config</b> page to true to enable the command line
    *https://developer.mozilla.org/Tools/Browser_Console#Browser_Console_command_line

  • I've just brought myself a new MacBook Pro from a friend I have no idea how to use them I YouTube how to reset a MacBook Pro I done everything all we'll In till you reinstall then it say "this disk is locked"??? Can anyone help me please

    I've just brought myself a new MacBook Pro from a friend I have no idea how to use them I YouTube how to reset a MacBook Pro I done everything all we'll In till you reinstall then it say "this disk is locked"??? Can anyone help me please

    I done everything all we'll In till you reinstall
    Please detail ALL you have done so far in the way of troubleshooting?   Need this info to avoid the been there done that scenarios.
    Care to share which OS you are using?
    Which MBP model do you have? 

  • I am still trying to update my ipod touch 4 to the iOS 6 version. It is on the 4.3.5 (8L1) version. When I go to settings-General-it just says About and nothing about an update. I have no idea how to update it! Please help!

    I am still trying to update my Ipod touch 4 to the iOS 6 version. It is on the 4.3.5 (8L1) version. When I go to settings-General-it just says About and nothing about an update. I have no idea how to update it! Please help!

    You can only update via the ipod with ios 5 or later.
    You need to update via itunes on your computer as always.

  • Can anyone help, I have an I-phone 4s and trying to sync to ITunes. It tells me I need to update I tunes on the phone to 11.1 but have no idea how to do this....?

    Hi, can anyone help...trying to sync I phone 4s with I tunes, it comes up with message to upgrade iTunes on phones to 11.1 but have no idea how to? Can anyone help with how I do this or any other way to get all music onto iPhone?? Thanks.

    Yes, it will keep the music.
    Close all open apps or programs.  Open iTunes, look for Check for Updates in the menu and click on that.  Then follow the prompts.
    When the update is done.  Open iTunes and in the menu look for show sidebar and click on that, then connect your iPhone to iTunes, click on your phone's name on the Sidebar, review all the tabs to make sure your sync option are what you want them to be (music, movies, photos, books, etc.) and after that click sync or apply and wait for the sync to finish.

  • Most of the videos I have purchased from itunes have disappeared from my screen, and I have no idea how to get them back....help ???????

    Most of the videos I have purchased from Itunes have disappeared from the screen and I have no idea how to get them back...help ????

    Hello,
    The Reset Firefox feature can fix many issues by restoring Firefox to its factory default state while saving your essential information.
    Note: ''This will cause you to lose any Extensions, Open websites, and some Preferences, but not Bookmarks and history''
    To Reset Firefox do the following:
    #Go to Firefox > Help > Troubleshooting Information.
    #Click the "Reset Firefox" button.
    #Firefox will close and reset. After Firefox is done, it will show a window with the information that is imported. Click Finish.
    #Firefox will open with all factory defaults applied.
    Further information can be found in the [[Reset Firefox – easily fix most problems]] article.
    Did this fix your problems? Please report back to us!
    Thank you.

  • There were pictures originally on my computer and were somehow take from my computer completely and moved to my iphone. On my iphone it doesn't let me delete those photos and i have no idea how to move them back to my computer. HELP? I need to keep them.

    There were pictures originally on my computer and were somehow take from my computer completely and moved to my iphone. On my iphone it doesn't let me delete those photos and i have no idea how to move them back to my computer. HELP? I need to keep them, they are valuable to me and were supposed to have been on my computer. I have no idea how to move them back since my computer barely lets me open my iphone and acknoledge those moved photos. Please help me!!

    Also in itunes on my computer i deselected the photos and their still on my phone.

  • I'm trying to order a calendar off iphoto and have never had any issues, now it wont allow me to input a canadian address for ordering  help help help this is an xmas gift and i have no idea how to fix this have tried numerous things

    I'm trying to order a calendar off iphoto and have never had any issues, now it wont allow me to input a canadian address for ordering  help help help this is an xmas gift and i have no idea how to fix this have tried numerous things

    No I didn't even read through your post, to long.
    You said that "No matter what program I try to install, as soon as the installer opens it crashes". Were you talking about the install for this/these 3D programs or ANY program? As it is worded it sounded like ANY program. That tells me there is a problem with your Mac, software or hardware.
    Now you say others install programs, which is it. Programs other then the 3D ones install or no programs install?
    Could simply be your Mac is to old to run these programs and the installer knows that. You never said what model or year Mac it is.

Maybe you are looking for

  • Miro - purchase order

    Hi, I ve the following issue: I started using MIRO 5 months ago. When I select lines through delivery note parameters I also get back notes from the last year (which have the same number) even if I set a different period (burt unfortunately the refer

  • Cannot find the servlet--can anyone pls help

    Hi, I am trying to run an example servlet the code is as follows: Example.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <Title>Example</Title> </HEAD> <BODY> <form action="\example\classes.ShowParameters" method="p

  • Is it possible to convert .doc to pdf  ?

    Hi.. I am very new to Livecycle Workflow...! I just want to know whether it is possible to convert .doc to pdf  ? Just by googling i came to know that CreatePDF2 Service will do this Conversion ! But it throws me an Exception like "Conversion Excepti

  • SMSY Technical System Delete and Clean-up

    I need to completely delete a SID from Solution Manager: SMSY technical system, projects, logical components,ChaRM, CCMS, tansport routes, SMD, SLD, etc. I am looking for clean-up documentation. Is there a document(s) which will describe a logical pr

  • Adding Editable Web Galleries

    I would like to add a web gallery to my website but Bridge CS4 does not allow the editability i would like. ie I would like to add a navigation menu and change fonts and add text so that the web gallery html syncs with the rest of my site. Any idea i