First program in PL/SQL - oracle

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

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.

Printing VBScript variables in QTP UFT

How can I check values of my variables at run time when using QTP UFT?
I simply want to create a variable, do logic and fill it with data, set a breakpoint in the line following the variable and then check or output its value somewhere.
I have tried:
print variableName
WScript.Echo variableName
The first produces error: Print function type mismatch
The second produces error: Object required: "WScript"
I'm not sure where the problem lies as I've just started to get into both UFT and VBScript (mostly did C# and javascript and everything is quite different here). Could someone tell me the correct solution and perhaps also explain these errors to me?
If you wanted to see all variables use the debug viewer in QTP.
View -> Debug Viewer
There you can list all the variables you want to watch. You should be able to see them in break points.
Note : Ensure you have Windows script debugger installed to use the debug viewer.
You can also add the variable to watch .. Insert a breakpoint>> start the script then right click on the variable under test and add it to watch(Add to Watch) . After Adding you will see a watch window and the value of the variable will be displayed.
I obsess over my variables... so much so that I sprinkly my code with print statements... Actually, I abstract print into my own UDF (so that I can optionally add logging to a file if needed)...
Here's my function: (it's actually a sub because it doesn't return anything)
Sub say(textToSay)
If talkative Then 'note that if I set global var "talkative" to false, then the whole log is disabled
print textToSay
End If
End Sub
Then, ALL my code typically looks like this...
say "click submit button"
Broswer("WebSite").Page("Search").WebButton("Submit").click
say "check for result"
if not Browser("WebSite").Page("Results").Exist then
say "results page didn't appear after exist timeout"
failtest
else
say "successfully found results page"
end if
Honestly, I'm shocked that your "print variableName" statement gave an error, print is actually available in the VBScript api, so it should have worked.
All of my output goes to the Output pane in UFT. I would give my right-arm to find a way to programmatically clear that output pane between runs, but noone seems to know how to do it.
The benefit of all this logging is that I can watch my code run and see EVERY branch taken by the code, and I can add statements to print my variables.
Here's an example that shows how I would answer your question:
result = browser("WebSite").Page("Results").WebElement("Result").GetROProperty("innertext")
say "result:" & result
if result = "Approved" then
Reporter.ReportEvent micPass, "Check for approved", "Approved!"
else
Reporter.ReportEvent micFail, "Check for approved", "NOT Approved!"
End If
Notice the say statement in there - I'll be able to see that immediately during code execution without waiting until the results are shown at the end.

Compiling and using a function in SQL developer

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.

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.

Resources