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

  • Home
  • SEARCH
  • 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 633495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:11:33+00:00 2026-05-13T20:11:33+00:00

I have some dynamic sql statement which bombs under certain conditions, so I am

  • 0

I have some dynamic sql statement which bombs under certain conditions, so I am trying to debug it. it gets built like so:

declare @sql varchar(4000);
...
select @sql = '<part1>';
...
select @sql = @sql + '<part2>';
...
select @sql = @sql + '<part3>';
...
begin 
execute(@sql);
select @ec__errno = @@error
    if @ec__errno != 0
    begin
    if @@trancount != 0
    begin
    rollback;
    end
return @ec__errno;
end;
... 

As I said, it bombs in a particular iteration of a loop (don’t ask me why it is implemented like this, I am just fixing a bug) and I am having a hard time displaying the contents of the string in a watch window. I think I am getting the first 255 characters only. Watching for substring(@sql, 0, 200) results in 'substring(@sql,0,200)' could not be evaluated. Please help. I was hoping to watch substrings from 0 to 199, from 200 to 399 and so on, and then piece this thing together and finally debug it.

I would appreciate pointers from you. Thanks!

  • 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-13T20:11:33+00:00Added an answer on May 13, 2026 at 8:11 pm

    When forced to use dynamic sql in a stored proc, we do the following. add an input variable of debug which is a bit field. If it is 0, the exec statment will proces if it is 1 then you will get a print statement instead. I suggest you do something simliar to debug. Instead of executing, print the results of your SQL or possibly insert the SQl to a table since it seems to be happening in a loop. Then you can look over the sql that was built and see where it went wrong.

    Declare debug bit
    set debug = 1
    
    ...
    if debug = 1 Begin     Print @SQL End
    Else 
    Begin Exec (@sql) End
    

    Alternatively

    Create a table called mydynamiccode_logging (with a sql column the same length as the max sql statment, a rundatecolumn and whatever other columns you might find necessary (I’d consider the input variables used to make up the sql statment, the user, the application if more than one uses this piece of code)

    Before you run the exec statment run something like this:

    insert mydynamiccode_logging (sql, rundate)
    values (@sql, getdate()) 
    

    Now you could also add the debug bit field and only log when you have changed it to debug mode or you could always log, depends on the system and how much extra time this takes to do and how slammed the rest of the system is. You don’t want to slow prod down significantly by logging.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 351k
  • Answers 351k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CGRect, CGPoint, etc. are just plain structs, so you can… May 14, 2026 at 7:16 am
  • Editorial Team
    Editorial Team added an answer There's celery. You could break it down into 2 different… May 14, 2026 at 7:16 am
  • Editorial Team
    Editorial Team added an answer Are you already using Firebug to monitor the AJAX calls… May 14, 2026 at 7:16 am

Related Questions

I have the following scenario: I have a database with a particular MyISAM table
Is there a way to write SQL Server Stored Procedure which to be strongly
.NET 3.5, C# I have a web app with a search feature. Some of
I'm trying to run some dynamic SQL in an installscript project. In the UI
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.