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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:55:33+00:00 2026-06-17T08:55:33+00:00

I have local table and string that must be populated from its values: DECLARE

  • 0

I have local table and string that must be populated from its values:

    DECLARE @#SomeTable  TABLE ( some columns ..)
    DECLARE @SomeString  varchar(8000) = 'init string'

While iterating it

    WHILE EXISTS(SELECT * FROM  @#SomeTable)
     BEGIN
        // [somecolumn] is declared temp variable
        SELECT TOP 1 @somecolumn = somecolumn FROM  @#SomeTable

        PRINT 'before ' + @SomeString // 'init string'
        PRINT [some values from SomeTable]  // this OK
        SET @SomeString += [some values from SomeTable] 
        PRINT 'after ' +  @SomeString //'init string' UPDATE NOT TAKE PLACE!!!!

        DELETE  @#SomeTable Where somecolumn = @somecolumn
     END

I find that concatenation fails. Why?

EDIT:

Here is piece of original code:

    /*Represents [WHERE] clause for retrieving values from specifyed range*/
DECLARE @WHEREclause nchar(1000) = 'WHERE '
/*Represents [ORDER BY] clause for sorting in right order and direction {ASC|DESC}*/
DECLARE @ORDERBYclause nchar(1000) = 'ORDER BY '
/*Dynamic query that returns end result*/
DECLARE @sqlCmd varchar(8000) = 
'SELECT 
        img,
        capacity,
        price,
        Id
 FROM HDD  '
/* -a- filling table for input values*/
INSERT INTO @#SequenceTable(columnName,columnValue,comparator,isASC,columnOrder)
SELECT 
    columnName,
    columnValue,
    comparator,
    isASC,
    ROW_NUMBER() OVER (ORDER BY  outOrder) AS columnOrder
FROM
(
        SELECT 'buffer' as columnName, CAST(@buffer AS nchar(20)) as columnValue, @bufferCmp as comparator, @bufferASC as isASC, @bufferOrder as outOrder
    UNION  
        SELECT 'capacity', CAST(@capacity AS nchar(20)), @capacityCmp, @capacityASC, @capacityOrder  
    UNION  
        SELECT 'price', STR(@price,20,2),  @priceCmp, @priceASC, @priceOrder
    UNION  
        SELECT 'angle_speed', CAST(@angleSpeed AS nchar(20)), @angleSpeedCmp ,@angleSpeedASC,@angleSpeedOrder
) AS AnyName
ORDER BY columnOrder 

/*---/a-----------------------------------------------------------------------------------*/

/*variables for above fields*/
DECLARE @columnName nchar(20)
DECLARE @comparator char
DECLARE @columnValue nchar(20)
DECLARE @isASC char

WHILE EXISTS(SELECT * FROM  @#SequenceTable)
BEGIN

    SELECT TOP 1 @columnName = columnName FROM  @#SequenceTable
    SELECT TOP 1 @comparator = comparator FROM  @#SequenceTable 
    SELECT TOP 1 @columnValue = columnValue FROM  @#SequenceTable 
    SELECT TOP 1 @isASC = isASC FROM  @#SequenceTable 

    IF @WHEREclause != 'WHERE '
        BEGIN
            SET @WHEREclause += ' AND '
        END
    PRINT 'before ' + @WHEREclause
    PRINT CONCAT(RTRIM(@columnName), @comparator, @columnValue)
    SET @WHEREclause += CONCAT(RTRIM(@columnName), @comparator, @columnValue)
    PRINT 'after ' +  @WHEREclause

    IF @ORDERBYclause != 'ORDER BY '
        BEGIN
            SET @ORDERBYclause += ','
        END
    IF @isASC = '1'
       SET @ORDERBYclause += CONCAT(RTRIM(@columnName),' ASC ')
    ELSE
       SET @ORDERBYclause += CONCAT(RTRIM(@columnName),' DESC ')

    Delete  @#SequenceTable Where columnName = @columnName

END

And here is piece of print result:

        before WHERE 
        angle_speed=7400 
        after WHERE 

+= does not work as expected. If I use

@SomeString = 'some value' 

then the update is visible, but if I instead use

@SomeString += 'some value' 

update is not visible

  • 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-06-17T08:55:33+00:00Added an answer on June 17, 2026 at 8:55 am

    I can assure you that there is no bug in SQL Server and that += works exactly as expected. I tried the following code:

    DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
    
    INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
    
    DECLARE @SomeString  varchar(8000) = 'init string',
            @somecolumn  varchar(8000);
    
    WHILE EXISTS (SELECT * FROM  @#SomeTable)
    BEGIN
        SELECT TOP 1 @somecolumn = somecolumn FROM @#SomeTable;
    
        SET @SomeString += @somecolumn;
    
        PRINT @SomeString; -- Works fine!!!
    
        DELETE  @#SomeTable Where somecolumn = @somecolumn;
    END
    

    And here are my results:

    init stringa
    init stringabbb
    init stringabbbccccc
    

    Since it’s impossible to tell exactly what you’re doing in your code (you’ve obfuscated the most important parts), maybe you could start from there? Surely either you have a NULL value in the table, or you’re assigning incorrectly, or you’re assigning to the wrong variable. Again, impossible to tell, because you’ve hidden the key parts of your code!

    Also, since you don’t seem to care about order, you can also do this without looping:

    DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
    
    INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
    
    DECLARE @SomeString  varchar(8000) = 'init string',
            @somecolumn  varchar(8000);
    
    SELECT @SomeString += somecolumn FROM @#SomeTable;
    
    PRINT @SomeString;
    

    Result:

    init stringabbbccccc
    

    If you care about order, you can still do this without looping – use an XML trick to concatenate in that order, and then append it to the init string afterward:

    DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
    
    INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
    
    DECLARE @SomeString  varchar(8000) = 'init string',
            @somecolumn  varchar(8000) = '';
    
    SELECT @somecolumn = (SELECT '' + somecolumn FROM @#SomeTable
    ORDER BY somecolumn DESC
    FOR XML PATH(''), TYPE).value(N'./text()[1]', N'varchar(max)');
    
    PRINT @SomeString + @somecolumn;
    

    Result:

    init stringcccccbbba
    

    On more modern versions (SQL Server 2017+), you can do this:

    DECLARE @#SomeTable TABLE (somecolumn varchar(8000));
    
    INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');
    
    DECLARE @SomeString  varchar(8000) = 'init string',
            @somecolumn  varchar(8000);
    
    SELECT @somecolumn = STRING_AGG(somecolumn, '')
      WITHIN GROUP (ORDER BY somecolumn DESC)
      FROM @#SomeTable;
    
    PRINT @SomeString + @somecolumn;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a local database table that contains 50+ external data sources from which
My issue is that I have created a table for a local db in
I have a lua table that I use as a hashmap, ie with string
So I have a table that holds references to other tables like: local a
I am new sqlite. I have one table that having 3 columns 1. Id
So I have a local SQLite table breadcrumbs being created on the Android device.
I have a simple table on my local server. mysql> desc table ; +-------+---------+------+-----+---------+-------+
I have manage to display local health center listings from my database to a
I have a DataTable that is filled in from an SQL query to a
I have a registration form that has some required field. i want to check

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.