Problem with "Package Spec" [message #162521] |
Fri, 10 March 2006 14:50 ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
Achilles
Messages: 15 Registered: February 2006
|
Junior Member |
|
|
Hello once again.
I'm developing a master-detail form, so I'm using a "Package Spec" variable to hold the primary key value from the master form. The primary key value for the master is generated by a sequence.
Here's the package spec:
PACKAGE primary_keygen IS
pkey varchar2(15);
END;
Now the master table has the following "Before Insert" Trigger .
CREATE OR REPLACE TRIGGER MASTER_NUM_GEN
BEFORE INSERT
ON MASTER
FOR EACH ROW
DECLARE
primary_key_value varchar2(15);
BEGIN
select lpad(to_char(ref_gen.nextval), 4,'0')
into primary_key_value from dual;
primary_keygen.pkey:='ABC/'||primary_key_value;
:new.Ref_Number:=primary_keygen.pkey;
END;
For the detail block. I have the following "Before Insert Trigger" to generate the primary key values.
CREATE OR REPLACE TRIGGER DET1_NUM_GEN
BEFORE INSERT
ON DETAIL1
FOR EACH ROW
BEGIN
if :new.M_ref_number is NULL THEN
:new.M_ref_number:=primary_keygen.pkey;
ENd if;
END;
Works quite fine if I have only one detail block. But if I have multiple detail blocks. Depending on the user's selection. i.e. After entering data into the master block, the user selects a detail block ('Letter type'- using stacked canvases for this purpose and radio buttons for selecting the view) and then Inserts data into it. here's what I do in the Detail block2.
CREATE OR REPLACE TRIGGER OREF_NUM_GEN
BEFORE INSERT
ON DETAIL2
FOR EACH ROW
BEGIN
if :new.O_ref_number is NULL THEN
:new.O_ref_number:=pkey_gen.master_key;
ENd if;
END;
Now the problem is that When I enter one record into detail1, works fine, but for the second time, when I try to insert another record. the master table gets a new reference number (primary key value) while the detail block gets the previous value that was used in the first record!, so that means 'pkey_gen.master_key' is holding the old value, while in my opinion it should hold the new value, I dont know whats wrong here. If I try to insert two consecutive records into the same detail table, I get an error saying "Unique Constraint voilated", becuase the variable is holding the old values.
And lastly after it inserts the record into the database, I get a dialog box saying, "successfuly inserted 2 records into the database" and when I click ok, the Form closes by itself, any ideas on how to stop this?
I'm really stuck here. Please help me out on this.
Thanks.
EDIT:
I'm using Form6i with Database 10g.
[Updated on: Fri, 10 March 2006 14:56] Report message to a moderator
|
|
|
Re: Problem with "Package Spec" [message #162834 is a reply to message #162521] |
Mon, 13 March 2006 15:21 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
RJ.Zijlstra
Messages: 104 Registered: December 2005 Location: Netherlands - IJmuiden
|
Senior Member |
|
|
Hi Achilles,
I think I see the problem and I would suggest the following:
Instead of
PACKAGE primary_keygen IS
pkey varchar2(15);
END;
Make: (beware of typos, just hitting the keyboard and no testing!)
Create or Replace Package Primary_KeyGen is
function Get_Child_Primary_Key return varchar2;
function Get_Master_Primary_Key return varchar2;
end;
CREATE OR REPLACE PACKAGE BODY primary_keygen IS
pkg_primarykey varchar2(15);
--
function Get_Master_Primary_Key returns varchar2
is
BEGIN
select lpad(to_char(ref_gen.nextval), 4,'0')
into primary_key_value from dual;
pkg_primarykey:='ABC/'||primary_key_value;
return pkg_primarykey;
EXCEPTION
Your code/logic
END Get_Master_Primary_Key;
--
function Get_Child_Primary_Key return varchar2 is
begin
return pkg_priomary_key;
end Get_Child_Primary_Key;
--
end Primary_KeyGen; -- end of pkg
But now for the second part of my answer:
Both mine and your solutiuons will crash in a multiuser envirenment:
Steps in the code for one user:
1) primary_keygen.Get_Master_Primary_Key
2) primary_keygen.Get_Child_Primary_Key
Steps in code for multiuser: (user A and B)
1) A does primary_keygen.Get_Master_Primary_Key
2) B does primary_keygen.Get_Master_Primary_Key
3) A does primary_keygen.Get_Child_Primary_Key
4) B does primary_keygen.Get_Child_Primary_Key
You see the problem? In step 3 A gets the value of B and in step 4 you'll get an erro (duplicate PK)
And the point is, that there is no way you can solve this easily.
HTH,
Regards,
Rob Zijlstra
|
|
|
|
Re: Problem with "Package Spec" [message #163366 is a reply to message #162521] |
Thu, 16 March 2006 07:38 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
Achilles
Messages: 15 Registered: February 2006
|
Junior Member |
|
|
Hello again.
Thanks alot for all your help.
I tried getting the nextval in the "WCR" Trigger, but failed, getting the "ORA-00600" error. Googling for the Error, I found out that I have to upgrade my forms from 6i to some patch 17, so now I'm downloading Oracle Developer Suite 10G. I hope it solves the problem. till then I can just keep my fingers crossed and wait for the download to finish.
Once again thanks alot for your time.
Achilles.
|
|
|
|
Re: Problem with "Package Spec" [message #163916 is a reply to message #163487] |
Mon, 20 March 2006 15:13 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) |
Achilles
Messages: 15 Registered: February 2006
|
Junior Member |
|
|
I tried that as well, but no use.
Thanks for all the help. I'll post back the results of compiling the same form with Oracle Forms 10g, once I'm finished with the download.
Once again thanks alot.
|
|
|