Is it possible to have insert statement outside of the scope of a transaction in a Stored Proc?
So there is a stored proc that writes some inserts, as part of a transaction
The reason for this is to write to Audit table and keep the audit records even if the stored proc fails or gets an exception
clarification, sorry if it was not clear, I am writing multiple audits for each action of the procedure so I can keep track what it did and what failed. not just a single Audit when there is an exception...
CREATE OR REPLACE PROCEDURE sampleProc
IS
BEGIN
start a transaction
INSERT to table 1
write to audit table about insert 1
INSERT to table 2
write to audit table about insert 2
INSERT to table 3
write to audit table about insert 3
INSERT to table 4
write to audit table about insert 4
if there is an exception - rollback except audit
all ok? commit.
END;
/
Yes, there is an option to perform actions in a separate transaction running simulteneously with your main one. See detail here
Example:
create table logs(creation_date date default sysdate, msg varchar2(4000));
create or replace procedure log_proc(sMessage varchar2)
is
pragma autonomous_transaction;
begin
insert into logs(msg)
values(sMessage);
commit; -- don't forget to commit in this separate transaction
end;
/
begin
log_proc('some message');
rollback;
end;
/
select * from logs
What you're after is PRAGMA AUTONOMOUS_TRANSACTION, which kicks off the module using it in a separate session.
Your code would be something like:
CREATE OR REPLACE PROCEDURE sample_proc
AS
procedure audit_insert (<params>)
is
pragma autonomous_transaction;
begin
<log details>
commit;
end audit_insert;
BEGIN
<INSERT to table 1>;
audit_insert(...);
<INSERT to table 2>;
audit_insert(...);
...
EXCEPTION
when others then
rollback;
raise;
END sample_proc;
/
Doing it like this would mean your audit details would be saved regardless of whether the calling code succeeds or fails.
N.B. I've created the audit_insert as a sub procedure of sample_proc. You would do better to have the code as individual procedures inside a package, rather than as one or more procedures.
Related
I am brand new to SQL, and I am learning on an SQLite editor. So I create a couple of very simple tables. This code is straight from Linkedin learning "SQL essential training", and I am using the recommended SQLite editor.
CREATE TABLE widgetInventory(
id INTEGER PRIMARY KEY,
description TEXT,
onhand INTEGER NOT NULL);
CREATE TABLE widgetSales(
id INTEGER PRIMARY KEY,
inv_id INTEGER,
quan INTEGER,
price INTEGER);
Then I update widgetInventory with some data:
INSERT INTO widgetInventory (description, onhand) VALUES ('rock', 25);
INSERT INTO widgetInventory (description, onhand) VALUES ('paper', 25);
INSERT INTO widgetInventory (description, onhand) VALUES ('scissors', 25);
Next, I want to update the widgetSales table with a sale, and update the widgetInventory table to record the reduction of onhand.
BEGIN TRANSACTION;
INSERT INTO widgetSales (inv_id, quan, price) VALUES (1,5,500);
UPDATE widgetInventory SET onhand = (onhand-5) WHERE id = 1;
END TRANSACTION;
I am not understanding why this gives me an error when I run it, as it is exactly as it is in the lesson.
[06:18:04] Error while executing SQL query on database 'test': cannot start a transaction within a transaction
But, I can run the INSERT and UPDATE lines separately, and they do what I want them to do.
Apparently, running - END TRANSACTION; - before running the entire transaction appears to work.
I think that somehow, SQL thinks that a transaction is already occurring. Though, I'm not sure where exactly. So to stop it, you have to end the transaction first before proceeding with the course.
In the SQLite Editor, you may have to delete or comment out all of the code before and after these two transactions.
BEGIN TRANSACTION;
INSERT INTO widgetSales ( inv_id, quan, price ) VALUES ( 1, 5, 500 );
UPDATE widgetInventory SET onhand = ( onhand - 5 ) WHERE id = 1;
END TRANSACTION;
BEGIN TRANSACTION;
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'toy', 25 );
ROLLBACK;
Otherwise it won't execute the transaction.
Other than that, there is probably an error written in somewhere. Copying and pasting in the .txt file didn't give me that transaction error and could execute the transaction normally.
Just had this same error and my issue was I only highlighted the first line so SQLLite started the transaction but didn't run it fully. All I did was run end transaction, highlight the whole block of code and run that and it worked fine. Must be some syntax issue in Lite that doesn't run the full block itself.
while executing SQL query on database 'test': cannot start a
transaction within a transaction
means a transaction already exists. It may happen if someone forgets to select the END TRANSACTION; statement.
If you face this issue just select END TRANSACTION once and run. With this it will end the active transaction and then you can run any of the existing transaction.
For the particular case of following the Linkedin learning "SQL essential training" course, I have figured out to fix it by running (f9) the "BEGIN TRANSACTION", "...TRANSACTION CONTENTS..." and "END TRANSACTION" statements separately, not all the statements at the same time.
So,
First select the "BEGIN TRANSACTION;" and run it by pressing f9.
Then select the contents of the transactions (I think you can include also the "END TRANSACTION;" part) and run it.
If I have a procedure that is AUTONOMOUS_TRANSACTION that does an insert and then it calls a procedure with an insert, does the second procedure need a commit, or will the procedure with the AUTONOMOUS_TRANSACTION handle the commit?
The answer is NO. The "second" procedure - invoked by the first - does not have to include a COMMIT statement. When you add this statement to the declaration section of a procedure or function...
PRAGMA AUTONOMOUS_TRANSACTION;
the following rule then applies:
Before the subprogram can be closed and control passed back to the
calling block, any DML changes made within that subprogram must be
committed or rolled back.
If there are any unsaved changes, the PL/SQL engine will raise the ORA-06519 exception, as shown below:
CREATE OR REPLACE FUNCTION nothing RETURN INTEGER
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
UPDATE employees SET last_name = 'abc';
RETURN 1;
END;
/
BEGIN
DBMS_OUTPUT.put_line (nothing);
END;
/
ORA-06519: active autonomous transaction detected and rolled back
ORA-06512: at "STEVEN.NOTHING", line 10
ORA-06512: at line 2
OK, so that's the basic idea. Now let's move on the specific question. What if an autonomous transaction procedure calls another procedure, which does not include the pragma shown above but does execute a DML statement and does not commit? Will we see an ORA-06519 error? The code below shows that we will not.
CREATE TABLE me_and_my_lovelies (name VARCHAR2 (100));
BEGIN
INSERT INTO me_and_my_lovelies VALUES ('Grandpa Steven');
INSERT INTO me_and_my_lovelies VALUES ('Loey');
INSERT INTO me_and_my_lovelies VALUES ('Juna');
COMMIT;
END;
/
CREATE OR REPLACE PROCEDURE not_auton_no_commit
AUTHID DEFINER
IS
BEGIN
UPDATE me_and_my_lovelies
SET name = UPPER (name);
END not_auton_no_commit;
/
CREATE OR REPLACE PROCEDURE auton_commit
AUTHID DEFINER
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
not_auton_no_commit ();
COMMIT;
END auton_commit;
/
BEGIN
auton_commit;
END;
/
SELECT COUNT(*) low_name
FROM me_and_my_lovelies
WHERE name <> UPPER (name)
/
LOW_NAME
--------
0
No error is raised. All rows have been updated. So let's go back to the rule:
Before the subprogram can be closed and control passed back to the calling block, any DML changes made within that subprogram must be committed or rolled back.
You might be thinking: But the UPDATE statement (the "DML change") was not made "within" auton_commit. Yes and no. Yes, the UPDATE statement is not part of the text of auton_commit. But the UPDATE statement was executed within the scope of auton_commit. And that's what counts. Any code executed by auton_commit, either "directly" in its executable section or "indirectly" by invoking another subprogram, is part of the autonomous transaction.
The only point at which the rule is applied is when PL/SQL attempts to close auton_commit and return control to the outer block.
LiveSQL script containing the above code here.
More information about autonomous transactions here.
Note: this Q&A was taken from by blog. Full post here.
Does TCL statements come under implicit cursors if they are not declared explicitly? link. Or is COMMIT a PL/SQL statement?
A SQL (implicit) cursor is opened by the database to process each SQL statement that is not associated with an explicit cursor.
set serveroutput on
DECLARE
row_var test%rowtype;
BEGIN
savepoint a;
execute immediate 'delete from test';
DBMS_OUTPUT.PUT_LINE ('No. of deleted '||sql%rowcount);
if (sql%rowcount=0) then
DBMS_OUTPUT.PUT_LINE ('CP1:');
end if;
/*Select * into row_var from test;
DBMS_OUTPUT.PUT_LINE ('No. of after delete '||row_var.testcol);*/
commit; //TCL statment
END;
Select * from test
COMMIT is part of the SQL language. SQL Doc
"Use the COMMIT statement to end your current transaction and make permanent all changes performed in the transaction. A transaction is a sequence of SQL statements that Oracle Database treats as a single unit. This statement also erases all savepoints in the transaction and releases transaction locks."
And can be invoked in a PL/SQL block as a static SQL statement. PL/SQL Doc
"The COMMIT statement makes permanent any changes made to the database during the current transaction. A commit also makes the changes visible to other users....The SQL COMMIT statement can be embedded as static SQL in PL/SQL."
I have started SQlite 2-days ago, and today i tried to learn Transactions in Sqlite3. But i am unable to even run the simplest ever transaction.
Begin;
Insert into newTable(Name,Age) values ("Adnan Ahamd KHan",24)
Insert into tbl2 (Name, FID) values ("Adnan",(Select MAx(ID) from newTable))
END Transaction;
Error Displayed is
cannot start a transaction within a transaction: Begin;
Here we go,
I found the answer to my Questio. Actually i am using DBBrowser for SQlite. And upto my knowledge, you have to commit every statement in DBBrowser for SQlite.
I First created Tables, established Relationship, and without committing them all, i then tried to start that Transaction, that's why it was saying
cannot start a transaction within a transaction: Begin;
What i did, first issued a single
commit
to commit the statements which created tables, and then started Transaction. And it worked fine.
Thanks
I looked into the code and found BEGIN TRANSACTION; starting line and Commit; ending line. I removed both (remove the first and last line which says BEGIN TRANSACTION and Commit/ END TRANSACTION) of them and it works now.
The following failed for me:
BEGIN TRANSACTION;
....
END TRANSACTION;
AND
BEGIN TRANSACTION;
....
Commit;
I have a few sql files which contain thousands of INSERT statements. The problem when executing these scripts is that some of them violate the unique constraint.
I originally did something like this:
BEGIN
INSERT INTO .....;
INSERT INTO .....;
...
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
null;
END;
/
EXIT;
However it seems the first instance where the exception occurs will cause the entire block to end, so the rest of the statements are not executed.
Since the Oracle version I'm using is not 11, I can't use the nice CONTINUE command. So I am thinking of doing something like this for each INSERT INTO statement:
BEGIN
INSERT INTO ....;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
null;
END;
/
...
How can I use sed to read each line in the sql file, scan for the words "INSERT INTO" and if that exists, then append the BEGIN and EXCEPTION lines in between the INSERT INTO line? The reason is, sometimes a line is just a comment and I don't want to append to that.
It's not clear if you are trying to add BEGIN before a block of INSERT INTO lines, or if you simply want to add BEGIN/EXCEPTION lines around each INSERT INTO line. If the latter:
sed '/^ *INSERT INTO/{ i\
BEGIN
a\
EXCEPTION
}' input-file
You can fix this problem in more than one way. One sloution is to detect that record is not duplicate before inserting. This can be done like this
select cout(*) INTO v_count
Fron table1 tb
where tb.column_name1 = val1;
IF v_count == 0 THEN
insert into table1 values (val1, vale2....);
END IF;
Here you are inserting only when there is no duplication.
Another way can be create a procedure and call the procedure instead of insert.
Here is psedo code
create procedure insert_row (val1 varchar2, val2 varchar2)
IS
BEGIN
INSERT INTO ....;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
null;
END;
This procedure will handle exception due to duplicate record in calling procedure.
You can also write a loop. That depend on your data and logic of the program. Pdedo code is
Loop
/* get the date from the cursor*/
fetch cursor_c into record_r
EXIT WHEN cursor%NOTFOUND;
BEGIN
INSERT INTO .....;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
null;
END;
END LOOP;
For getting all inserts use simple grep -i statement. This will collect all inserts in file. You can write a PL/SQL or C/C++/Java language program to generate new SQL script.