Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dynamic SQL while loop append variable

Posted on 2013-01-10
3
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 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 23

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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