Compiling and using a function in SQL developer - oracle

You might not believe it but I searched the questions on stackoverflow to find an answer for the following question:
I can compile this function
create or replace function cart_distance(x1 in number,x2 in number,y1 in number,y2 in number)
return number is
begin
return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
end;
But if I want to test it the compiler gives the following error "Encountered the symbol SELECT"
SELECT cart_distance(2,3,4,5) FROM DUAL;
The function and the query are written in the SQL worksheet and I press the "Run statement" option. (I'm very unexperienced with the program)

Try Selecting the query text and pressing the "Run" button. From the error which you have posted, I understand that it is executing the function compilation in the same Run as executing the query. Or better - try in a separate worksheet. Make sure your connection is correct.

Related

Show only input from user at runtime with sql developer and not the whole code

This is an example and i know this post too:
How to get input from user at runtime
When i run this code in sql developer with pl/sql:
DECLARE
a NUMBER;
BEGIN
a:=&a;
DBMS_OUTPUT.PUT_LINE(a);
end;
I get a prompt for my user input value and i see my input value on the script output.
Ok this works.
But i see the whole code and my input value.
A solution for exactly this example, no sqlplus, prompt or accept and not this post:
Provide a message prompt for user input in SQL Developer 3.1.07
Is it possible to deactivate the whole code output and only see my input value, is there a sql developer setting?
I'm using:
Oracle SQL Developer
Version 21.4.1.349
Build 349.1822
On a Windows 10 PRO
Oracle Database
21c Express(21.0.0.0.0)
Manage your output panel in SQL Developer:
Turns on|off verification
The VERIFY setting controls whether or not SQL*Plus displays before and after images of each line that contains a substitution variable.
SET VER[IFY] {OFF | ON}
Turns on|off feedback completely
The FEEDBACK setting controls whether SQL*Plus displays the number of records returned.You can set a threshold, below which you don't get any feedback regardless of whether the setting is on.
SET FEED[BACK] {OFF | ON | row_threshold}
Some Links:
set feedback off
SET FEEDBACK
SET VERIFY
Substitution variables, like your &a, are exclusively an SQL*Plus concept. The only reason SQL Developer knows what to do with it is that SQL Developer supports most of the SQL*Plus scripting language. So your "no sqlplus" requirement (in your question) is nonsensical. Any solution must be a SQL*Plus solution in one way or another.
What you are asking for is very simple. SQL*Plus supports a feature called verify, which does exactly what you don't want it to do: showing you the before and after versions of your code. By default it is on - all you need to do is to turn it off before you run your code.
Another thing is the displaying of just your value in the output. By default the serveroutput feature is off; to see the output from dbms_output.put_line() you must turn it on. It is not clear if you have done that.
So, before you run your code, you must run these two SQL*Plus commands; the first one is the one you were asking about, the second one is to see the output from put_line(). Note that both are SQL*Plus commands; they are not terminated with semicolon (although SQL Developer will not throw an error if they are).
set verify off
set serveroutput on
As an aside, SQL*Plus tolerates some syntax errors, so your code will run as written (unless it's surrounded by other code); but technically, you are missing a forward slash on a line by itself at the end of your code, to indicate the end of a PL/SQL block.
In the editor window:
set verify off
set serveroutput on
DECLARE
a NUMBER;
BEGIN
a:=&a;
DBMS_OUTPUT.PUT_LINE(a);
end;
/
Pop-up window asks for value for a; I enter the value 5 and hit the OK button
Script output (lower pane):
5
PL/SQL procedure successfully completed.
If your next question is "how do I make it so that PL/SQL procedure successfully completed does not appear in the output window" - the answer is similar:
set feedback off
(also a SQL*Plus command, understood by SQL Developer, and not terminated with a semicolon).

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.

First program in PL/SQL

I'm trying to learn some PL/SQL but I'm having an issue with my first program:
declare
v_string_tx varchar2(256):='Hello World!';
begin
dbms_output.put_line(v_string_tx);
end;
When I run this in SQL Developer I just get a message saying 'anonymous block completed'. However I don't get the 'Hello World!' as expected. Anyone know what I'm missing?
I've tried placing the line 'set serveroutput on' before this code but when I do that and run it all, nothing happens(I don't even get the message telling me that the anonymous block has completed).
To see your output in SQL/Developer, you'll have to do this:
go to View -> DBMS Output (this will open a new tab called "DBMS Output")
click the green + sign
choose your connection from the drop-down box (this will add a new sub-tab to "DBMS Output")
run your script

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 Error: Missing IN or OUT parameter at index:: 1"

I have an Oracle script that looks like the following:
variable L_kSite number;
variable L_kPage number;
exec SomeStoredProcedureThatReturnsASite( :L_kSite );
exec SomeStoredProcedureThatAddsAPageToTheSite( :L_kSite, :L_kPage );
update SiteToPageLinkingTable
set HomePage = 1
where kSite = :L_kSite and kPage = :L_kPage;
Supposedly the last statement is a valid use of a bind variable but when I try to run the script I get this on the last line:
SQL Error: Missing IN or OUT parameter at index:: 1
I'm not sure how to proceed here as I'm not especially proficient in Oracle.
I had a similar error on my side when I was using JDBC in Java code.
According to this website (the second awnser) it suggest that you are trying to execute the query with a missing parameter.
For instance :
exec SomeStoredProcedureThatReturnsASite( :L_kSite );
You are trying to execute the query without the last parameter.
Maybe in SQLPlus it doesn't have the same requirements, so it might have been a luck that it worked there.
Based on the comments left above I ran this under sqlplus instead of SQL Developer and the UPDATE statement ran perfectly, leaving me to believe this is an issue in SQL Developer particularly as there was no ORA error number being returned. Thank you for leading me in the right direction.
I think its related with jdbc.
I have a similar problem (missing param) when I have a where condition like this:
a = :namedparameter and b = :namedparameter
It's ok, When I have like this:
a = :namedparameter and b = :namedparameter2 (the two param has the same value)
So it's a problem with named parameters.
I think there is a bug around named parameter handling, it looks like if only the first parameter get the right value, the second is not set by driver classes. Maybe its not a bug, only I don't know something, but anyway I guess that's the reason for the difference between the SQL dev and the sqlplus running for you, because as far as I know SQL developer uses jdbc driver.
I got the same error and found the cause to be a wrong or missing foreign key. (Using JDBC)
I had this error because of some typo in an alias of a column that contained a questionmark (e.g. contract.reference as contract?ref)
I had issue in SQL Developer because I was using binds incorrectly. Was using this, copied from log:
variable = ?
should be
variable = :variable
Now SQL Developer prompts me for values.
I got the same error sporadically appearing on some user setup, while others were content with the same report. I had my parameters written in altered case and with nordic letters, for example: Henkilö. I changed them to HENKILO, using only upper case and no nordics, and it did the trick.
The driver is some unknown or varying JDBC version to Oracle.
My error desc was originated from some 3rd party bin:
Excel Plugin Error: Failed executing statement (Missing IN or OUT parameter at index:: 4)
SQL Statement failed. Please verify and correct it!

Resources