Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1113839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:54:58+00:00 2026-05-17T02:54:58+00:00

In Sql Server 2000 and 2005 I am running a select statement in a

  • 0

In Sql Server 2000 and 2005 I am running a select statement in a while loop. JFYI, this select statement is connecting to many linked servers and getting some values.

IF there is any error, i want it should still execute next statement in the loop (similar to continue statement in c#)

Example:-

while @rowcount < 10
begin
 set @sql = 'select * from <Remotemachine>.db1.dbo.table1'
 exec sp_executesql @sql
 set @rowcount = @rowcount +1
End
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:54:59+00:00Added an answer on May 17, 2026 at 2:54 am

    Start here: http://www.sommarskog.se/error_handling_2005.html

    Keep in mind that some errors are session and even batch terminators and you can’t trap those

    The link I gave you(and the 2 links on that page) should give you enough information on how to handle this

    BTW, unless it is a non trapable error it will continue executing

    run this

    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
    

    Here is the output

    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
    

    Here is how you can use TRY CATCH to trap this

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sql 2000 server and sql 2005 server running on the same box
I maintain a couple of older sites running SQL Server 2000 and 2005. On
Background: I'm working on migrating from SQL Server 2000 to SQL Server 2005. This
SQL Server 2005 BOL says about BREAK: Exits the innermost loop in a WHILE
In SQL Server 2000 and 2005: what is the difference between these two WHERE
Using SQL Server 2000 My Query. SELECT (Format(IIf(CLng(OutTime) > 180000, CDate('18:00:00'), CDate(Format(OutTime, '00:00:00'))) -
TSQL (as used in MS SQL Server 2000 and 2005) allows multiple JOIN clauses,
I'm working with SQL Server 2005 and Windows Server 2000 and wonder if there
We use SQL Server 2000/2005 and Vault or SVN on most of our projects.
How to create a stored procedure (for SQL Server 2000 / 2005) to swap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.