Unable to Start Simple Transaction in SQlite - sqlite

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;

Related

SQLite error: cannot start a transaction within a transaction with very basic tables

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.

Query outside of the transaction scope - Oracle

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.

Does TCL statements come under implicit cursors?

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

In a plsql block all the statements will get rollbacked if any of the statement through error in DML?

In a plsql block all the statements will get roll backed if any of the statement through error in DML?
No. You have the power to rollback any specific set of statements when an exception or error comes. You can use
SAVEPOINT savepoint_name;
ROLLBACK TO savepoint_name;
Create a SAVEPOINT before the block till which you want to rollback. SAVEPOINT names and marks the current point in the processing of a transaction. Savepoints let you roll back part of a transaction instead of the whole transaction. The number of active savepoints for each session is unlimited.
Example :
BEGIN
-- other DML statements
SAVEPOINT do_insert;
INSERT INTO emp_name VALUES (emp_id, emp_lastname, emp_salary);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK TO do_insert;
DBMS_OUTPUT.PUT_LINE('Insert has been rolled back');
END;
SAVEPOINT
Not by itself.
With autocommit on the individual sql statement forms a transaction and is committed/rolled back on completion. A block forms an aggregation of statements. If you have one of the statements of a block failing any stateents before (within the block) still have been executed (and committed).
If you want a group of statements to behave transactional you need to use "BEGIN [TRANSACTION]...COMMIT/ROLLBACK" and do proper error handling to your needs.

Rollback SQLite transaction operations in case of failure

How can I rewrite my transaction to undo changes since the beginning whenever an error condition occurs before the end?
I see this question has already been asked and I followed the link provided in that answer. It's not clear to me what the next step is. I am hoping someone can illustrate the solution with an example applied to this transaction:
BEGIN TRANSACTION;
INSERT INTO Employee(ID, Name) VALUES(1, 'John');
...
<some failure happens here>
...
END TRANSACTION;
In this case, I would like the insert into the Employee table to be undone because a failure occurs before END TRANSACTION. How do I write rules that enforce this? This is for sqlite version 3.9.2.
Execute a ROLLBACK TRANSACTION query, or use the ON CONFLICT ROLLBACK conflict resolution on the failable queries to let SQLite roll back the transaction for you.

Resources