Can dbms_errlog be used for SELECT queries? - sql

Can the dbms_errlog function be used for SELECT queries?
I earlier encountered an error where Oracle is throwing an ORA-0722, i was trying to identify which column and possibly, row of a PL/SQL statement that was throwing that error. However i found out that dbms_errlog is native to only Oracle 10g and above.
In the case also, what alternatives do i have if i am using Oracle 9i?

DBMS_ERRLOG ist not a function, it is a PL/SQL package. It contains one procedure that creates an error table. To log errors to this error, you need to specify the "log errors" clause to your DML statements. From this description it should be obvious that this is tightly integrated with the transaction layer.
One way to reproduce similar behavior in earlier releases is to
Create your own error table
Create a PL/SQL procedure that inserts into that error table. To
make sure that the log is written in case of errors this procedure has to use
autonomous transactions.
The calls to log errors have to be explicitly added to the
corresponding exception handlers.

Related

What is the proper way to execute Dynamic SQL?

We have lots of SQL scripts in our codebase, which produce dynamic SQL statements, and we execute these statements against the database with
EXEC (#FINALSQL)
It is declared like this
DECLARE #FINALSQL NVARCHAR(MAX);
In one of our scripts, we now get an error when executing the dynamic SQL:
Error Number: 2812
Error Severity: 16
ErrorState: 62
ErrorMessage: Could not find stored procedure
with the SQL statement following.
The error number is related to the the error message but still couldn't find any related issue of how to resolve this problem.
I have also read this question but it didn't help because I already enclosed dynamic SQL in brackets
calling EXEC() generates error: could not find stored procedure
Any ideas what could have caused the problem?
Update: the use of #FINALSQL is to create Updates for an amount on tables based on condition in the script.
The answer to almost anything dynamic can be found in Erland Sommarskog's
tour de force.
http://www.sommarskog.se/dynamic_sql.html
"Execute" is optional for the first statement in a batch when executing a stored procedure. There is likely some formatting error in the string and the parser is trying to interpret the first word as a procedure name after failing to parse it otherwise.
If you have no option to capture the string, then use an Extended Event session or trace to capture the query for errors.
If it was a real procedure, then I 'd suspect that current database is wrong. A "USE mydatabase;" as the first thing in the string would fix that. But you state this is not the case. Also, the procedure name was not listed in the error message.

how to fix this error:ddl statement interferred with query replan?

database : vertica
when I use insert into.....select.... statement to add data, following error occurred:
notice : error encounterred in contriant validation
error : ddl statement interferred with query plan
hint : please reissue query
This error, although it is not the actual error, is pretty self explanatory. You tried to issue an INSERT statement, but there was another DDL statement that interfered (was holding a lock on the table) at the same time and so your INSERT statement was killed.
At this point you probably won't be able to see it any more, but in the future when you see this error, take note of the time that the statement was executed, then query the v_monitor.query_requests system table to see what other DDL statements were being executed at the same time.
If this was just an ad hoc INSERT statement, then the solution is to do exactly what the Vertica notice message said, re-issue the query. If this is part of a script or application logic, then you need to handle this error accordingly and add the logic to re-issue the statement again if this error is thrown.

Teradata: How can I raise an error in a stored procedures

Is there a way to raise errors inside of Teradata stored procedures?
For example, I want to check if a table is empty. If the table is empty I wish to cause the stored procedure to error out with the error message "Table Empty".
That will allow me to bubble up the error to the calling application.
TD13+ implements Standard SQL's SIGNAL/RESIGNAL.

Oracle - Obtain the SQL as a string when an exception occurs

I am trying to get the SQL query to be returned from a database call, when the query fails to execute and the return cursor has a failure state. This query string will be used for logging purposes.
One way to do this is to hold a variable that contains the SQL query as a string, and then wrap each query in the PL/SQL block with the exception handler and if an exception arises, return the query string with the failure status to the UI component.
Is there a better way to do this? Does Oracle exception object or any other package support this feature?
This question seems like a duplicate for the thread: Obtain the Query/CommandText that caused a SQLException
However, I did not find a solution to the problem in that thread, and I would like to know if there are any new packages supported by Oracle that gives us the query string that caused the exception to occur.
When you are calling the database from an external language (Java, C#) you can probably do it, by implementing your own JDBC (or whatever) driver.
You would do so by delegating the real world to a normal driver, but whenever a statement is sent to the database you remember the statement and if causes an exception you create a new exception which also includes the sql statement (and possibly bind variables).
Note that this will only give you your statement. If you execute a stored procedure or cause a trigger to fire which in turn fails you only get the original statement not the one that actually failed.

How to get inner errors from try/catch clause in SQL Server

I am running a stored procedure in SQL Server 2008 inside a try/catch. The stored procedure and the stored procs it calls raise a few errors but in the try/catch you only get the last error from the stored procedure that you are running.
Is there a way/trick to be able to somehow catch ALL the errors generated by child stored proc calls while running a particular stored procedure? (assume that you have no access to any stored procedures so you can't modify where they can write the error, i.e. you can't just change all the stored procedures to stop raising errors and instead write them to some table and in your catch read from that table)
Here is a good resource for how to deal with error handling in SQL Server.
http://www.sqlservercentral.com/articles/Development/anerrorhandlingtemplatefor2005/2295/
However, some of the methods require that you have the ability to change the code in order to capture the errors. There is really no way of getting around this. You can't just ignore the error, keep processing, and then come around later to deal with the error. In most, if not all, languages, exceptions have to be dealt with at the time the exception was raised. T-SQL is no different.
I personally use a stored procedure to log any error whenever it occurs. Here is what I use:
CREATE PROCEDURE [dbo].[Error_Handler]
#returnMessage bit = 'False'
WITH EXEC AS CALLER
AS
BEGIN
INSERT INTO Errors (Number,Severity,State,[Procedure],Line,[Message])
VALUES (
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
isnull(ERROR_PROCEDURE(),'Ad-Hoc Query'),
isnull(ERROR_LINE(),0),
ERROR_MESSAGE())
IF(#returnMessage = 'True')
BEGIN
select Number,Severity,State,[Procedure],Line,[Message]
from Errors
where ErrorID = scope_identity()
END
END
If you have stored procs that are raising more than one error, they need to be replaced no matter what. You probably have data integrity errors in your database. That is a critical, "everything needs to stop right now until this is fixed" kind of issue. If you can't replace them and they were incorrectly written to allow processing to continue when an error was reached, then I know of no way to find the errors. Errors are not recorded unless you tell them to be recorded. If the stored procs belong to a product you bought from another vendor and that's why you can't change them, your best bet is to change to a vendor that actually understands how to program database code because there is no salvaging a product written that badly.
You wouldn't have a Java or c# methods raising error after error. Why do you expect SQL to allow this? An exception is an exception
If the DB Engine is throwing errors then you have problems.
What I've done before is to separate testing and checking code: find out what is wronf first and throw one exception If no errors, do your writes.