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 6803255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:19:19+00:00 2026-05-26T19:19:19+00:00

I try to update a column in a temporary table by concatenating rows from

  • 0

I try to update a column in a temporary table by concatenating rows from another table.

Look at this:

DECLARE @Test VARCHAR(MAX);

CREATE TABLE #Test
(
     EmployeeId INT
     ,Html VARCHAR(MAX)
);

CREATE TABLE #EmployeeItems
(
    EmployeeId INT
    ,ItemNo INT
);

INSERT INTO #EmployeeItems (EmployeeId, ItemNo)
VALUES
    (1, 1)
    ,(1, 2);

INSERT INTO #Test (EmployeeId, Html) VALUES (1, '<div class="first">');
SET @Test = '<div class="first">';

UPDATE T SET Html += '<div class="second">' + CAST(E.ItemNo AS VARCHAR) + '</div>'
FROM #Test AS T
    JOIN #EmployeeItems AS E ON T.EmployeeId = E.EmployeeId;

SELECT @Test += '<div class="second">' + CAST(E.ItemNo AS VARCHAR) + '</div>'
FROM #Test AS T
JOIN #EmployeeItems AS E ON T.EmployeeId = E.EmployeeId;

UPDATE #Test SET Html += '</div>';
SET @Test += '</div>';

SELECT Html FROM #Test;
SELECT @Test;

DROP TABLE #Test;
DROP TABLE #EmployeeItems;

The column in the #Test table contains: <div class="first"><div class="second">1</div></div>

And the @Test variable contains: <div class="first"><div class="second">1</div><div class="second">2</div></div>

Why is this? What difference is it? I thought I would get the same results but in the table case it only concatenates one row, the first.

What should I do instead to update my table without running a cursor?

EDIT

My original problem comes from something like this:

My input data is

CREATE TABLE #Actions(EmployeeId INT,EmployeeName VARCHAR(100),ActionStart TIME,ActionEnd TIME,Type VARCHAR(10));
INSERT INTO #Actions(EmployeeId,EmployeeName,ActionStart,ActionEnd, Type)
VALUES (1,'Bob','09:00','12:00', 'action'),(1,'Bob','14:30','16:00', 'action'),(1,'Bob','18:00','20:00', 'event'),(2,'Susan','10:00','12:00', 'action');

I want output like this

<div class="employee" employeeid="1" employeename="Bob">
    <div class="action" start="09:00" end="12:00" type="action"></div>
    <div class="action" start="14:30" end="16:00" type="action"></div>
    <div class="action" start="18:00" end="20:00" type="event"></div>
</div>
<div class="employee" employeeid="2" employeename="Susan">
    <div class="action" start="10:00" end="12:00" type="action"></div>
</div>

Like the first example this is a simplified example. But if I solve a case I can solve my problem. How would you do this with the FOR XML clause?

  • 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-05-26T19:19:20+00:00Added an answer on May 26, 2026 at 7:19 pm

    Think of the variable concatenation as a quirk; it’s unsupported behavior that gets used a lot in code 🙂 If you want to keep using it, you could modify your UPDATE statement like so:

    UPDATE t 
    SET HTML = @Test
    FROM #test t
    

    An alternate method would be to use the FOR XML syntax to concatenate

    CREATE TABLE #Test
        (
          EmployeeId INT
        , Html VARCHAR(MAX)
        ) ;
    CREATE TABLE #EmployeeItems
        (
          EmployeeId INT
        , ItemNo INT
        ) ;
    INSERT  INTO #EmployeeItems
            ( EmployeeId, ItemNo )
    VALUES  ( 1, 1 )     ,
            ( 1, 2 ) ;
    INSERT  INTO #Test
            ( EmployeeId, Html )
    VALUES  ( 1, '<div class="first">' ) ;
    
    UPDATE  #Test
    SET     HTML+= REPLACE(REPLACE(( SELECT '|div class="second"|'
                                            + CAST(E.ItemNo AS VARCHAR) + '|/div|'
                                     FROM   #Test AS T
                                            JOIN #EmployeeItems AS E ON T.EmployeeId = E.EmployeeId
                                   FOR
                                     XML PATH('')
                                   ), '|div class="second"|',
                                   '<div class="second">'), '|/div|', '</div>')    
    UPDATE  #Test
    SET     Html += '</div>' ;
    
    SELECT  Html
    FROM    #Test ;
    
    
    DROP TABLE #Test ;
    DROP TABLE #EmployeeItems ;  
    

    Or (and perhaps best), just build your XML document directly.

    SELECT  'first' AS "div/@class"
          , 'second' AS "div/div/@class"
          , e.ItemNo AS "div/div"
    FROM    #EmployeeItems e
    FOR     XML PATH('') 
    

    Additional Answer for edited question:

    BEGIN TRAN
    
    
    CREATE TABLE #Actions
        (
          EmployeeId INT
        , EmployeeName VARCHAR(100)
        , ActionStart TIME
        , ActionEnd TIME
        , Type VARCHAR(10)
        ) ;
    INSERT  INTO #Actions
            ( EmployeeId, EmployeeName, ActionStart, ActionEnd, Type )
    VALUES  ( 1, 'Bob', '09:00', '12:00', 'action' ),
            ( 1, 'Bob', '14:30', '16:00', 'action' ),
            ( 1, 'Bob', '18:00', '20:00', 'event' ),
            ( 2, 'Susan', '10:00', '12:00', 'action' ) ; 
    
    ;
    WITH    CTE
              AS ( SELECT DISTINCT
                            EmployeeID
                          , EmployeeName
                   FROM     #actions
                 )
        SELECT  'employee' AS "@class"
              , employeeid AS "@employeeid"
              , employeename AS "@employeename"
              , ( SELECT    'action' AS "@class"
                          , ActionStart AS "@start"
                          , ActionEnd AS "@end"
                          , Type AS "@type"
                  FROM      #Actions a2
                  WHERE     a2.EmployeeId = a.EmployeeID
                FOR
                  XML PATH('div')
                    , TYPE
                )
        FROM    cte a
    FOR     XML PATH('div') 
    
    
    ROLLBACK TRAN       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
I continually get these errors when I try to update tables based on another
When I try to install/update any plugin from Help -> Install New Software I
I try to realize a link (in fact many links) that update a table
If I try to implement this simple-as-possible example: Update Content with AJAXRefreshRequest it does
I have some code to update a database table that looks like try {
I am getting this below error when I try to update a row in
This is currently my MySQL UPDATE query, which is called from program written in
I am trying to update one column for any number of rows. Here is
I'm using insert-/update triggers to update a second table's column Price . The insert

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.