Ignoring SQL Errors

More
2 years 11 months ago #22793 by hanisultan
Hello,  I'm trying to run a SQL script that calls a SP/proc and I want to ignore SQL error and continue.  When I run the same script on SSMS , it shows the error and continue and I want the same thing here.  Some example showing below - is this possible or any SQL statements errors will cause a failure and can't be skipped like what happens in SSMS?

Please Log in or Create an account to join the conversation.

More
2 years 10 months ago #22795 by Peter.Jonson
Replied by Peter.Jonson on topic Ignoring SQL Errors
Not sure I understand the question

I have run some tests and SQL Script package object is working as expected

Here is an example

update [DEMO].[dbo].[afleveradres] set id=id 
GO
update [DEMO].[dbo].[afleveradres] set id='ABCCC' 
GO
update [DEMO].[dbo].[afleveradres] set id=id 
GO

Line 2 fails

But line 3 still executed

Information    13/08/2023 09:52:04    Executing Sql Script...
Information    13/08/2023 09:52:04    Executing Sql...
Information    13/08/2023 09:52:04    update [DEMO].[dbo].[afleveradres] set id=id
Information    13/08/2023 09:52:04    Done!
Information    13/08/2023 09:52:04    Executing Sql...
Information    13/08/2023 09:52:04    update [DEMO].[dbo].[afleveradres] set id='ABCCC'
Error    13/08/2023 09:52:04    22018 [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'ABCCC' to data type int.
Information    13/08/2023 09:52:04    Executing Sql...
Information    13/08/2023 09:52:04    update [DEMO].[dbo].[afleveradres] set id=id
Information    13/08/2023 09:52:04    Done!

If you are using dynamic SQL inside a stored procedure you might find this link very useful.

www.sqlservercentral.com/forums/topic/ig...g-error-and-continue
 

Peter Jonson
ETL Developer

Please Log in or Create an account to join the conversation.

More
2 years 10 months ago #22798 by hanisultan
Replied by hanisultan on topic Ignoring SQL Errors
From your emample, I think you understand what I'm trying to say.   But the script is actually calling a procedure which contains dynamic sql statements and calls to other store procedures and functions - maybe that is why? all the erros I get are the same, the insert statment ... and I know why and we have fix for that but after the completion of the store procedure.  Maybe I should execute the store procedure as sql statements inside a script and see what happens.  In SSMS it continues to the next statement after it fails executing the current statement as you can see from my example.  Thank you 

Please Log in or Create an account to join the conversation.

More
2 years 10 months ago #22799 by Peter.Jonson
Replied by Peter.Jonson on topic Ignoring SQL Errors
I think it s a question of handling error messages inside the stored procedure.

I remember dealing with it a while ago

Can you post your stored procedure here so we can review it

Peter Jonson
ETL Developer

Please Log in or Create an account to join the conversation.

More
2 years 10 months ago #22802 by Peter.Jonson
Replied by Peter.Jonson on topic Ignoring SQL Errors
Found a very old example

www.etl-tools.com/forum/advanced-etl-pro...ed-procedure?start=6

CREATE PROCEDURE [dbo].[EXECUTE_SQL] (@SQL VARCHAR(1000))
AS
BEGIN
 SET NOCOUNT ON   
 BEGIN TRY
        EXEC(@SQL);
    END TRY
    BEGIN CATCH
     DECLARE @error_message varchar(500)
      SET @error_message=ERROR_MESSAGE()
      RAISERROR (@error_message, 1, 1);
    END CATCH
END;


create or alter procedure my_procedure
as 
begin
SET NOCOUNT ON
 exec EXECUTE_SQL 'truncate table x'
 exec EXECUTE_SQL 'truncate table orders'
end

 exec my_procedure

Peter Jonson
ETL Developer
The following user(s) said Thank You: hanisultan

Please Log in or Create an account to join the conversation.

More
2 years 10 months ago #22803 by hanisultan
Replied by hanisultan on topic Ignoring SQL Errors
Thank you for the suggestion.  I have decided to go with External Application object and it worked great with variables and executing the SQL Proc using sqlcmd utility so I will be going that route.

Please Log in or Create an account to join the conversation.

Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Read more
Marketing
Set of techniques which have for object the commercial strategy and in particular the market study.
Google
Accept
Decline
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline
Google Analytics
Accept
Decline
Functional
Tools used to give you more features when navigating on the website, this can include social sharing.
Advertisement
If you accept, the ads on the page will be adapted to your preferences.
Google Ad
Accept
Decline
Save