Set define off not working in Oracle SQL Developer - oracle

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".

Related

View results of pl/sql stored procedure in Toad?

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

Oracle PL/SQL Trigger asking for binds

I'm trying to create my trigger but it's asking for binds EVERY time. It works the way I want it to when I click apply on the window that appears... However, it will log an error...
My trigger checks to see if a client is active or not and do NOT allow changes if it is found to be active...
CREATE Trigger Client_Activity
BEFORE Insert or Update or Delete ON Client
FOR EACH ROW
DECLARE
VAR_AC char(2);
BEGIN
IF UPDATING THEN
SELECT Activity INTO VAR_AC
FROM Client_Additionals
WHERE Activity = :Old.Activity;
IF Activity = 'AC'
THEN Raise_Application_Error(-20999, 'active')
END IF;
END;
/
ORACLE VERSION 12 USING SQLDEVELOPER
You have two syntax errors in your trigger:
The IF is missing an END IF
You need to compare the content of the variable var_ac
You are missing a ; after the Raise_Application_Error()
Putting that together, you can create the trigger without problems.
However, you need to use the "Run Script" button in SQL Developer to run a PL/SQL block like that.
SQL*Plus requires no special handling:

how to find the query used for creation of Temporary table in Oracle sql developer

I have created a temporary table in oracle sql developer but I forgot to save it and now I want to reuse the query but I don't remember the code used then. Is there a process to get query used creation of temp table?
You can use dbms_metadata.get_ddl()
select dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME_HERE')
from dual;
The result is a CLOB with the complete DDL. You might need to adjust the display in SQL Developer to make the content of that value fully visible (I don't use SQL Developer, so I don't know if that is necessary and if so, what you would need to do)
Edit:
It seems SQL Developer can't display the result of this query properly unless you use the "Run Script" option. And with that you need to use a SET LONG 60000 (or some other big number) before you run it, to see the complete source code:

How to execute a stored procedure which has an output parameter

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.

Creating a trigger in Oracle Express

I was trying to do something like auto-increment in Oracle 11g Express and SQL Developer.
I know very little about Oracle and I am also new to triggers.
I tried running this, but I don't know how to do it properly.
CREATE TABLE theschema.thetable
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));
CREATE SEQUENCE theschema.test1_sequence
START WITH 1
INCREMENT BY 1;
create or replace trigger insert_nums
before insert on theschema.thetable
for each row
begin
select test1_sequence.nextval into :new.id from dual;
end;
/
When I try to create the trigger, I get a screen which asks me for some "binds".
The dialog box has only one check box "null". What does this mean and how do I make
a script that works properly?
Any precautions to take while doing this kind of "auto-increment" ?
It seems that SQL Developer thinks that you are running a plain DML (data manipulation) script, not a DDL (data definition). It also thinks that :new.id is a bindable variable.
Why this happens, I don't know; I can't reproduce it in Oracle SQL Developer 2.1.
Try to open a new SQL worksheet window in the theschema schema and execute a "whole" script (not a statement) by pressing F5 (not F9).
This is how I have solved this problem, put "set define off;" before the command:
set define off;
create or replace trigger [...]
[...]
end;
/
Then highlight both commands and press F9 to run.
Or you could run all the commands with F5.
It seems, that if the commands are executed separetly with F9, then the set define off does not take affect.
For my case, solution was entering "newrow" for 'new' and "oldrow" for 'old' as values for the binds...
I am a novice at this so keep that in mind as I give my answer.
I think the issue is that the code
create or replace trigger insert_nums
before insert on theschema.thetable
for each row
begin
select test1_sequence.nextval into :new.id from dual;
end;
Is actually a script and not straight forward SQL statement. Hence you have to run the "Run Script". I discovered that when I had a worksheet open in SQL Developer that if I had anywhere in the worksheet the any code for trigger like above then even I just tried to run a statement that SQL Developer would look back in the worksheet and try to run the script. To stop that from happening I had to comment out the code.
And If I did want to run the code for the trigger than I had to open a new worksheet, place the code there and do a RUN SCRIPT.

Resources