It is not working when use trigger to check data from other table.

Please help me with this, I have put a trigger on a table but it can not work as I expect.
case study: one class has many students, only one of them goes to match.
The purpose of this trigger is to check when choose a student goes to match, this student must in his class where he belongs to.
Version of Oracle is 10.2.0.1.0.
--table:
DROP TABLE STU;
DROP TABLE CLASS;
create table CLASS(
CID     VARCHAR2(5)   PRIMARY KEY,
CNAME   VARCHAR2(20)  NOT NULL,
SCHOSEN VARCHAR2(5));
create table STU(
SID     VARCHAR2(5)   PRIMARY KEY,
SNAME   VARCHAR2(20)  NOT NULL,
CID     VARCHAR2(5)   NOT NULL REFERENCES CLASS(CID) ON DELETE CASCADE);
--data:
--CLASS
INSERT INTO CLASS(CID,CNAME) VALUES(1,'SUN');
INSERT INTO CLASS(CID,CNAME) VALUES(2,'MOON');
INSERT INTO CLASS(CID,CNAME) VALUES(3,'EARTH');
--STU
INSERT INTO STU VALUES(1,'JACK',1);
INSERT INTO STU VALUES(2,'TOM',1);
INSERT INTO STU VALUES(3,'LILY',2);
INSERT INTO STU VALUES(4,'DUSTIN',3);
--TRIGGER
CREATE OR REPLACE TRIGGER CHECK_SCHOSEN
BEFORE INSERT OR UPDATE OF SCHOSEN ON CLASS
FOR EACH ROW WHEN (NEW.SCHOSEN IS NOT NULL)
DECLARE
DUMMY INTEGER;
INVALID_STU EXCEPTION;
VALID_STU EXCEPTION;
MUTATING_TABLE EXCEPTION;
PRAGMA EXCEPTION_INIT(MUTATING_TABLE, -4091);
CURSOR DUMMY_CURSOR (ST VARCHAR2, CL VARCHAR2) IS
  SELECT SID FROM STU, CLASS
  WHERE STU.SID=ST AND STU.CID=CLASS.CID AND CLASS.CID=CL
    FOR UPDATE OF CLASS.SCHOSEN;
BEGIN
  OPEN DUMMY_CURSOR(:NEW.SCHOSEN, :NEW.CID);
  FETCH DUMMY_CURSOR INTO DUMMY;
  IF DUMMY_CURSOR%NOTFOUND THEN
    RAISE INVALID_STU;
  ELSE
    RAISE VALID_STU;
  END IF;
  CLOSE DUMMY_CURSOR;
EXCEPTION
  WHEN INVALID_STU THEN
    CLOSE DUMMY_CURSOR;
    DBMS_OUTPUT.PUT_LINE('PLEASE RE-ENTER CLASS ID AND STUDENT ID AS CLASS OR STUDENT IS NOT VALID.');
  WHEN VALID_STU THEN
    CLOSE DUMMY_CURSOR;
    DBMS_OUTPUT.PUT_LINE('STUDENT CHOOSE SUCCEFULLY!');
  WHEN MUTATING_TABLE THEN
    NULL;
END;
/Just copy and paste above and try to run following:
UPDATE CLASS
SET SCHOSEN=3
WHERE CID=1;
Clearly,you can not choose student which is 3 as match member of class 1. Please help me. Thanks.
Edited by: 991096 on 01-Mar-2013 02:36
Edited by: 991096 on 01-Mar-2013 03:03
Edited by: 991096 on 01-Mar-2013 03:11

