3

Is it possible/make sense to have COMMIT statement in SQL functions?

4
  • Yes.Sometime. docs.oracle.com/cd/B19306_01/appdev.102/b14261/… Commented May 25, 2016 at 12:03
  • 1
    What do you mean by "SQL functions"? If you mean calling a function with a commit/rollback in it from a select statement, then no, it's not possible, as per the documentation Commented May 25, 2016 at 12:05
  • In my own user defined function, i am doing some operation. then I want to commit the changes. Commented May 25, 2016 at 12:07
  • 1
    If the function is going to be used only in PL/SQL, then yes, you can. I generally wouldn't recommend commits anywhere inside PL/SQL apart from at the end of the logical transaction - usually this would be the calling code (eg. a scheduled database job calling a PL/SQL procedure that does stuff) or logging errors in a separate transaction (using pragma autonomous_transaction). If you do a commit, remember that it will affect all DML done since the transaction started, hence why it's generally not a good thing to do mid-transaction. Commented May 25, 2016 at 13:45

3 Answers 3

8

Technically, the answer ist yes. You can do the following:

create or replace function committest return number as 
begin 
  update my_table set col = 'x';
  commit;
  return 1;
end;
/

declare
  number n;
begin
  n := committest();
end;
/

However, you can't do the following:

select committest() from dual;

this would be a commit during a query and thus result in a

ORA-14552: Cannot Perform a DDL Commit or Rollback Inside a Query or DML

Sign up to request clarification or add additional context in comments.

Comments

4

Yes, you can do that if you make the function an autonomous transaction. That way it will not be part of the current transaction anymore.

create or replace function doIt
as
  pragma autonomous_transaction;
begin
  ... code ...
  commit;
end;
/

More documentation

3 Comments

You can. But I'd suggest a whole heap of "but you really shouldn't unless you understand deeply the transactional implications of what you are asking for" caveats.
Agree, but we don't know what OP wants to accomplish. The simplest legitimate use I can think of is logging; it is very inconvenient (to say the least) if a transaction fails and the associated logging is rolled back as well.
True. If you have a procedure whose only function is to log and you want that logging to persist even if the transaction fails, autonomous transactions are wonderful and appropriate. Basically any other use of autonomous transactions, though, is at best a hack and at worst a bug. The vast majority of people using autonomous transactions are using them incorrectly (see any mutating table thread) so a suggestion to use them without some evidence that this would be a valid use case strikes me as dangerous.
-3

No, it's not possible, see the documentation:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5009.htm

Restrictions on User-Defined Functions

.... In addition, when a function is called from within a query or DML statement, the function cannot: ....

  • Commit or roll back the current transaction, create a savepoint or roll back to a savepoint, or alter the session or the system. DDL statements implicitly commit the current transaction, so a user-defined function cannot execute any DDL statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.