Creating a trigger in Oracle Express - oracle

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.

Related

How To Enable dbms_output in PL/SQL Developer

I know it's a simple thing but I can't seem to find a solution, how do I enable dbms_output in PL/SQL Developer. From googling all I got was how to enable it in sql developer but thats not what I'm looking for...
I thought it should be automatically enabled in PL/SQL Developer but for example this block outputs nothing for me.
declare
v_sample employees.first_name%type;
begin
select first_name
into v_sample
from employees
where employee_id = 100;
dbms_output.put_line(v_sample);
end;
By default, PL/SQL Developer automatically retrieves DBMS_OUTPUT and you don't have to do anything extra to retrieve it. You do not need to use a Command Window to see output. (And you should generally avoid the Command Window - it's a terrible way to program.)
Go to Configure --> Preferences --> Oracle --> Output, and ensure that the "Enabled" button is checked:
Another possibility is that the FIRST_NAME value is null and there is nothing to output. To avoid that confusion, I typically add a hard-coded value before outputting variables:
dbms_output.put_line('Name: ' || v_sample);

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:

Running PL/SQL script in PHP

I have a script in PL/SQL , that drop some tables , sequences , triggers, and creating them again in the same script , I need this script in my website for deleting all data from database (and I want to acces this script pressing one button on website) so I tried to do a function / procedure in PL/SQL that read line by line from the script file and executing every line with dynamic sql , everything went ok till one error at:
CREATE OR REPLACE TRIGGER players_bir BEFORE INSERT ON players FOR EACH ROW BEGIN SELECT players_seq.NEXTVAL INTO :new.id FROM dual END
this gave me the error:
ORA-24344: success with compilation error
I searched for solutions, but I didn't find anything.
You are missing the semi-colon after dual on your SELECT statement. This ORA error generally means you compiled some code, but that is has some issue.
CREATE OR REPLACE TRIGGER players_bir
BEFORE INSERT ON players
FOR EACH ROW
BEGIN
SELECT players_seq.nextval
INTO :new.id
FROM dual;
END
Looking in all_errors will show you what the issue is or you could manually compile this trigger in some IDE (like SQL Developer) and that should also make it obvious.
SELECT *
FROM all_errors e
WHERE e.name = 'PLAYERS_BIR'
AND e.type = 'TRIGGER';
Additionally if all you are using your trigger for is to set some identity column and you are on Oracle 12c you can use some of the new features to accomplish this. Triggers are inherently slow and the new identity feature performs about as good as referencing the sequence directly in your INSERT.

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:

Set define off not working in Oracle SQL Developer

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

Resources