Solved

dynamic SQL while loop append variable

Posted on 2013-01-10
3
213 Views
Last Modified: 2014-06-03
in my dynamic sql, i have a while loop, after i loop through a table, and then append the variable as result, here is my code

DECLARE @sqlmiddle NVARCHAR(MAX)
DECLARE @loop2 INT = 1

WHILE (@loop2 < = (Select count(*) from #SomeTable))
      BEGIN
            set @sqlmiddle = @sqlmiddle + somethingINTable
            SELECT @loop2 = @loop2 + 1
      END

how come it seems the @sqlmiddle not adding up, is my sytnax wrong?
0
Comment
Question by:hongclub
3 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 500 total points
Comment Utility
It seems right, just make sure you're not adding any nulls there because that will yield unexpected results.

Just to play it safe you can use:

DECLARE @sqlmiddle NVARCHAR(MAX)
DECLARE @loop2 INT = 1

WHILE (@loop2 < = (Select count(*) from #SomeTable))
      BEGIN
            set @sqlmiddle = @sqlmiddle + ISNULL(somethingINTable,'')
            SELECT @loop2 = @loop2 + 1
      END
0
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now