This question answers about calling an Oracle stored procedure: How to execute an oracle stored procedure?
However I have a procedure which takes one IN and one OUT parameter. What should my test syntax in Oracle SQL Developer look like to define a variable, run a stored procedure, and output the result of that variable?
I'm trying this but declaring the variable gives me an error:
begin
xyz MY_TABLE.EMAIL_ADDRESS%TYPE := NULL;
myPackage.GetEmailForId('12345',xyz);
end;
declare
xyz MY_TABLE.EMAIL_ADDRESS%TYPE;
begin
myPackage.GetEmailForId('12345',xyz);
dbms_output.put_line(xyz);
end;
What should my test syntax in Oracle SQL Developer look like to define a variable, run a stored procedure, and output the result of that variable?
Create a unit test.
Go to Tools > Unit Test > Select Current Repository and follow the prompts to set up a unit test repository.
Go to View > Unit Test to open the Unit Testing view.
Right-click on tests and choose Create test.
Choose the connection and then pick the procedure you want to test.
Specify the test name and select Create single with dummy implementation then click Next.
Click 'Next' (you don't need any startup).
Specify the values for the parameters then click Finish.
The test should then appear in the Unit Test view and you can right-click and select Run Test.
Oracle's documentation for unit tests is here.
Related
I'm new to Oracle, and I use Toad Data Point to create and test stored procedures.
I created this simple stored procedure:
CREATE OR REPLACE PROCEDURE dummy_sp (
p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
select sysdate, user from dual;
END dummy_sp ;
/
I executed this, and the result from Toad is Executed Successfully.
Now, I would like to view the results of this stored procedure. In Toad Data Point I type the following:
variable mycursor refcursor;
call dummy_sp ( :mycursor );
I get a popup asking for a parameter. I click OK and I get the error ORA-00900: invalid SQL statement.
How can I see the result of SP dummy_sp in Toad Data Point?
In SQL Server I can run exec usp_sales and see the results of a select statement. There has to be something like that in Oracle and Toad, right?
Here you go, using Toad Data Point.
Execute the stored procedure with a bind variable in it, like :mycursor, and then make sure to configure the type as CURSOR and direction as OUT when Toad Data Point prompts you for the bind variable settings.
Here's the result:
Finally, if you wish to avoid the popup for bind variables, you can execute the procedure directly from the object explorer:
Right-click the procedure and choose Operations / Execute Procedure, and Toad will run it, without prompting for data type.
In case you need a workaround while you wait for help with your tool, the default, free IDE for Oracle Database makes this pretty easy.
If you execute the program using the code editor, it will automatically grab any outputs, whether those be OUT params or RETURNs from a function, including your refcursor
Or if your GUI has proper SQLPlus script execution support (SQL Developer does, not sure about your program):
var x refcursor
exec dummy_sp(:x);
print :x;
And the output:
PL/SQL procedure successfully completed.
SYSDATE USER
------------------- --------------------------------------------------------------------------------------------------------------------------------
27-JUN-19 13.58.47 HR
I'm using SQL developer connected to an Oracle database, and I'm trying to create a trigger that looks something like below:
CREATE OR REPLACE TRIGGER my_trigger BEFORE
UPDATE ON ZONE FOR EACH ROW BEGIN :new.LAST_UPDATED_DTTM := SYSTIMESTAMP ;
END;
/
The problem I'm having is that the ":new" causes SQL developer to prompt for the value of the variable "new" (to try to bind the variable).
I tried using:
set define off;
It executes successfully, but I'm still prompted to enter a value for the variable.
Any idea what to do?
The trigger is created as expected if I use the "Run Script" option.
If I try to run the create statement using "Run Statement (Ctrl+enter)" it doesn't work.
So to successfully create the script I had to put only the trigger creation statements in the window and use "Run Script".
With TSQL I'm used to putting some repeatable tests in for my stored procs. Typically this may include putting the db in a particular state, runnings the sproc, validating the state and rolling back. And contrived example might something like this"
BEGIN TRAN
--input for test case
DECLARE #TestName VARCHAR(10) = 'bob'
--insert test row
INSERT INTO tbl (data) values (#TestName)
--display initial state of target row
SELECT * FROM tbl WHERE data = #TestName
--do some useful test
EXEC MyProc
--display the final state of the target row
SELECT * FROM tbl WHERE data = #TestName
--put the db back where it started
ROLLBACK TRAN
Now I'm working with Oracle and PL/SQL and I'm trying to use a some similar pattern to test my work and not finding it obvious to me quite how to do that. I believe there are a few different ways I might accomplish it but haven't gotten anything to actually work. Ideally I would have a single script in which I could run multiple test cases and inspect the result.
I am trying to work in PL/SQL Developer at this point and understand that might have some differences from how it might work in Oracle SQL Developer or elsewhere.
In Oracle, using tools like SQL*Plus and GUI tools like SQL Developer, you have many options :
To execute the statements and procedures in a single session in an order, i.e. using procedural method of PL/SQL, write an anonymous plsql block and execute it as a script.
Most of the GUI based tools have an option like Execute as script or Test Window to execute your scripts individually or embedded in an anonymous block.
Using DBMS_SCHEDULER also you could achieve the same task.
As you are interested in PL/SQL Developer tool product of Allround Automations, you could simply use the test window to test individual objects.
I have documented few useful features of the PL/SQL Developer tool in my blog, please read http://lalitkumarb.wordpress.com/2014/08/14/plsql-developer-settings/
I am developing a series of PL/SQL Stored Procedures in a package that open REF CURSORs that are passed to the caller as OUT parameters for purposes of interoperability with another framework. I am using SQL Developer and recently started looking into using the testing features built into it.
I am looking for a way to validate the results of a REF CURSOR using SQL Developer's Unit Testing framework. I developed some paging logic, and I want to ensure that it is operating correctly so I was hoping I could verify that I have the correct number of rows and that their row numbers are within the range of the appropriate page.
I found a similar question here, but that solution renders the unit tests absolutely useless (at least as far as I can tell) because it doesn't actually perform any validation after running the procedure. If someone could explain how to validate it or show me what I'm missing from the solution from Oracle's Forums, I would appreciate it.
A Process Validation can be used to unit test datatypes that SQL Developer cannot handle.
Create an implementation and link (synchronize) it with a dummy procedure or function.
Set up parameters and results so that the Test completes with a Success
Create a Process Validation with 'User Pl/Sql Code' using the template shown
Template:
DECLARE
l_Cursor SYS_REFCURSOR ;
BEGIN
OPEN l_Cursor FOR '<SQL goes here>' ;
MY_PROCEDURE( l_Cursor ) ;
<do validation>
CLOSE l_Cursor;
IF <not valid> THEN
RAISE PROGRAM_ERROR ;
END IF ;
END ;
We have done something similar in PL/SQL (although not with the Unit Testing Framework of SQL Developer) by comparing the XML representations of two REF CURSORs:
define a "expected" query that returns your expected output as a REF CURSOR (typically selecting some constants from dual)
run both your "expected" and your "real" query, and convert their output to XML / HTML / whatever
compare the output
To convert a REF CURSOR to XML, you can use DBMS_XMLGEN, as shown in AskTom on describing a ref cursor
I'm working on a project with a lot of plsql code and would like to add more specific unit-tests to our codebase. Some of the procedures/functions I like to test aren't in the package spec and I have no means to change that.
Is there a way to access these 'private' plsql procedures without adding them to the spec?
The only Idea I had so far, was to compile a special package spec to the DB before the tests, that specifies the procedures under test. I gues that would work, but I wonder if there is a simpler way, some evil secret oracle hack maybe ;-)
I'm testing from Java with JUnit/DBUnit.
BR
Frank
There is a way to do this, providing you are on 10g or higher. It's called Conditional Compilation. This is a highly neat feature which provides special syntax so we can change our PL/SQL code at compilation time.
As it happens I have been using this feature precisely to expose private packages in a spec so I can run UTPLSQL tests against them.
Here is the special syntax:
create or replace package my_pkg
as
$IF $$dev_env_test $THEN
PROCEDURE private_proc;
$END
FUNCTION public_function return date;
end my_pkg;
/
That variable with the double-dollar sign is a Conditional Compilation flag.
If I describe the package we can only see the public package:
SQL> desc my_pkg
FUNCTION PUBLIC_FUNCTION RETURNS DATE
SQL>
Now I set the conditional flag and re-compile the package, and as if by magic ...
SQL> alter session set plsql_ccflags='dev_env_test:true'
2 /
Session altered.
SQL> alter package my_pkg compile
2 /
Package altered.
SQL> desc my_pkg
PROCEDURE PRIVATE_PROC
FUNCTION PUBLIC_FUNCTION RETURNS DATE
SQL>
Privatising the functions is as simple as you think it would be:
SQL> alter session set plsql_ccflags='dev_env_test:false'
2 /
Session altered.
SQL> alter package my_pkg compile
2 /
Package altered.
SQL> desc my_pkg
FUNCTION PUBLIC_FUNCTION RETURNS DATE
SQL>
We can do lots more with conditional compilation. It's covered in the docs. Find out more.
I would be surprised if such a thing existed. The whole purpose of private procedures, functions and variables is that they are not visible to applications outside the package.
As #Robert said, it shouldn't be possible to access anything that is declared only in the package body outside of that package. Furthermore, creating a "special" spec for the purpose of running unit tests may not work either: if the body contains forward declarations (statements like those in the spec, usually found at the beginning of the body), then the "special" spec will conflict with those declarations and the package won't compile.
You can use pl/sql developer for testing the pl/sql procedures.
1) Go to the packages--> procedures/functions
2) Right click and select "test"
3) Enter the input parameters and click execute/run button and verify the results.
4) you can run with varieties of data sets and check in target tables.
5) Run with invalid data and check for the expected errors.
you can get more details at
http://www.handyinsight.com/2016/06/database-testing.html
temruzinn