I am creating a trigger for my database. In my trigger code which number argument should I use :
set serveroutput on
create or replace trigger iss_bk
after insert on Issue
for each row
declare
p integer;
q integer;
begin
select Available_copy
into p
from book
where Book.ISBN = :NEW.book_id;
q := p - :NEW.quantity;
if q < 0 then
RAISE_APPLICATION_ERROR(-1722,' Exceeded available quantity of book .');
else
update Book set
Available_copy = q
where Book.ISBN = :NEW.book_id;
end if;
end;
/
While q < 0 input is given, the error is :
ORA-21000: error number argument to raise_application_error of -1722 is out of range
ORA-06512: at "R1507090.ISS_BK", line 9
According to documentation (pick another version, if needed, but I don't think that much has chanaged). the "Handling PL/SQL Errors" says:
raise_application_error(error_number, message[, {TRUE | FALSE}]);
where
error_number is a negative integer in the range -20000 .. -20999
message is a character string up to 2048 bytes long.
If the optional third parameter is TRUE, the error is placed on the stack of
previous errors. If the parameter is FALSE (the default), the error
replaces all previous errors.
It means that you should use, for example,
RAISE_APPLICATION_ERROR(-20001, 'Exceeded available quantity of book.');
The same ERROR_NUMBER value can be reused, i.e. all your own error messages can share the same "-20001" code, if you want.
Related
What should be the minimum difference between the readbuffer variable size and the max_linesize(specified in UTL_FILE.FOPEN) so the code executes without ORA-29284 error.
For example below is my code:
set serveroutput on;
Declare
f UTL_FILE.FILE_TYPE;
r varchar2(1220);
begin
f:= UTL_FILE.FOPEN('XYZ_DIR', 'ABC.txt', 'R',max_linesize => 1222 );
UTL_FILE.GET_LINE(f,r);
UTL_FILE.FCLOSE(f);
dbms_output.put_line(r);
end;
When I execute the above mentioned code, I get ORA-29284 error. This error is due to the fetched linesize being larger than read buffer in this problem's context.
My file ABC.txt has multiple lines having individual length of 1221 characters. For this scenario getting the 29284 error is valid. But when I strip the line size in file to 1220 character, I still get the same error. If I strip the individual line size in file to 1218, my code works properly.
I want to know the reason why my code fails even though if I strip the individual line in file to 1220 character(same as read buffer) and why do I have to strip the length of lines to 1218 character?
This question already has an answer here:
How to fix run-time error 201
(1 answer)
Closed 5 years ago.
I am having problems with this program. The project is for it to be a cash register.
Program Cash_Register;
var
ItemsPrices: array[1..20] of real;
ItemsNames: array[1..20] of string;
Item_Number: integer;
NameNumber: integer;
PriceTracker: integer; {1}
NameTracker: integer; {1}
To_End_Or_Not_To_End: string;
PriceNumber: integer; {0}
Subtotal: real;
gst_sum: real;
Final_Total: real;
const
GST: real = 0.125; {0.125}
Base: integer = 21; {21}
CR: string = #13; {#13}
LF: string = #10; {#10}
CRLF: string = #13#10; {CR + LF}
begin
{Variable and constant assignment}
PriceTracker:= 1;
NameTracker:= 1;
PriceNumber:= 0;
{This area below starts the name taking and price taking}
while (PriceTracker AND NameTracker) < Base do
begin
{This area below Asks the user for the name of the product}
Writeln('Please enter the name of product ');
write(Item_Number);
write(' please.');
readln(ItemsNames[Item_Number]);
{This area below asks the user for the price of said item}
Writeln('Please enter the price of product ');
write(Item_Number);
write(' please.');
readln(ItemsPrices[Item_Number]);
{This area below imcrements the counter by 1}
Item_Number:= Item_Number + 1;
{This area below asks the user if they want ot continue or not}
writeln('Do you want to stop entering items? [Yes/No]');
readln(To_End_Or_Not_To_End);
{This area below will determine the programs path}
if To_End_Or_Not_To_End = 'Yes' then
continue
else
break
end;
NameNumber:= Item_Number + 1;
PriceNumber:= Item_Number + 1;
Item_Number:= 1;
{This area below defines the code that will create the Subtotal}
while Item_Number < PriceNumber do
begin
Subtotal:= Subtotal + ItemsPrices[Item_Number];
Item_Number:= Item_Number + 1;
end;
gst_sum:= Subtotal * GST;
Final_Total:= Subtotal + gst;
Item_Number:= 1;
{This area below prints the List of items and prices in reciept form}
while Item_Number < NameNumber do
begin
write(ItemsNames[Item_Number]);
write(' Bz$ ');
write(ItemsPrices[Item_Number]);
write(CRLF);
Item_Number:= Item_Number + 1;
continue
end;
{This area below prints a reciept for the customer}
write('Subtotal'#9#9);
write(Subtotal);
writeln('GST tax 12.5%'#9#9 + 'Bz$');
write(gst_sum);
writeln('Total'#9#9 + 'Bz$');
write(Final_Total);
write(CRLF);
writeln('Tips:______________________________');
writeln(CRLF);
writeln('Total:_____________________________');
writeln(CRLF);
writeln('Print Name:________________________');
writeln(CRLF);
writeln('Signature__________________________');
end.
But it compiled and it throws an error at me saying "excited with exitcode 201." I don't want to change the structure and I have no idea what is going on with the compiler as it refuses to run without immediately exiting. What i'm Trying is to see what happens as it is exiting, because I managed to catch a glimpse of the text that should appear on startup. If someone knows what is wrong, do please inform me.
The cause of your problem is staring you in the face, but I suspect you don't know enough yet to realise what it is.
When these lines execute
Writeln('Please enter the name of product ');
write(Item_Number);
write(' please.');
what you see is
Please enter the name of product
0 please.
This is telling you that the value of Item_Number is 0 (zero). Your next statement is
readln(ItemsNames[Item_Number]);
You've declared your ItemNames array as having elements 1 to 20, so there is no ItemNames[0], which is what your readln is trying to read. Same thing with your
readln(ItemsPrices[Item_Number]);
To fix this, assign the value 1 to Item_Number before your while loop begins.
Next, add the statement
readln();
as the very last line of your program (before the end.). That will stop the console window closing before you have a chance to read what your program outputs.
The above should at least get you started on debugging you program. You'll need to learn how to debug the rest yourself. Google yourself some debugger tutorials, e.g. this one https://www.youtube.com/watch?v=LZ90IBa9_8M
Until you get to grips with debugging your own code, you will get absolutely nowhere in Pascal or any other programming language. Others may disagree, but imo it is probably the single most important skill a programmer needs.
I'm still learning PowerBuilder and trying to get familiar with it. I'm receiving the following error when I try to run a program against a specific document in my database:
ct_fetch(): user api layer: internal common library error: The bind of result set item 4 resulted in an overflow. ErrCode: 2.
What does this error mean? What is item 4? This is only when I run this program against a specific document in my database, any other document works fine. Please see code below:
string s_doc_nmbr, s_doc_type, s_pvds_doc_status, s_sql
long l_rtn, l_current_fl, l_apld_fl, l_obj_id
integer l_pvds_obj_id, i_count
IF cbx_1.checked = True THEN
SELECT dsk_obj.obj_usr_num,
dsk_obj.obj_type,
preaward_validation_doc_status.doc_status,
preaward_validation_doc_status.obj_id
INTO :s_doc_nmbr, :s_doc_type, :s_pvds_doc_status, :l_pvds_obj_id
FROM dbo.dsk_obj dsk_obj,
preaward_validation_doc_status
WHERE dsk_obj.obj_id = :gx_l_doc_obj_id
AND preaward_validation_doc_status.obj_id = dsk_obj.obj_id
using SQLCA;
l_rtn = sqlca.uf_sqlerrcheck("w_pdutl095_main", "ue_run_script", TRUE)
IF l_rtn = -1 THEN
RETURN -1
END IF
//check to see if document (via obj_id) exists in the preaward_validation_doc_status table.
SELECT count(*)
into :i_count
FROM preaward_validation_doc_status
where obj_id = :l_pvds_obj_id
USING SQLCA;
IF i_count = 0 THEN
//document doesn't exist
// messagebox("Update Preaward Validation Doc Status", + gx_s_doc_nmbr + ' does not exist in the Preaward Validation Document Status table.', Stopsign!)
//MC - 070815-0030-MC Updating code to insert row into preaward_validation_doc_status if row doesn't already exist
// s_sql = "insert into preaward_validation_doc_status(obj_id, doc_status) values (:gx_l_doc_obj_id, 'SUCCESS') "
INSERT INTO preaward_validation_doc_status(obj_id, doc_status)
VALUES (:gx_l_doc_obj_id, 'SUCCESS')
USING SQLCA;
IF sqlca.sqldbcode <> 0 then
messagebox('SQL ERROR Message',string(sqlca.sqldbcode)+'-'+sqlca.sqlerrtext)
return -1
end if
MessageBox("PreAward Validation ", 'Document number ' + gx_s_doc_nmbr + ' has been inserted and marked as SUCCESS for PreAward Validation.')
return 1
Else
//Update document status in the preaward_validation_doc_status table to SUCCESS
Update preaward_validation_doc_status
Set doc_status = 'SUCCESS'
where obj_id = :l_pvds_obj_id
USING SQLCA;
IF sqlca.sqldbcode <> 0 then
messagebox('SQL ERROR Message',string(sqlca.sqldbcode)+'-'+sqlca.sqlerrtext)
return -1
end if
MessageBox("PreAward Validation ", 'Document number '+ gx_s_doc_nmbr + ' has been marked as SUCCESS for PreAward Validation.')
End IF
update crt_script
set alt_1 = 'Acknowledged' where
ticket_nmbr = :gx_s_ticket_nmbr and
alt_2 = 'Running' and
doc_nmbr = :gx_s_doc_nmbr
USING SQLCA;
Return 1
ElseIF cbx_1.checked = False THEN
messagebox("Update Preaward Validation Doc Status", 'The acknowledgment checkbox must be selected for the script to run successfully. The script will now exit. Please relaunch the script and try again . ', Stopsign!)
Return -1
End IF
Save yourself a ton of headaches and use datawindows... You'd reduce that entire script to about 10 lines of code.
Paul Horan gave you good advice. This would be simple using DataWindows or DataStores. Terry Voth is on the right track for your problem.
In your code, Variable l_pvds_obj_id needs to be the same type as gx_l_doc_obj_id because if you get a result, it will always be equal to it. From the apparent naming scheme it was intended to be long. This is the kind of stuff we look for in peer reviews.
A few other things:
Most of the time you want SQLCode not SQLDbCode but you didn't say what database you're using.
After you UPDATE crt_script you need to check the result.
I don't see COMMIT or ROLLBACK. Autocommit isn't suitable when you need to update multiple tables.
You aren't using most of the values from the first SELECT. Perhaps you've simplified your code for posting or troubleshooting.
My program should allow the user to choose ice cream from menu then add the price for the ice cream to show total price of the ice cream.However my output for total price is wrong. For example if i choose 1 and 2 the answer should be 1.5+1.7=3.2 but I get 3.4. Also if i choose 2 I'll only get error. Please help me.
program iceCream;
var
count,i: integer;
price:array[1..50]of real;
totalPrice: real;
choice: integer;
begin
count:= 0;
writeln ( ' ICE CREAM FLAVOUR');
write ( ' 1.Vanilla: RM 1.50 | 3.Chocolate: RM 2.00');
writeln;
write ( ' 2. Strawberry: RM 1.70 | 0. Exit ');
writeln;
repeat
write ( ' Enter your choice(number): ');
readln ( choice);
if choice <= 3 then
count:= count+1
else
writeln ( 'Invalid choice');
case choice of
1: begin
price[i]:= 1.50;
end;
2: begin
price[i]:= 1.70 ;
end;
3: begin
price[i]:= 2.00;
end;
end;
for i:= 1 to count do
begin
totalPrice:= totalPrice+price[i];
end;
until choice = 0;
writeln ( ' Total ice-cream: ', count);
readln;
writeln ( ' Total price: RM ', totalPrice:2:2);
readln;
end.
I won't answer your question here. It's more important to find out what you're doing wrong and how you can solve the problem on your own.
How to find out what's going wrong?
The compiler shows two warnings when it compiles your code:
Warnings: 2
project1.lpr(29,17) Warning: Variable "i" does not seem to be initialized
project1.lpr(44,25) Warning: Variable "totalPrice" does not seem to be initialized
This kind of warnings appears when you read a variable without having a value assigned to it. The compiler leads you to the location of the unsafe code. The first warning is located in line 29 in column 17:
1: begin
price[i]:= 1.50; // i has not been set before. So its value is undefined or 0
end;
Is your intention to use set Price[i] here? So why hasn't it been assigned to a value before?
Or do you have a different variable that holds the number of ice creams? Why don't you use that?
Let's take a look at the second warning in line 44 in column 25. It's this line:
totalPrice:= totalPrice+price[i];
// ^
Are you sure totalPrice has been initialized correctly?
Even though you initialize it correctly the program may still doesn't work properly. In that case it helps to debug the application.
Open your application in the Lazarus IDE and press F8 to step through your program. You go from line to during the execution of your program. You will see what's wrong then.
uses crt;
var
i: integer;
stav: integer;
prsten: boolean;
begin
clrscr();
stav:=0;
prsten:=false;
repeat
case stav of
0: begin //Zacatek hry//
writeln('Toto je hra, jsi Princ a jsi v lese.');
writeln('Na krizovatce muzes jit doleva = 1, nebo doprava = 2');
readln(stav);
end;
1: begin
writeln('Potkas draka, ktery vezni krasnou princeznu. ');
writeln('3 = Prepadnout draka, 4 = Promluvit s nim');
readln(stav);
end;
2: begin
writeln('Potkas pocestneho. ');
writeln('5 = Pokracujes dal lesem, 6 = Promluvit s pocestnym');
readln(stav);
end;
3: begin
writeln('Drak je silnejsi nez Ty a tak te rozprasil na popel.');
writeln('Zacni znovu stisknutim klavesy 0.');
readln(stav);
end;
4: begin
writeln('Drak Te vyzve na souboj, ale Ty na nej nejsi jeste pripraven.');
writeln('Musis pokracovat dal lesem. Stiskni 5.');
readln(stav);
end;
5: begin
writeln('Po dlouhe a namahave ceste jsi dorazil do mistni knajpy.');
writeln('Najis a napijes se a pokracujes dal. Kousek od knajpy potkas pocestneho');
writeln('Promluvis s nim. Stiskni 6.');
readln(stav);
end;
6: begin
writeln('Povis mu, ze se pokousis zachranit princeznu pred drakem.');
writeln('On se ti rozhodne pomoci a daruje Ti kouzelny prsten.');
writeln('Nasadit prsten na ruku a pokracovat k drakovi 7 / Strcit prsten do kapsy 8 a pokracovat k drakovi.');
readln(stav);
end;
7: begin
writeln('Prijdes k drakovi a das se s nim do boje.');
writeln('Draka zabijes a muzes pokracovat k princezne.');
writeln('Pokracovat k princezne, stiskni 9.');
prsten:=true;
readln(stav);
end;
8: begin
writeln('Prijdes k drakovi a das se s nim do boje.');
writeln('Draka zabijes a muzes pokracovat k princezne.');
writeln('Pokracovat k princezne, stiskni 9.');
readln(stav);
prsten:=false;
end;
9: begin
if prsten then
writeln('Princeznu jsi uchvatil a muzes si ji odvest do hradu.');
else
wrtieln('Princezna je rada, ze jsi ji zachranil, ale opovrhuje Tebou.');
readln(stav);
end;
end;
until stav<0;
writeln('KONEC');
readln;
end.
What is causing the fatal error ";" expected but else founded message?
Unlike C, in Pascal a semicolon ; separates statements, it does not terminate them, and the then clause requires a single statement. then WriteLn(...); else is two statements; you want then WriteLn(...) else.
Let's take this opportunity to learn how to read and use error messages to your advantage.
The compiler tells you exactly what the error is (it's a ; before an else, because both of those are mentioned in the error message). It also gives you the exact line number where it's reporting the error; that's the number (usually in parentheses right before the error message, like (from Delphi):
[DCC Error] Project2.dpr(14): E2153 ';' not allowed before 'ELSE'
So the error is happening on line 14 (in my code - your number will be different). Let's look at that line and a few before and after:
if prsten then
writeln('Princeznu jsi uchvatil a muzes si ji odvest do hradu.');
else
wrtieln('Princezna je rada, ze jsi ji zachranil, ale opovrhuje Tebou.');
So look at the error message:
';' not allowed before 'ELSE'
That clearly tells you that the ; in the line before the else is the problem (that's very clear, because it says not allowed), so remove it.
BTW, now you're going to get another error:
[DCC Error] Project2.dpr(15): E2003 Undeclared identifier: 'wrtieln'
I think you should be able to figure that one out; again, the compiler gives you the exact line number.
You're going to get another one, if you've posted your entire code:
[DCC Error] Project2.dpr(18): E2029 Statement expected but end of file found
This is because you've left out the end. that marks the end of a program file in Pascal. If you've not posted your entire code, you may not get it.
It's important to learn to actually read the words when you get an error message from the compiler. In most languages, the messages are clearly worded, and they all have information you can use to try to figure out (or at least narrow down) the problems in your code.