The syntax for this is probably database dependent. But in Transact-SQL, what you are doing is checking @@ TRANCOUNT to see if you have a transaction. If you want to create a savepoint, and in the end you can simply go through the end of the function (assuming that the commit or rollback occurs later) or rollback to your savepoint.
For more information, see the Microsoft documentation for savepoints .
Support for savepoints is fairly common, but I think that the mechanism (provided that it exists) to find out what you are currently in a transaction will be slightly different.
source share