從這裏開始:http://www.sommarskog.se/error_handling_2005.html
請記住,有些錯誤是會話甚至批終止符,你不能捕獲這些錯誤
我給你的鏈接(和2個鏈接該網頁上)應該給你如何BTW處理這個
足夠的信息,除非它是一個非trapable錯誤,否則它將繼續執行
來看,這種
declare @rowcount int, @sql nvarchar(100)
set @rowcount = 1
while @rowcount < 10
begin
set @sql = 'select * from <Remotemachine>.db1.dbo.table1'
exec sp_executesql @sql
print @rowcount
set @rowcount = @rowcount +1
End
這裏是輸出
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
1
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
2
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
3
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
4
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
5
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
6
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
7
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
8
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
9
這裏是你如何使用TRY CATCH
捕獲這個
declare @rowcount int, @sql nvarchar(100)
set @rowcount = 1
while @rowcount < 10
begin
set @sql = 'select * from <Remotemachine>.db1.dbo.table1'
begin try
exec sp_executesql @sql
end try
begin catch
select ERROR_MESSAGE() -- or do something
end catch
print @rowcount
set @rowcount = @rowcount +1
End
我不在乎錯誤是什麼。我需要的是我不想打破while循環。怎麼樣?? – Jango 2010-09-27 16:09:44