I’m using PL/SQL, and I would like to check if a record exists in a table, I’ve tried using when exists or if not exists, but I end up with exception.
Select name from nameTable where name = nameFromArgument;
When the procedures starts, and I put in a wrong name, which is not in the database I’m getting an error, can’t even handle it
I’ve tried something like this:
PL/SQL check if query returns empty
edit:
sorry, the select I’ve put before was just an example, the whole code [ sorry for names of variables… ] is here:
CREATE OR REPLACE
PROCEDURE SELLSTOCKS
( IloscAkcji IN NUMBER
, NazwaAkcji IN VARCHAR2
, IdRachunkuMaklerskiego IN NUMBER
) AS
Bledna_ilosc EXCEPTION;
Bledna_nazwa EXCEPTION;
amount NUMBER;
stocksCash number;
idRachunku number;
stocksOnDealerSide number;
temp number;
tempus akcje_uzytkownika.nazwa % type;
BEGIN
--Select nazwa into tempus from akcje_uzytkownika where nazwa = nazwaakcji;
--dbms_output.put_line(tempus);
dbms_output.put_line('pre');
select nazwa into tempus from akcje_uzytkownika where nazwa = nazwaakcji;
dbms_output.put_line('pre2');
if tempus!=null then
if ( iloscakcji> amount) THEN
raise Bledna_ilosc;
else
select ilosc, cena_sztuki into amount, stockscash from akcje_uzytkownika where nazwa= NazwaAkcji and id_rachunku_maklerskiego= IdRachunkuMaklerskiego;
dbms_output.put_line(amount);
dbms_output.put_line(stockscash);
amount :=amount - iloscakcji;
dbms_output.put_line(amount);
update akcje_uzytkownika set ilosc= amount where nazwa=NazwaAkcji and id_rachunku_maklerskiego= idrachunkumaklerskiego;
stockscash:= iloscAkcji * stocksCash;
dbms_output.put_line(stockscash);
select id_rachunku into idrachunku from rachunek_maklerski where id_rachunku_maklerskiego= idrachunkumaklerskiego;
temp:=SALDOUPDATE(stockscash, idrachunku);
select ilosc into stocksOnDealerSide from akcje where nazwa = nazwaAkcji;
stocksOnDealerSide := stocksondealerside+ iloscakcji;
dbms_output.put_line(stocksOnDealerSide);
update akcje set ilosc = stocksondealerside where nazwa = nazwaAkcji;
end if;
else
dbms_output.put_line('post');
end if;
EXCEPTION
when Bledna_ilosc then
dbms_output.put_line('Bledna ilosc');
when Bledna_nazwa then
dbms_output.put_line('Bledna nazwa');
END SELLSTOCKS;
I’m using oracle sql developer 1.5.5
BEGIN
--Select nazwa into tempus from akcje_uzytkownika where nazwa = nazwaakcji;
--dbms_output.put_line(tempus);
dbms_output.put_line('pre');
select nazwa into tempus from akcje_uzytkownika where nazwa = nazwaakcji;
dbms_output.put_line('pre2');
exception when no_data_found then
raise no_data_found;
-- is this the right place?
select ilosc, cena_sztuki into amount, stockscash from akcje_uzytkownika where nazwa= NazwaAkcji and id_rachunku_maklerskiego= IdRachunkuMaklerskiego;
dbms_output.put_line(amount);
dbms_output.put_line(stockscash);
amount :=amount - iloscakcji;
dbms_output.put_line(amount);
if ( iloscakcji> amount) THEN
raise Bledna_ilosc;
else
update akcje_uzytkownika set ilosc= amount where nazwa=NazwaAkcji and id_rachunku_maklerskiego= idrachunkumaklerskiego;
stockscash:= iloscAkcji * stocksCash;
dbms_output.put_line(stockscash);
select id_rachunku into idrachunku from rachunek_maklerski where id_rachunku_maklerskiego= idrachunkumaklerskiego;
temp:=SALDOUPDATE(stockscash, idrachunku);
……..
You have four main things that are wrong (that I can see).
Firstly,
tempus!=nullwill never work. As null is the absence of information you can’t use the (in)equality operator when checking for nulls. Non-existence != non-existence. The correct syntax is to useis null, or in your casetempus is not null.Secondly,
if ( iloscakcji> amount) THENwill never evaluate to true as you don’t initialise the valueamount. A number cannot be greater thannullas it doesn’t exist. You have to initialise the variableamount. Maybe you meant the next select statement to be above this?Thirdly, all of your
select ... into ...statements are into single variables – not user defined table-types. This means that if 0 rows are returned by your query ano_data_foundexception will be raised. If more than one row is returned atoo_many_rowsexception will be raised.If you’re only expecting at most one row then you don’t need to worry too much about
too_many_rows. This should really be enforced by a unique constraint on your table if possible. For example I would expect thatnazwashould be unique onakcje_uzytkownika.no_data_foundis slightly different and is, I expect, what you meant to bytempus != null. Assuming thatamounthas been initialised to a value this can be re-written as:Lastly, and it’s not really a mistake but, your code is fairly unreadable. I would consider using a lot more whitespace so that you can see what’s going on.
P.S. By Oracle version I meant 11gr2 or 10g etc but it doesn’t make any difference in this case.