Hi,
Welcome to the forum!
Like the others, I can't see a business purpose for this trigger. I assume it's just an experiment to learn how triggers work.
991096 wrote:
Please help me with this, I have put a trigger on a table but it can not be triggered.Sorry, I don't understand what you want help with.
Are you saying the trigger doesn't fire? It does fire; it just raises the mutating table error, so you don't see anything.
case study: one class has many students, only one of them goes to match.What do you mean when you say "goes to match"?
--table:
DROP TABLE STU;
DROP TABLE CLASS;
create table CLASS(
CID VARCHAR2(5) PRIMARY KEY,
CNAME VARCHAR2(20) NOT NULL,
SCHOSEN VARCHAR2(5));
create table STU(
SID VARCHAR2(5) PRIMARY KEY,
SNAME VARCHAR2(20) NOT NULL,
CID VARCHAR2(5) NOT NULL REFERENCES CLASS(CID) ON DELETE CASCADE);
--data:
--CLASS
INSERT INTO CLASS(CID,CNAME) VALUES(1,'SUN');
INSERT INTO CLASS(CID,CNAME) VALUES(2,'MOON');
INSERT INTO CLASS(CID,CNAME) VALUES(3,'EARTH');
--STU
INSERT INTO STU VALUES(1,'JACK',1);
INSERT INTO STU VALUES(2,'TOM',1);
INSERT INTO STU VALUES(3,'LILY',2);
INSERT INTO STU VALUES(4,'DUSTIN',3);
--TRIGGER
CREATE OR REPLACE TRIGGER CHECK_SCHOSEN
BEFORE INSERT OR UPDATE OF SCHOSEN ON CLASS
FOR EACH ROW WHEN (NEW.SCHOSEN IS NOT NULL)
DECLARE
DUMMY INTEGER;
INVALID_STU EXCEPTION;
VALID_STU EXCEPTION;
MUTATING_TABLE EXCEPTION;
PRAGMA EXCEPTION_INIT(MUTATING_TABLE, -4091);
CURSOR DUMMY_CURSOR (ST VARCHAR2, CL VARCHAR2) IS
SELECT SID FROM STU, CLASS
WHERE STU.SID=ST AND STU.CID=CLASS.CID AND CLASS.CID=CL
FOR UPDATE OF CLASS.SCHOSEN;
BEGIN
OPEN DUMMY_CURSOR(:NEW.SCHOSEN, :NEW.CID);
FETCH DUMMY_CURSOR INTO DUMMY;
IF DUMMY_CURSOR%NOTFOUND THEN
RAISE INVALID_STU;
ELSE
RAISE VALID_STU;
END IF;
CLOSE DUMMY_CURSOR;
EXCEPTION
WHEN INVALID_STU THEN
CLOSE DUMMY_CURSOR;
DBMS_OUTPUT.PUT_LINE('PLEASE RE-ENTER CLASS ID AND STUDENT ID AS CLASS OR STUDENT IS NOT VALID.');
WHEN VALID_STU THEN
CLOSE DUMMY_CURSOR;
DBMS_OUTPUT.PUT_LINE('STUDENT CHOOSE SUCCEFULLY!');
WHEN MUTATING_TABLE THEN
NULL;
END;
/Thanks for posting the CREATE TABLE, INSERT and CREATE TRIGGER statements; that really helps!
See the forum FAQ {message:id=9360002} for other helpful tips, such as how to use \ tags to post formatted code.
Just copy and paste above and try to run following:
UPDATE CLASS
SET SCHOSEN=3
WHERE CID=1;
Clearly,you can not choose student which is 3 as match member of class 1. The trigger did not be triggered. Please help me. Thanks.You can't do DML, or even query, the class table from a FOR EACH ROW trigger on the same class table.
Add 3 more calls to put_line to see this:CREATE OR REPLACE TRIGGER CHECK_SCHOSEN
BEFORE INSERT OR UPDATE OF SCHOSEN ON CLASS
FOR EACH ROW WHEN (NEW.SCHOSEN IS NOT NULL)
DECLARE
DUMMY          INTEGER;
INVALID_STU      EXCEPTION;
VALID_STU          EXCEPTION;
MUTATING_TABLE      EXCEPTION;
PRAGMA           EXCEPTION_INIT (MUTATING_TABLE, -4091);
CURSOR DUMMY_CURSOR (ST VARCHAR2, CL VARCHAR2) IS
SELECT SID
     FROM      STU
     ,     CLASS
     WHERE     STU.SID          = ST
     AND     STU.CID          = CLASS.CID
     AND     CLASS.CID     = CL
     FOR UPDATE OF           CLASS.SCHOSEN;
BEGIN
dbms_output.put_line (:NEW.schosen || ' = schosen entering check_schosen');     -- ***** NEW *****
OPEN DUMMY_CURSOR (:NEW.SCHOSEN, :NEW.CID);
dbms_output.put_line ('Cursor is open now.');     -- ***** NEW *****
FETCH DUMMY_CURSOR INTO DUMMY;
IF DUMMY_CURSOR%NOTFOUND THEN
RAISE INVALID_STU;
ELSE
RAISE VALID_STU;
END IF;
CLOSE DUMMY_CURSOR;
EXCEPTION
WHEN INVALID_STU THEN
CLOSE DUMMY_CURSOR;
     DBMS_OUTPUT.PUT_LINE ('PLEASE RE-ENTER CLASS ID AND STUDENT ID AS CLASS OR STUDENT IS NOT VALID.');
WHEN VALID_STU THEN
     CLOSE DUMMY_CURSOR;
     DBMS_OUTPUT.PUT_LINE ('STUDENT CHOOSE SUCCEFULLY!');
WHEN MUTATING_TABLE THEN
DBMS_OUTPUT.PUT_LINE ('MUTATING TABLE');     -- ***** NEW *****
     NULL;
END;
Output, when trying to UPDATE class:3 = schosen entering check_schosen
MUTATING TABLE
1 row updated.
Obviously, the trigger fired, since you see the message "3 = schosen entering check_schosen"
Obviously, the OPEN statement raised an error, since you don't see the message 'Cursor is open now.'
Obviously, the mutating table error was raised, since you see the message 'MUTATING TABLE'.
So the trigger fired, and went to the EXCEPTION handler almost immediately.  The EXCEPTION handler printed a message, as instructed, and the trigger ended.  Then the actual UPDATE took place.
I hope this answers your question.
If not, what is your question?  It may be very clear to you, but a complete mystery to others.  Ask clear questions, such as "Why did ... happen?  I thought ... would happen, be ... as the PL/SQL manual says at ...".                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • How do I set up a mail group on mac mail?  All the advise on line seems to refer to 'address book' and I only have 'contacts'.  The guidance does not work when using 'contacts' - can anyone help me?

    How do I set up a mail group on mac mail?  All the advise on line seems to refer to 'address book' and I only have 'contacts'.  The guidance does not work when using 'contacts' - can anyone help me?

    Create a group and send mail
    http://www.dummies.com/how-to/content/how-to-create-a-basic-contact-group-in-mac -os-x-li.html
    http://www.macworld.com/article/1165582/how_to_email_groups_with_mail.html
    Best.

  • Photoshop CC. Colour selection not working when using alt and brush.

    Hi, Photoshop CC. Windows 8.1 64 bit. Colour selection not working when using alt and brush. I have tried rebooting and other things like closing swatch panel etc.
    The alt key is otherwise OK.  It's not the recent upgrade from Win 8 to 8.1 (5 days ago) because the colour selection facility was working yesterday. Any ideas.

    I'd say things sound desperate enough to try resetting Preferences
    Reset Preferences
    Windows — Hold down Shift Ctrl Alt immediately after starting Photoshop
    Mac — Hold down Shift Cmd Opt immediately after starting Photoshop
    If that fails, we need to know your operating system?
    Do you have a full CC subscription, or by apps do you mean Photoshop, Bridge and Lightroom?
    Another other information that might pertain ?

  • Siri does not work when used for calling a contact function

    Siri does not work when used for calling a contact function

    Very strange, try to turn off/on your iPhone. Or make a reset (home+power button)...
    If it continues try to restore it.

  • HT204168 touch screen function not working when using facetime

    Why is my iPad touch screen function not working when using FaceTime?

    Hi megascones,
    I apologize, I'm a bit unclear on the exact nature or scope of the issue you are describing. If you are having issues with the touch screen on your iPad, you may find the troubleshooting steps outlined in the following article helpful:
    If the screen on your iPhone, iPad, or iPod touch doesn't respond to touch - Apple Support
    Regards,
    - Brenden

  • Why is my touch screen function not working when using facetime

    Why is my touch screen function not working when using facetime

    Hello megascones,
    After reviewing your post, it sounds like the screen is not responding to touch in one app. I would recommend that you read this article, it may be able to help the issue.
    If the screen on your iPhone, iPad, or iPod touch doesn't respond to touch - Apple Support
    Restart your device. If you can't restart, reset your device.
    Thanks for using Apple Support Communities.
    Have a nice day,
    Mario

  • Lenovo S8-50LC Wlan(WiFi) Does not work when use Sim Cart(3G)

    Hi
    Tablet Lenovo S8-50LC
    Wlan(WiFi) Does not work when use Sim Cart(3G).
    What is the solution for this problem?
    Build Number : TABS8-50LC_S000200_150121_ROW
    SoftWarw Version : TABS8-50LC_150121

    Right, but if you have the SIM in the tablet and you put the tablet in airplane mode, does wifi work then? If so, it may be a bad SIM. Do you have another SIM you can try?

  • I just installed Photoshop CC 2014, but Latest version of Bridge will not work when I want to open it from PS

    Any idea why is Bridge latest version not working when trying to open it in Photoshop CC 2014 ? (French version) ? When trying, Creative Cloud app will open to ask if I want to download newest version, which I did numerous times now.
    Thank you
    Guy Deschenes
    Shawinigan (QC) Canada

    Hi Eric,
    Here's how a similar problem was resolved between PS and BR.
    The issue was confirmed by ChunXiaYang at Adobe staff and solution put in the forum discussion on June 19 (no. 13). Here's a copy:
    I have confirmed that this is a known issue before release. And the workaround is to uninstall and reinstall PS 2014. Then Bridge can be launched by selecting 'Browse in Bridge' in PS2014.
    Please try the installation, and let me know whether it works. Thanks!
    It woks just fine since I unistalled and reinstalled PS 2014.
    Guy
    Le 2014-07-17 à 1:13 PM, ericboerner a écrit :
    I just installed Photoshop CC 2014, but Latest version of Bridge will not work when I want to open it from PS
    created by ericboerner in Bridge General Discussion - View the full discussion
    I did this with a fresh install of the entire CC package. As soon as the CC packages updated, CC Bridge broke. Unistalled CC PS, reinstalled again, CC Bridge works, but CC PS updates itself again, and breaks CC Bridge.
    You know... The whole move to CC has been a massive cluster... I've struggled with the CC packages for my entire subscription year, and now I'm 1.5 month away from needing to throw down another $720 for no reason what-so-ever, just so we can Beta test your really screwed up subscription services.
    Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at https://forums.adobe.com/message/6559164#6559164
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
    To unsubscribe from this thread, please visit the message page at . In the Actions box on the right, click the Stop Email Notifications link.
    Start a new discussion in Bridge General Discussion by email or at Adobe Community
    For more information about maintaining your forum email notifications please go to http://forums.adobe.com/thread/416458?tstart=0.

  • Why does diadem intellisense not work when using scriptinclude or for global object

    I have a script with a different classes in it (scripted 1).  When I create an object of a class in the script 1 intellisense works.  Intellisense is when you type name of the object created from a class it shows all the possible variables and functions contained in that object.  But when I write another script and use scriptinclude(scripted 1) the intellisense does not work.  I also tried a global object and intesllisense does not work.  Is there a way to get intellisense to work when using scriptinclude?  I am working with version 2012. 

    Here is more information on the issue.
    I have a script with a different classes in it (scripted 1).  When I create an object of a class in the script 1 intellisense works.  Intellisense is when you type name of the object created from a class it shows all the possible variables and functions contained in that object.  But when I write another script and use scriptinclude(scripted 1) the intellisense does not work.  I also tried a global object and intesllisense does not work.  Is there a way to get intellisense to work when using scriptinclude?  I am working with version 2012. 
    'Test.vbs
    class test
        public function test1()
             msgbox("test1")
        end function
        public function test2()
             msgbox("test2")
        end function
    end class
    'now create a object of the class
    dim oTest
    set oTest=new Test
    'now when you type oTest. you get the following functions to choose from
    oTest.test1
            .test2
    Now if you remove the object from Test.vbs and create a main program with scriptinclude(Test.vbs) it will not show the two functions
    'Main.vbs
    Option Explicit  'Forces the explicit declaration of all the variables in a script.
    call scriptinclude("c:\1atmp\Test.vbs")
    dim oTest
    set oTest=new test
    'now when you type oTest. it shows none of the functions in the object oTest
    oTest.

  • Microphone not working when using a phone call, Microphone not working when using a phone call

    Microphone is not working when i use my iphone 4, first day when i installed a iOS 7 , was all good, second day this happened. Please help me. When i use a Viber microphone working. I think this is not a hardwear problem.

    Hi Mike
    That was very helpful thank you.
    I did as you suggested and found the "switch" was already turned on green. I swiched it off waited a while then turned it back on and microphone was back in use in viber. I have a sneaky feeling I may have to do this again in the future.  I am not aware that I did anything that led to the microphone going off, but having said that I have only had the phone for a few days so may have done something when checking it out.
    The phone has been a lot of fun after replacing my ancient Nokia with it. That Nokia worked in Morse code!
    Best wishes
    Rob

  • Keyboard backlighting not working when using Windows in Boot Camp

    Hi, I'm posting this for a friend with a new Macbook Air. He says that his keyboard backlighting does not work when he is using it for Windows (don't know which, but probably Vista). Any ideas why? Thanks. Gary

    Install all the drivers needed when running boot camp - try for example inserting a DVD - then press the eject key - See if it comes out - If it doesn't install all the drivers - try Control Panel - If not try re-installing your version of Windows.

  • Elements 11 - Brush tool not working when using layer mask

    My brush tool won't work when using layer mask in PS Elements 11. I also have Elements version 10 installed and there it works fine. If I do the exact same proces in version 11 nothing happens.
    Is this a bug or has something changed in the proces?
    Please help.
    Thanks.

      Make sure you have the mask thumbnail selected - it will be highlighted in blue.

  • Enter Key Not Working When Using Text Tool

    For some reason, my enter key has stopped working when using the text tool.  Instead of making a soft return to a new line, it simply commits the type. What is going on here?
    I'm using a desktop and CS3.

    there are 2 enter keys in key board
    if u enter main key board enter, sentence will go to next line
    In case u enter the key  available in  rightside of the key board , in the cluster of numericals, the type tool is committed.

  • SQL Insert from "Select Query" Not Work when use Order By

    Hai every body...
    I have a problem with T-SQL. I use SQL Server 2012 Express with SP2 on Windows 7 32-bit SP1.
    This is the problem
    -- first, create table
    create table dtoth.tableA (
    id_data TINYINT PRIMARY KEY,
    kd_data VARCHAR(20),
    nm_data VARCHAR(200)
    -- then, insert values
    INSERT INTO dtoth.tableA VALUES (0,'100.001','KAS');
    INSERT INTO dtoth.tableA VALUES (1,'110.001','BANK');
    INSERT INTO dtoth.tableA VALUES (2,'120.001','PIUTANG DAGANG');
    INSERT INTO dtoth.tableA VALUES (3,'121.001','PIUTANG GIRO');
    INSERT INTO dtoth.tableA VALUES (4,'130.001','PERSEDIAAN BARANG DAGANGAN');
    -- then, i create a temporary table
    create table dtoth.temp_tableA (
    kd_data VARCHAR(20),
    nm_data VARCHAR(200)
    -- then, i create a store procedure to call data from temporary table
    CREATE procedure dtoth.report
    AS
    BEGIN
    DELETE FROM dtoth.temp_tableA;
    INSERT INTO dtoth.temp_tableA SELECT kd_data, nm_data FROM dtoth.tableA ORDER BY kd_data desc;
    SELECT * FROM dtoth.temp_tableA;
    END
    GO
    When i execute the the store procedure with
    EXEC dtoth.report;
    the result is not accurate like this (kd_data not sorted desc):
    I want the column "kd_data" sort descending because i use ORDER BY kd_data DESC in insert statement on the store procedure.
    By the way, if i execute code like :
    SELECT kd_data, nm_data FROM dtoth.tableA ORDER BY kd_data desc;
    the result is correct like this
    So, what solution for my code in the store procedure ?
    Thanks.

    to get the data sorted, you should order by in your select in the stored procedure
    sorting while inserting does not guarenatee order..
    remove the order by in insert statement and put that in the select statement
    so, your procedure should be 
    -- then, i create a store procedure to call data from temporary table
    CREATE procedure dtoth.report
    AS
    BEGIN
    DELETE FROM dtoth.temp_tableA;
    INSERT INTO dtoth.temp_tableA SELECT kd_data, nm_data FROM dtoth.tableA;
    SELECT * FROM dtoth.temp_tableA ORDER BY kd_data desc;
    END
    GO
    Hope it Helps!!

  • HT1430 For some unknown reason my Apple ID password does not work when trying to down load books from the IBook or Nook Apps.  It also has stop work when trying to down load new Apps.  Any suggestions out their???

    For some unknown reason, my Apple ID Password does not work when trying to down load IBooks, Nook books, or new Apps.  Everything else seems to work.  Any suggestions out their???

    I appreciate the info and realize that the Nook App is not related to my Apple account but this too has stopped working.  My situation first started with the Nook App not down loading and then has now spread to my IBook and new app downloads.  I have checked into my ITunes account and the ID and password are correct  and at times when I am asked to submit my Apple password such as setting up this Apple Support Community the ID and password work.  My problem seems to be just with trying to use my ID and password when wanting to download new books or apps.  I can go into the IBook, Nook, and App stores and seemingly download an item but when clicking on the new book or app nothing happens and I get a message that states it can not get into the ITunes store and it wants me to either try again or cancel.  This message appears usually ten minutes after I have tried to make a purchase.  At other times when I attempt to download a book or app I lose the screen and the IPad goes into the opening screen.  Short of redoing my Apple account or deleting an app and attempting to reload the app (but this won't work because the App store won't load new apps) I don't have a clue what to do.

Maybe you are looking for