I am generating a query string dynamically which i want to execute only if it is avalid query (valid syntax). Otherwise i want to skip that query and continue pl sql execution. How can it be done?
Nest that query execution with another BEGIN..EXCEPTION..END; .In exception, handle "WHEN OTHERS". In case there is any syntax error, it will be caught in exception block and next line of your logic in pl/sql execution will be moved on to.
I am brand new to Postgresql and am trying to write a function that calls another function, and then returns -1 if that function had any errors. I am coming from a T-SQL background and essentially want the equivalent of
CREATE PROCEDURE [dbo].[p_newproc]
AS
BEGIN
EXEC p_seed_table
IF ##ERROR <> 0
RETURN -1
...
END
So far I have found that I am likely going to use something like the following:
CREATE OR REPLACE FUNCTION public.fn_newfunc()
RETURNS void
AS
$$
BEGIN
PERFORM fn_seed_table();
EXCEPTION
WHEN
SQLSTATE <> '00000'
RAISE EXCEPTION '-1';
End;
$$
LANGUAGE 'plpgsql';
Any advice would be appreciated. Thanks.
This is too long for a comment.
Both SQL Server and Postgres support exception handling. You chose not to use exception handling in the SQL Server code. You should make a conscious and informed decision about using exceptions (in either database).
SQL Server stored procedures return integers by default, and these can be used for status. Postgres doesn't have stored procedures, "only" stored functions, so you need to be a bit more explicit about the return values. The closest equivalent in Postgres to a stored procedure is a function that returns an integer.
There are pros and cons to switching over to exceptions rather than handling exceptions locally using return values (and be extra careful if you have transactions that span code blocks). I think exception handling is more robust in Postgres: SQL Server misses some types of exceptions.
However, you can implement either method in either scripting language. If you switch to using exceptions, think about the issues before doing so. That is, don't just reflexively make the switch. If you do switch to exception handling, also change the SQL Server code -- assuming you are maintaining both code bases.
The problem is on SQL Server 2008
I have run into this unusual situation. The situation is like this. I have two separate SQL Server installations. Lets name them as
Installation-1
Installation-2
On Installation-1 lets say I have database called Database-1. And on Installation-2 I have other database called Database-2.
Database-1 has a SP called Parent_SP. Database-2 has two SPs called Child_SP_1 and Child_SP_2.
Installation-2 has been added as a linked server on Installation-1.
The Parent_SP on Database-1 calls Child_SP_1 which in turn calls Child_SP_2.
Now what happens is if there is an exception/error in Child_SP_1. I am able to catch it. However if the exception is raised at Child_SP_2. The exception never gets caught in Parent_SP.
I have no idea why such a behavior. Logically any exception should be raised eventually to parent SP. Which is not happening.
I've been trying to debug and create different scenarios but so far no luck.
Is there any reason why it should not work ?
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.
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.