How to Get the Text of SQL Statement in a Trigger - sql

Do you know how to obtain the text of sql statement inside a trigger?
Thanks

Sorry to be the bearer of sad tidings, but I don't believe you can do that. No triggering event I'm aware of has visibility on the SQL statement text. The triggering events supported by Oracle (11g) are:
An INSERT, UPDATE, or DELETE statement on a specific table (or view, in some cases)
A CREATE, ALTER, or DROP statement on any schema object
A database startup or instance shutdown
A specific error message or any error message
A user logon or logoff
None of these, as far as I'm aware, have access to the text of the SQL statement. Docs here

Related

Insert works in SQL Client but not in my code (SQL7008)

I am trying to perform insert/update statements in a DB2-AS400 database.
I use the jt400 driver, version 9.5 for java 8 in order to be able to connect and dialog with my DB.
In my app, I can perform selects just fine but when I try to insert or update I get the following SQL Error:
[SQL7008] Table not valid for operation.
I have done some research and it seems that it would be a journaling problem on the DB side and not in my code.
What I would like to understand is why am I able to perform insert/update using my SQL Client (DBeaver) on the same table with the exact same user ?
You might try disabling transaction isolation by adding transaction isolation=none to your connection string:
jdbc:as400://systemname;naming=sql;errors=full;transaction isolation=none;date format=iso
Ref: SQL7008 Error - Workaround?

Can I edit data on oracle database automatically when client application closed by human operator?

I have a client aplication work with a database on my server. That application insert/update a table on my database when human operator close application. I need to change some row of my table in database after application update/insert my table and closed.
How can i tell to oracle database execute procedure or trigger when application closed by human operators?
Oracle can understand time of a user close application that work with database? If yes, can i write job or trigger to run at that's time?
If no, have I choice to do that?
My Database: oracle 10g
The trigger is an prcedure call automatically.
In the trigger, you make an AFTER UPDATE or INSERT or both statement.
Then you program the specific row you need to change.
After an insert for exemple Oracle execute the trigger and change the rows.
Hope it helps you a litte bit ;)
You don't have specified much about Application (which is operated by human). And you can trigger procedure or execute procedure in oracle by handling appropriate event in Application (For example Application_End(), Form_Close() event in dot net).

prevent updates, deletes and truncates on database table

I am in the process of creating a sql server 2008 database table for auditing users actions.
Is it possible to create a database table which can only inserted in to - no truncates, deletes or updates allowed on the data in the table. One option I know of is to use a different user with limited rights, but this isnt option for me. So looking at other options?
You need to create a TRIGGER that fires on UPDATE and DELETE and throws an error:
CREATE TRIGGER user_action_update on UserActions FOR UPDATE, DELETE AS
BEGIN
RAISERROR ('Cannot modify or delete user actions', 16, 1)
ROLLBACK TRAN
RETURN
END
GO
Source: http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
Another way to do that is to Write a trigger creation script for the table and set the action to " INSTEAD OF " which will override the triggering action (unwanted action in your case ) for some other code, or null code.
INSTEAD OF Property
Specifies that the DML trigger is executed instead of the triggering SQL statement, therefore, overriding the actions of the triggering statements.
Here is a link in how to Write the SQL statement for the trigger creation:
http://technet.microsoft.com/en-us/library/ms189799.aspx
Good luck
Adrian

ORA-12406: unauthorized SQL statement for policy

I am receiving the following error:
ORA-12406: unauthorized SQL statement for policy
I am executing a stored procedure that executes the following two Oracle Label Security stored procedures:
SA_COMPONENTS.CREATE_GROUP
SA_LABEL_ADMIN.CREATE_LABEL
In the same transaction, but not the same procedure, I am trying to insert into a table using the newly created label. This is when the error occurs. I Googled the error, but the description doesn't help me. I do not know what privilege or authorization I would have to give to make this work. If I split the stored procedure and the insert statement into two separate transactions, it works fine. I am looking for an explanation as to what is going on here.
ORA-12406: unauthorized SQL statement
for policy string Cause: The policy
did not authorize the database session
to perform the requested SQL
statement. Action: Grant the user or
program unit the necessary policy
privilege or additional
authorizations....
From your description and testing it appears that the SA_COMPONENTS and SA_LABEL_ADMIN packages are participating in the transaction (i.e. they insert or update information in system tables that are part of the Label Security architecture but do not commit) and this information needs to be committed prior the application table insert.

SQL Server Triggers Firing Each other Question

I have an auditing trigger that automatically places the time something was updated and the user that updated in fields in all my tables. I have another set of triggers that write event information from updates to an events table. The issue is when someone updates something, the event information is fired twice because of the first triggers. How to I suppress the duplicate entries?
Look into TRIGGER_NESTLEVEL function. It returns the current level of trigger nesting. You can check that to prevent the duplicates.
I guess you have 2 choices:
Either combine both sets of triggers into one.
Or, there is a per database setting that allows recursive firing of triggers to be disabled.
You can also turn off nested triggers all together, if you have enough permissions (some webhosts don't allow you to do this). There is a stored procedure (Sql 2005) called sp_configure that lets you modify the server's configuration. The statement below disables nested triggers, which will stop a trigger from firing another trigger.
exec sp_configure 'nested triggers', 0
Disabling trigger-fires-trigger behavior can also be done in SS2005 by going into SS Mgt Studio and selecting the icon of the server in question, right-clicking, then select "Properties". Then select "Advanced" from the list on the left and set the value for "Allow Triggers to Fire Others" to 'False'.