Solved

dynamic SQL while loop append variable

Posted on 2013-01-10
3
219 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
ID: 38764040
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
ID: 40107968
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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