TSQL中while循环中的Continue语句?

TSQL中while循环中的Continue语句?,sql,sql-server,sql-server-2005,tsql,oracle-sqldeveloper,Sql,Sql Server,Sql Server 2005,Tsql,Oracle Sqldeveloper,在SQLServer2000和2005中,我在while循环中运行select语句。JFYI,此select语句正在连接到许多链接的服务器并获取一些值 若有任何错误,我希望它仍然执行循环中的下一个语句,类似于c中的continue语句 例如:- while @rowcount < 10 begin set @sql = 'select * from <Remotemachine>.db1.dbo.table1' exec sp_executesql @sql set @r

在SQLServer2000和2005中,我在while循环中运行select语句。JFYI,此select语句正在连接到许多链接的服务器并获取一些值

若有任何错误,我希望它仍然执行循环中的下一个语句,类似于c中的continue语句

例如:-

while @rowcount < 10
begin
 set @sql = 'select * from <Remotemachine>.db1.dbo.table1'
 exec sp_executesql @sql
 set @rowcount = @rowcount +1
End
从这里开始:

请记住,有些错误是会话甚至批处理终止符,您无法捕获这些错误

我给你的链接和那页上的两个链接应该能给你足够的信息来处理这个问题

顺便说一句,除非是不可跟踪的错误,否则它将继续执行

运行这个

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

在2005年,您可以输入try/catch。

但是有任何continue语句吗?1行受影响1行受影响1行受影响OLE DB提供程序SQLNCLI for linked server测试返回消息登录超时已过期。链接服务器测试的OLE DB提供程序SQLNCLI返回消息:建立与服务器的连接时出错。连接到SQL Server 2005时,此故障可能是因为在默认设置下,SQL Server不允许远程连接。。根据我的时间,它应该至少运行10次loop@Crawling,这个答案说明了不需要continue语句,至少在你的OP中是这样。我不在乎错误是什么。我所需要的只是我不想打破while循环。怎样
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
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