Solved

dynamic SQL while loop append variable

Posted on 2013-01-10
3
228 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 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

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

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

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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