trying to use trigger - sql

I'm trying to activate a trigger when a new employee is added. the trigger should store the username of the person that has done the insert and the time it happened. Using Oracle database.
So far my code for the trigger is:
CREATE OR REPLACE TRIGGER insert_2
AFTER INSERT ON employees
for each row
DECLARE
vUser varchar(50);
begin
select user into vUser from dual;
insert into audit_cheker1 (date_create, inserted_by)
values (sysdate(), vUser);
END;
/
The trigger works but after I try to insert a new record it doesn't work and tells me error in the trigger.

The error message is telling you your trigger is invalid, that is it contains syntax errors and cannot be compiled. So you need to fix the compilation errors.
There are several ways to find errors. You can run a query:
select * from user_errors
where type = 'TRIGGER'
and name = 'INSERT_2'
/
You could use the SQL*Plus command show errors after the CREATE TRIGGER statement.
Or, as it seems you're using SQL Developer, you could open the trigger in the Object Navigator. You'll see the tab has several panes, one of which is labelled Errors. Open that pane to see what's wrong.
Here is one for free: although sysdate is technically a function it is a special one. It never takes any parameters and calling it with brackets is wrong. Remove these brackets: sysdate().

Related

Oracle Sql trigger not working

I have a very simple trigger, that prints out the username and date when a new row is inserted into the users table. But after it successfully compiled, the trigger didn't get triggered (there was no output in the dbms window). So, I simplified my code until I got this:
CREATE OR REPLACE TRIGGER logger
AFTER INSERT ON USERS
BEGIN
DBMS_OUTPUT.PUT_LINE('User added with name:');
END;
If I run the code in the SQL worksheet (from BEGIN to END), I can see the output, but not when I try to use the trigger. Where is the problem?
There are two options, one is that the trigger is not firing, the other is that it is, but you're not seeing the output.
Assuming you're working in a console program like sqlplus, make sure to do SET SERVEROUTPUT ON before inserting your row.
If that doesn't fix it, then make sure the trigger is firing. Try creating a simple table:
CREATE TABLE logtable ( msg VARCHAR2(30 CHAR));
Next, add this to your trigger,
INSERT INTO logtable( msg ) VALUES ( 'trigger fired' );
Then, try your insert again.

Oracle APEX 5 Select List Error ORA-01400

i have a Problem with Oracle APEX 5.
I have a Popup with Textareas and with a Select List. I can write the Data of the Textareas without Problem into my Database, but i can not write the Data of my Select list into my Database. I alway get the Error ORA-01400 Server Error Insert of NULL imposible.
I also tried to write Hard data in the Table so i Changed the PLSQL to:
begin
INSERT INTO STATUS_TO_CHANGE (CHANGE_ID, CHANGE_STATUS_ID)
VALUES (:P2_CHANGE_ID, '43');
end;
and it worked without Problems :(
Maybe someone can help me :O
I use a own button to save the Textarea and a own button for the select list.
Code of my Dynamic Action:
begin
INSERT INTO STATUS_TO_CHANGE (CHANGE_ID, CHANGE_STATUS_ID)
VALUES (:P2_CHANGE_ID, :P2_STATUS_DESCRIPTION);
end;
Name of the Select List: P2_Status_Description
Select List with save Button .jpg
Error MessageData in the Table Change_Status which is shown in the LOV in the Select list
Thanks to all :)
I can't see images right now.
Anyway: this is, I believe, a "Execute PL/SQL Code" dynamic action, right? You put that BEGIN-END block into the "PL/SQL Code", but - did you put P2_CHANGE_ID and P2_STATUS_DESCRIPTION into Items to Submit? If not, do it and then try again.

SQL Server 2012 using SELECT in trigger breaks table

So let me first admit that I am a SQL Server newbie.
Here's the deal: I'm trying to create a trigger on a table in SQL Server 2012, and whenever I try any kind of SELECT statement in the trigger, the table quits working (as in NOTHING can be inserted until the trigger is deleted). As soon as I drop the trigger, everything starts working again. If I don't do any SELECTs, everything is peachy. Is there a permission or something somewhere that I'm missing?
Example:
CREATE TRIGGER sometrigger
ON sometable
FOR INSERT
AS
BEGIN
SELECT * FROM inserted
END
GO
Command completes successfully, but the table becomes frozen as described above.
CREATE TRIGGER sometrigger
ON sometable
FOR INSERT
AS
BEGIN
EXEC msdb.dbo.sp_send_dbmail
#recipients = N'someaddress#somedomain.com',
#subject = 'test',
#body = 'test body',
#profile_name = 'someprofile'
END
GO
Works like a charm.
You're may be falling foul of the disallow results from triggers option being set to 1, as it should be.
Note the warning on that page:
This feature will be removed in the next version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. We recommend that you set this value to 1.
I suspect that wherever you're running your inserts from is hiding an error message or exception, since you should get:
Msg 524, Level 16, State 1, Procedure , Line
"A trigger returned a resultset and the server option 'disallow_results_from_triggers' is true."
Or, in the alternative, you're working with a database layer that wraps all inserts in a transaction and will roll the transaction back if anything unexpected happens - such as receiving a result set or even just an extra information message saying (x rows affected).
But all of this is dancing around the main issue - you shouldn't be issuing a select that attempts to return results from inside of a trigger. I might have been able to offer more help if you'd actually told us what you're trying to achieve.
If it's the second case, and it's something tripping over the (x rows affected) messages, that can be cured by placing SET NOCOUNT ON at the top of the trigger.
You should never return data from a trigger anyway, mainly for simplicity and maintenance reasons. It's confusing: I did an INSERT but get a resultset back.
If you need to get the values you just inserted, you'd use the OUTPUT clause
INSERT sometable (...)
OUTPUT INSERTED.*
VALUES (...);
This at least tells you that the INSERT gives results.
And it is nestable too as per, say, SQL Server concurrent transaction issue

Is it possible to go back to line after error in PL/SQL?

I have tons of insert statements.
I want to ignore errors during the execution of these lines, and I prefer not to wrap each line seperately.
Example:
try
insert 1
insert 2
insert 3
exception
...
I want that if an exception was thrown in insert 1, it will ignore it and go back to perform insert 2, and etc.
How can I do it?
I'm looking for something like "Resume next" in VB.
If you can move all the inserts to a sql script and then run them in sql*plus, then every insert will run by its own and the script will continue to run.
If you are using plsqldeveloper (you taged it), then open a new command window (wich is exactly like a sql script run by sql*plus) and put your staements like this:
insert into table your_table values(1,'aa');
insert into table your_table values(2/0,'bb');
insert into table your_table values(3,'cc');
commit;
Even though statement (2) will throw an execption, since it's not in a block it will continue to the next command.
UPDATE: According to #CheranShunmugavel comment, add
WHENEVER SQLERROR CONTINUE NONE
at the top of the script (especially if your using sql*plus which there the difault is exit).
You'd need to wrap each INSERT statement with its own exception handler. If you have "tons" of insert statements where any of the statements can fail, however, I would tend to suspect that you're approaching the problem incorrectly. Where are these statements coming from? Could you pull the data directly from that source system? Could you execute the statements in a loop rather than listing each one? Could you load the data first into a set of staging tables that will ensure that all the INSERT statements succeed (i.e. no constraints, all columns defined as VARCHAR2(4000), etc.) and then write a single SQL statement that moves the data into the actual destination table with appropriate validations and exception handling?
Use the log error clause. More information in Avoiding Bulk INSERT Failures with DML Error Logging

Oracle - Trigger is created, but gives error anytime data is updated

I'm creating the following trigger:
CREATE TRIGGER Trigger_UpdateTrainingDelivery
AFTER DELETE OR INSERT OR UPDATE OF STARTDATE
ON TPM_TRAININGPLAN
BEGIN
UPDATE TPM_PROJECTVERSION V
SET TRAININGDELIVERYSTART = (SELECT MIN(STARTDATE) FROM TPM_TRAININGPLAN WHERE PROJECTID=V.PROJECTID AND VERSIONID=V.VERSIONID AND TRAININGPLANTYPE='prescribed')
END;
When I create it, I get a warning:
Warnings: --->
W (1): Warning: execution completed with warning
<---
However, it's still created it anyway. When I then modify a row in TPM_TRAININGPLAN, I get an error:
>[Error] Script lines: 12-12 ------------------------
ORA-04098: trigger 'TPMDBO.TRIGGER_UPDATETRAININGDELIVERY' is invalid and failed re-validation
Script line 12, statement line 1, column 7
Is there something wrong with my trigger? I can run the UPDATE statement in the trigger by itself and it runs fine, so I don't think there's anything wrong with that.
It appears that you are missing a semicolon at the end of your UPDATE statement.
If you query USER_ERRORS, you'll get the same error information that SQL*Plus will give you with the SHOW ERRORS command without needing to have access to SQL*Plus.
SELECT line, position, text
FROM user_errors
WHERE name = 'TRIGGER_UPDATETRAININGDELIVERY'
ORDER BY sequence
What was the error being reported during compilation?
SQL> show errors trigger trigger_updatetrainingdelivery
Ok I figured this out. It's actually a bug in Aqua Data Studio, which I'm using to run the query. For some reason, it doesn't handle semi-colons within the triggers correctly. I will report this bug, but I did find a workaround:
File->Options->General
Uncheck: ';' Statement separator