| 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 exitsWhen ever this SP fails I want this statement to be executed select * into Clean_hosts from hostsIF OBJECT_ID('hosts') IS NULLCreate table hosts (raw Nvarchar(10))ELSEexec Delimited --Stored procedure if @@ERROR <> 0select * 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-29 : 14:40:15
|
| [code]try this please:IF OBJECT_ID('hosts') IS NULLBEGINCreate table hosts (raw Nvarchar(10))ENDELSEBEGINexec Delimited --Stored procedure if @@ERROR <> 0beginselect * into Clean_hosts from hostsendEND[/code]--------------------------Joins are what RDBMS's do for a living |
 |
|
|
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 |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 valuesThis 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 |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-08-29 : 15:29:46
|
| I also tried xhostx's solution still not working-Neil |
 |
|
|
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 DelimitedASBEGIN SELECT 1/0ENDGOCREATE TABLE #Hosts (ID INT)GOINSERT #Hosts VALUES (1), (2), (3)GO-- Test LogicIF OBJECT_ID('tempdb..#hosts') IS NULLBEGIN Create table #hosts (ID INT)ENDELSEBEGIN exec Delimited --Stored procedure if @@ERROR <> 0 select * into #Clean_hosts from #hostsENDSELECT *FROM #Clean_hostsGO-- CleanupIF OBJECT_ID('tempdb..#hosts') IS NOT NULL DROP TABLE #HostsIF OBJECT_ID('tempdb..#Clean_hosts') IS NOT NULL DROP TABLE #Clean_hostsDROP PROCEDURE Delimited |
 |
|
|
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 valuesThis 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 |
 |
|
|
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 @@ERRORthe you should be using ERROR_NUMBER(),ERROR_MESSAGE() etchttp://msdn.microsoft.com/en-us/library/ms175976.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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'sIf 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 |
 |
|
|
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'sIf 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|