Very simple procedure statement from school slide. I want to know how it works - oracle

create or replace procedure HelloWorld(s varchar)
as
begin
dbms_output.put_line(s);
end;
-------- click execute button now
exec HelloWorld('Hello');
The codes above is from the school slide. The first code is shown as 'procedure created' which means good to go. When I execute it by using 'exec HelloWorld(‘Hello’);', it shows errors. May I ask some of question regarding this code please?
1) Why it does not work when I execute it?
2) I know that 's' is a parameter, and 'varchar' is the datatype for 's'. However, the code requires that print 's'. As I can see, there is nothing to assigned on 's'. Then, how can the code runs the value of 's' in 'dbms_output.put_line(s);'?
3)Can anybody just explain what is the basic function for each single word?

Related

Testing a function in PL/SQL Developer

This is my function:
FUNCTION GET(V_IN IN NUMBER) RETURN VARCHAR2 AS
V_OUT VARCHAR2(1000);
BEGIN
function body
END;
When I right click the function and click on test, I am getting the following:
begin
-- Call the function
:result := pkg.get(V_IN => :V_IN);
end;
How do I substitute a value for this variable V_IN? I need to test it for a number, say 940.
When I try the code:
declare
r varchar2(2000);
begin
-- Call the function
r := pkg.get(940);
end;
I am getting an error:
ORA-01036: illegal variable name/number
Can you suggest various ways of calling this function?
PS:
Tool used: PL/SQL Developer Allround Automations. Version 8.0.1.1502
Oracle Database 11g Enterprise Edition
Once you've clicked on your function and clicked on Test in the context menu a Test window is brought up with the code you've shown. There are two panes in this window - the top pane shows the code which PL/SQL Developer generated to invoke the function, and the lower pane contains a list of the parameters to your function. In the lower pane of the Test window there's a list of the parameters to the function with three columns - Variable, Type, and Value. Type the value you want into the Value column in the row with your parameter's name, then click on the Start Debugger button (top left of the Test window, under the 'Test Script' tab's name), then click on the Run button (immediately to the right of the Start Debugger button).
Best of luck.
"How do I substitute a value for this variable V_IN? I "
When you run the test in PLSQL Developer the bottom pane of the test window is a property list for all the substitution variables. You define the datatype and the input values (when appropriate). Output values are available in this window after the test is run.
"ORA-01036: illegal variable name/number"
Not sure what causes this. It's probably not PLSQL Developer but a bug in your function code (which you have not posted).
I was able to run the function as follows:
declare
v varchar2(1000);
begin
select pkg.get(940) into v from dual;
dbms_output.put_line(v);
end;
Also, as per APC's comment, there is a property list(the small window appearing below the PL/SQL worksheet, having Variable, Type and Value fields). You need to enter the value which you wish to pass in the value field and click on Execute (shortcut-F8). The output will be shown and highlighted in yellow in the same property list window. Click below link to refer the screenshot:
Function Call with single parameter
I assume you are still in pl/sql "Test window", when you modified the original test code to your custom. Pl/sql sometimes is buggy. Open a new "SQL window" and try to run there with dbms_output.put_line() to see the result.

PL/SQL function stops running after loop

I feel like this code has been working all day but all of a sudden it doesn't work anymore and I can't seem to find the solution.
This Loop is inside a big function so I'll only post this:
FOR i in 0..t_strGroups.count
LOOP
newStudentNumber := newStudentNumber || t_strGroups(i) || ' ';
dbms_output.put_line(newStudentNumber); -- this outputs fine
END LOOP;
dbms_output.put_line('test'); --this line doesn't output
When testing where the function stopped working I found that after this loop I can no longer output lines. Does anyone know what might have happened here?
Thanks in advance!
After thinking a bit more about this I found the answer.
In the loop I loop from 0 to t_strGroups.count. Because the first position of my collection is 0. but collection.count returns the ammount of values in the collection.
I have 5 values in my collection so collection.count returns 5. however, the last value in my collection has position 4. So when the loops gets to itteration 5 the code will crash. Since position 5 does not exist.
I guess it is just stupid of mine to overlook this. And I thought about removing this question. But I decided to post this answer. Maybe it can help someone some day!
EDIT:
According to the comment of Jeffrey Kemp the code doesn't crash it raises a NO_DATA_FOUND Exception. So it would also be smart to write an exception handler for that.

ORA-6502 Character string buffer too small error with Execute immediate statement

I get an Oracle error ORA-6502 Character string buffer too small in my code at the statement below
EXECUTE IMMEDIATE 'BEGIN :project_id :=
Activity_API.Get_Project_Id(:activity_seq); END;'
USING OUT project_id_, activity_seq_
project_id_ - this is a local variable in the function
activity_seq_ -- this is an IN parameter to the function.
I don't understand the cause of the error. Besides, the error is not consistently showing up.
Please help me know what am I missing out.
Thanks.
Generally this error means that you have a VARCHAR(N) variable somewhere in your code and you tried to assign VARCHAR(N+x) value to it. It may happens anywhere, say:
size of activity_seq_ is too big for function local variable
size of project_id_ is too small for function result
there is some oversized value used into the function itself
etc.
Sometimes it may happens because of multibyte character set used, say, if value is VARCHAR(N chars) while assignment target is VARCHAR(n bytes). Anyway, you should just debug it. Use PL/SQL Developer or any other tool which can trace stored procedures row by row, run your statement into the test window and see what happens, where and why.

How to find the error causing ora-06575?

Recently I had to write an oracle function, and the error ora-06575 popped up awfully a lot.
Usually it was because of missing colon from assignment, such as: z = 5 (vs. z := 5)
Or a missed ';' at the end of a statement.
Anyway, I was able to create the function, but it failed during execution with that error, and gave no hint as to where the problem was (except that it was in the function).
I wrote same function in MSSQL and Sybase, and both of those actually tried to point me to the place of any errors. So I figure I'm doing something wrong in Oracle - it can't just tell me 'there's an error'.
In oracle I have a statement like this:
CREATE OR REPLACE
FUNCTION...
I'm compiling the function from SQL developer by selecting the function, and pressing F9.
When I select a statement which executes the function, and press F9, I get the ora-06575 error.
If I press F5 to compile the function, it tells me:
ORA-24344: success with compilation error
Compiled.
So I found this website: http://www.dba-oracle.com/t_ora_24344_success_with_compilation_error.htm
But I can't seem to run 'show errors'. When I run it, I get no output that I can see.
Can that only work from sqlplus? I'm using SQL developer, I'd prefer to stick to SQL developer.
Is there something I'm missing? I want it to tell me where the error is.
SHOW ERRORS is a sql*plus command
You can also query the USER_ERRORS view
SELECT line, position, text
FROM user_errors
WHERE name = '<<your_object_name>>'
SHOW ERRORS works in SQL*Developer too (at least in the versions I've used recently, certainly 3.1). You mentioned in a comment that you're connected as SYS, so I really hope you're creating your function explicitly in another schema - I'd avoid this anyway just in case you forget one day, and modifying any of the pre-built schemas (SYS, SYSTEM etc.) is a bad idea. If so you need to prefix the errored object with the schema too:
create or replace function my_schema.x return number is
begin
return sysdate;
end;
/
show errors my_schema.x
When run as a script (F5) this says:
FUNCTION X compiled
Warning: execution completed with warning
3/8 PLS-00382: expression is of wrong type
3/1 PL/SQL: Statement ignored
The first two lines of the output come from the function compilation, the last two from show errors. You can also run the two statements separately with F9 to see the same results.
But I'm not sure how you're getting the ORA-24344, so maybe you're on an earlier version; and it's possible that won't work. A.B.Cade's solution will work whatever your client though.

Oracle SQL Developer - Help for debugging

I am trying to debug a package with in the SQL Developer. The method that i am trying to debug takes 2 parameters
PROCEDURE procedure_name (dblink IN CHAR, bDebug IN BOOLEAN DEFAULT FALSE)
When i click on "Debug" icon, it asks for inputs that i need to give to this procedure. I give
dblink:='linkname';
bDebug:=TRUE;
but when it starts debugging, I see the value of dblink as
'linkname
'
i.e. linkname, lots of spaces and then the ending quote. so when in code i try to do this
`strSrc VARCHAR(120) := 'tablename'||dblink;`
it gives me error that buffer is to small, which makes sense. but why SQL Developer is doing so? how to fix it?
I am guessing your padding is coming from how SQL Developer is defining its variable to bind with (it is probably defining it as a CHAR(4000)). For now, you should be able to get around this in your test code by putting trim() around the dblink variable:
strSrc VARCHAR(120) := 'tablename'||trim(dblink);
Note that this would normally not be needed if the procedure was passed a literal (or a correctly sized CHAR variable, a VARCHAR, etc), like the production code is probably doing.
For debugging purposes, create a wrapper procedure that accepts a VARCHAR2 parameter, then passes it to your procedure; then you debug it in SQL Developer by calling your wrapper.

Resources