My quest is to create a function that returns a string value from a lookup table given the ID.
If no match occurs, then I want to load that value into the lookup table.
So here's my lookup table... -
-
SQL> select * from bios_lookup;
-
-
BIOS_ID BIOS
-
---------- ----------------------------------------------------------------
-
1 Phoenix - AwardBIOS v6.00PG
-
2 Phoenix ROM BIOS PLUS Version 1.10 A14
-
3 Phoenix ROM BIOS PLUS Version 1.10 A20
-
4 Phoenix ROM BIOS PLUS Version 1.10 A19
-
5 Phoenix ROM BIOS PLUS Version 1.10 A08
-
6 Phoenix ROM BIOS PLUS Version 1.10 A11
-
7 Phoenix ROM BIOS PLUS Version 1.10 A17
-
8 Phoenix NoteBIOS 4.0 Release 6.0
-
9 Default System BIOS
-
10 786B2 v1.11
-
11 Phoenix ROM BIOS PLUS Version 1.10 A02
-
12 Phoenix ROM BIOS PLUS Version 1.10 A05
-
13 SE7520JR23 Production BIOS Version 07.20, Build 0074
-
14 Phoenix ROM BIOS PLUS Version 1.10 A09
-
15 Phoenix ROM BIOS PLUS Version 1.10 A06
-
16 Phoenix ROM BIOS PLUS Version 1.10 A04
-
17 &Phoenix NoteBIOS 4.0 Release 6.0
-
18 PhoenixBIOS 4.0 Release 6.0
-
19 )Phoenix - Award WorkstationBIOS v6.00PG
-
20 Phoenix ROM BIOS PLUS Version 1.10 A03
-
21 Phoenix FirstBIOS(tm) Notebook Pro Version 2.0 for IBM ThinkPad
-
22 Phoenix ROM BIOS PLUS Version 1.10 A18
-
23 OCPRF100- PhoenixBIOS 4.0 Release 6.0
-
-
23 rows selected.
-
-
--And here's my function...
-
SQL> create or replace function bios_no(bios_in in varchar2) return number is
-
2 pragma autonomous_transaction;
-
3 bnum number;
-
4 begin
-
5 select bios_id into bnum from bios_lookup where bios_in = bios;
-
6 EXCEPTION
-
7 WHEN OTHERS THEN
-
8 begin
-
9 select bios_seq.nextval into bnum from dual;
-
10 insert into bios_lookup (bios_id,bios) values (bnum,bios_in);
-
11 commit;
-
12 end;
-
13 return bnum;
-
14 end;
-
15 /
-
-
Function created.
-
-
SQL> select bios_no('test') from dual;
-
-
BIOS_NO('TEST')
-
---------------
-
43
-
-
--looks good.....
-
SQL> select * from bios_lookup;
-
-
BIOS_ID BIOS
-
---------- ----------------------------------------------------------------
-
1 Phoenix - AwardBIOS v6.00PG
-
2 Phoenix ROM BIOS PLUS Version 1.10 A14
-
3 Phoenix ROM BIOS PLUS Version 1.10 A20
-
4 Phoenix ROM BIOS PLUS Version 1.10 A19
-
5 Phoenix ROM BIOS PLUS Version 1.10 A08
-
6 Phoenix ROM BIOS PLUS Version 1.10 A11
-
7 Phoenix ROM BIOS PLUS Version 1.10 A17
-
8 Phoenix NoteBIOS 4.0 Release 6.0
-
9 Default System BIOS
-
10 786B2 v1.11
-
11 Phoenix ROM BIOS PLUS Version 1.10 A02
-
12 Phoenix ROM BIOS PLUS Version 1.10 A05
-
13 SE7520JR23 Production BIOS Version 07.20, Build 0074
-
14 Phoenix ROM BIOS PLUS Version 1.10 A09
-
15 Phoenix ROM BIOS PLUS Version 1.10 A06
-
16 Phoenix ROM BIOS PLUS Version 1.10 A04
-
17 &Phoenix NoteBIOS 4.0 Release 6.0
-
18 PhoenixBIOS 4.0 Release 6.0
-
19 )Phoenix - Award WorkstationBIOS v6.00PG
-
20 Phoenix ROM BIOS PLUS Version 1.10 A03
-
21 Phoenix FirstBIOS(tm) Notebook Pro Version 2.0 for IBM ThinkPad
-
22 Phoenix ROM BIOS PLUS Version 1.10 A18
-
23 OCPRF100- PhoenixBIOS 4.0 Release 6.0
-
43 test
-
-
24 rows selected.
-
-
--YES - it works, now create a view with the function embedded...
-
-
create or replace view sms_host as
-
SELECT csd."MachineID", csd."Name0", csd."Model0", csd."Domain0",
-
osd."Caption0",osd."Version0",
-
bios_no(pbd."SoftwareElementID00") as bios,
-
sia."IP_Addresses0",
-
sd."SystemRole0",
-
sdi."User_Name0"
-
FROM TEMP_CSD csd, TEMP_OSD osd, TEMP_PBD pbd, TEMP_SIA sia, TEMP_SD sd, TEMP_SDI sdi
-
WHERE csd."MachineID" = osd."MachineID"
-
AND osd."MachineID" = pbd."MachineID"
-
AND pbd."MachineID" = sia."ItemKey"
-
AND sia."ItemKey" = sd."MachineID"
-
AND sd."MachineID" = sdi."ItemKey";
-
-
SQL> select * from sms_host where rownum < 2;
-
select * from sms_host where rownum < 2
-
*
-
ERROR at line 1:
-
ORA-06503: PL/SQL: Function returned without value
-
ORA-06512: at "NETTEST.BIOS_NO", line 14
-
Why doesn't this work?????
(I resolved the issue below, but still am curious why the original failed) -
-
create or replace function bios_no(bios_in in varchar2) return number is
-
pragma autonomous_transaction;
-
bnum number;
-
begin
-
select nvl(bios_id,0) into bnum from bios_lookup where bios_in = bios;
-
if bnum = 0 then
-
begin
-
select bios_seq.nextval into bnum from dual;
-
insert into bios_lookup (bios_id,bios) values (bnum,bios_in);
-
commit;
-
end;
-
end if;
-
return bnum;
-
end;
-
/
-
3 4550
Your function only returns a value if it gets into the exception block. when it finds the passed in value in the lookup table nothing is returned, thats why your getting the no value returned error i believe.
try testing the function with a value that exists and see what happens.
Yes Dave is right. You have used RETURN statement in the EXCEPTION block. You need to use the return statement before your exception block,ie in the main begin block itself.
Saii 145
Expert 100+
In addition to above solution, I just want to point out that try using no_data_found exception instead of generic when others exception to populate a new value, unless you want to populate new row on every exception thrown by the code.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sam |
last post by:
Guys,
I have a question regarding oracle.
I have a stored procedure executing a number of statements including
delete , select and lots of inserts in one big procedure and main
transaction
I...
|
by: Dan Loomis |
last post by:
Hi guys - I have a weird condition here I'm trying to get resolution
on, and unfortunately I'm not getting anywhere with my friends at
Oracle. I'm hoping it's something simple, or at the very...
|
by: Tim Smith |
last post by:
I have a lot of code which follows the general pattern below.
Is there an easy (or hard) way of avoiding repeating the same code
over and over. I do need to manage the connection at this level,...
|
by: Steven T. Hatton |
last post by:
I read Stroustrup's article of the day:
http://www.research.att.com/~bs/C++.html
Programming with Exceptions. InformIt.com. April 2001.
http://www.research.att.com/~bs/eh_brief.pdf
Some of...
|
by: craig |
last post by:
I am wondering if there are some best practices for determining a strategy
for using try/catch blocks within an application.
My current thoughts are:
1. The code the initiates any high-level...
|
by: tolisss |
last post by:
Hi
i have setup a global exception handler b4 Application.Run like
Application.ThreadException += new
ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException
);
then after...
|
by: Daniel Daoust |
last post by:
Hi, knowing that "autonomous transaction" (Oracle
concept of) are not yet implemented in PostgreSQL, has
anyone found a work-around. I need to preserve
database states from a potential rollback...
|
by: dilippanda |
last post by:
Hi Experts,
I want to know what is the use of pragma autonomous transaction in PL/SQL.
In which condition should we use this?
Please guide me with an example.
Thanks,
Dilip
| |
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |