Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 exception handling

Author  Topic 

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 14:16:22
Hi,

I would like to dump the content to other table if the SP fails,
I mean I would like to (Create&) populate the "clean_hosts" table with the content of "hosts" table only if the SP(Delimited) fails, how can add this exception or logic could any one help, as the @@Error <>0 is not working, the SP is throwing err and exits

When ever this SP fails I want this statement to be executed select * into Clean_hosts from hosts




IF OBJECT_ID('hosts') IS NULL
Create table hosts (raw Nvarchar(10))
ELSE
exec Delimited --Stored procedure
if @@ERROR <> 0
select * into Clean_hosts from hosts




-Neil

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-29 : 14:34:14
use try catch..put execute sp inside try and write error logic inside catch



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-29 : 14:40:15
[code]try this please:

IF OBJECT_ID('hosts') IS NULL
BEGIN
Create table hosts (raw Nvarchar(10))
END
ELSE
BEGIN
exec Delimited --Stored procedure
if @@ERROR <> 0
begin
select * into Clean_hosts from hosts
end
END
[/code]

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 14:47:05
hi Visakh,

It is still not working, I have made the sp to fail intentionally after adding the try catch and control is coming out with executing the catch block

-Neil
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-29 : 15:12:42
quote:
Originally posted by aakcse

hi Visakh,

It is still not working, I have made the sp to fail intentionally after adding the try catch and control is coming out with executing the catch block

-Neil



whats the error happening in sp? if its compilation error etc it wont get caught...

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 15:20:52
SP error details:

creating dynamically clean_host table with 2 columns
insert into clean_host values ( 'oneonly')--here it errors out as it is expecting 2 column values

This is the requirement, normally this sp works fine for correct data set which is passed as a parameter to this sp. occasionally this sp fails if the data set is not correct, hence if this sp fails I want to populate other table as shown in the eg.

let me know how can I achieve this :)

-Neil
Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 15:29:46
I also tried xhostx's solution still not working

-Neil
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-08-29 : 15:37:52
Is this what you are trying to do?
CREATE PROCEDURE Delimited
AS
BEGIN
SELECT 1/0
END
GO

CREATE TABLE #Hosts (ID INT)
GO

INSERT #Hosts VALUES (1), (2), (3)
GO

-- Test Logic
IF OBJECT_ID('tempdb..#hosts') IS NULL
BEGIN
Create table #hosts (ID INT)
END
ELSE
BEGIN
exec Delimited --Stored procedure
if @@ERROR <> 0
select * into #Clean_hosts from #hosts
END

SELECT *
FROM #Clean_hosts
GO

-- Cleanup
IF OBJECT_ID('tempdb..#hosts') IS NOT NULL
DROP TABLE #Hosts

IF OBJECT_ID('tempdb..#Clean_hosts') IS NOT NULL
DROP TABLE #Clean_hosts

DROP PROCEDURE Delimited
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-29 : 15:50:26
quote:
Originally posted by aakcse

SP error details:

creating dynamically clean_host table with 2 columns
insert into clean_host values ( 'oneonly')--here it errors out as it is expecting 2 column values

This is the requirement, normally this sp works fine for correct data set which is passed as a parameter to this sp. occasionally this sp fails if the data set is not correct, hence if this sp fails I want to populate other table as shown in the eg.

let me know how can I achieve this :)

-Neil


thats again a compilation error which try...catch wont capture. we did discuss this in other thread

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 15:55:56
Thanks Visakh & Lamprey,

I have a try catch in the sp which is working fine and executing the catch block and displaying the error from SP, is it because of sp's catch block is getting executed hence this is failing @@Error <>0 ?... is there any work around ..

-Neil
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-29 : 16:10:31
quote:
Originally posted by aakcse

Thanks Visakh & Lamprey,

I have a try catch in the sp which is working fine and executing the catch block and displaying the error from SP, is it because of sp's catch block is getting executed hence this is failing @@Error <>0 ?... is there any work around ..

-Neil


if you use try...catch you dont require @@ERROR

the you should be using ERROR_NUMBER(),ERROR_MESSAGE() etc

http://msdn.microsoft.com/en-us/library/ms175976.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 16:56:54
Thanks Visakh,

After reading the below stm, I came to know that if the SP's catch blocks get executed, it will not throw the err or this error will not be catch by the calling blocks CATCH's

If the stored procedure contains a TRY…CATCH construct, the error transfers control to the CATCH block in the stored procedure. When the CATCH block code finishes, control is passed back to the statement immediately after the EXECUTE statement that called the stored procedure.


I need to look for some other work around or include the insert into other stm in the sp's catch block


-Neil
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-29 : 17:19:17
quote:
Originally posted by aakcse

Thanks Visakh,

After reading the below stm, I came to know that if the SP's catch blocks get executed, it will not throw the err or this error will not be catch by the calling blocks CATCH's

If the stored procedure contains a TRY…CATCH construct, the error transfers control to the CATCH block in the stored procedure. When the CATCH block code finishes, control is passed back to the statement immediately after the EXECUTE statement that called the stored procedure.


I need to look for some other work around or include the insert into other stm in the sp's catch block


-Neil


but why do you need try catch inside procedure?

if attempt is to catch error in procedure try...catch should be in outer query with EXEC procedure coming inside try part

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2012-08-29 : 17:54:01
this proc is standard already developed one with the try catch...hence I included the insert stm in the catch block of sp instead in the calling block :)

-Neil
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-30 : 01:24:47
quote:
Originally posted by aakcse

this proc is standard already developed one with the try catch...hence I included the insert stm in the catch block of sp instead in the calling block :)

-Neil



you need to use TRY CATCH if you've capture error in procedure. This being compilation error it cant be captured in same proc TRY...CATCH

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -