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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:24:32+00:00 2026-05-24T08:24:32+00:00

I have a table like c1 c2 c3 c4 —————– 2 1 7 13

  • 0

I have a table like

c1  c2    c3  c4
-----------------
2    1    7    13 
9    2    8    14
1    3    9    15
5    4   10    16
2    5   11    17
11   6   12    18

As in general I would not know the number of columns (in the code @d here 4) to get a string in the form:

2,9,1,5,2,11,  1,2,3,4,5,6,  7,8,9,10,11,12,  13,14,15,16,17,18  

To do so I am doing:

 DECLARE  @d INT
         ,@counterI INT
         ,@template AS nvarchar(max) 

   SET  @d  = 4;       
   SET  @counterI   = 1;
   Set  @template = 'SELECT STUFF( 
                         (  SELECT  '','' + CAST([col] AS VARCHAR) FROM (';                        
   WHILE (@counterI < @d) BEGIN
        SET @template += ' SELECT [c'+CAST(@counterI-1 AS VARCHAR)+'] AS col FROM [MyTable] UNION ALL ';
        SET @counterI   = @counterI + 1; 
   END
  Set  @template += ' SELECT [c'+CAST(@counterI-1 AS VARCHAR)+'] AS col FROM [MyTable] ' 
  Set  @template += ') alldata FOR XML PATH('''')  )  ,   1  ,   1  ,  '''' )';    

declare @CommaString varchar(max)
set @CommaString = ''
exec sp_executesql @template, N'@CommaString varchar(max) out', @CommaString out

So if I do

select @CommaString;

enter image description here

Why is not @CommaString at the moment of selecting it returning the string if when doing the sp_executesql it is printing it right?

  • 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-24T08:24:34+00:00Added an answer on May 24, 2026 at 8:24 am

    I may be missing something about how sp_executesql works, but don’t you need something like ‘SELECT @CommaString = …’ in @template, so that it assigns the comma string to the out parameter?

    Just to clarify, I think you need something like:

    DECLARE  @d INT
            ,@counterI INT
            ,@template AS nvarchar(max) 
    
    SET  @d  = 4;       
    SET  @counterI   = 1;
    Set  @template = 'SELECT @CommaString = STUFF( 
                         (  SELECT  '','' + CAST([col] AS VARCHAR) FROM (';                        
    WHILE (@counterI < @d) BEGIN
        SET @template += ' SELECT [c'+CAST(@counterI-1 AS VARCHAR)+'] AS col FROM [MyTable] UNION ALL ';
        SET @counterI   = @counterI + 1; 
    END
    Set  @template += ' SELECT [c'+CAST(@counterI-1 AS VARCHAR)+'] AS col FROM [MyTable] ' 
    Set  @template += ') alldata FOR XML PATH('''')  )  ,   1  ,   1  ,  '''' )';    
    
    declare @CommaString varchar(max)
    set @CommaString = ''
    exec sp_executesql @template, N'@CommaString varchar(max) out', @CommaString = @CommaString out
    

    As a simpler example, something like this is perhaps easier to read/see what I mean:

    declare @CommaString varchar(max)
    set @CommaString = ''
    exec sp_executesql 'SELECT @CommaString = ''1,2,3''', '@CommaString varchar(max) out', @CommaString = @CommaString out
    

    Incidentally, I’ve usually seen this kind of thing for string concatenation:

    DECLARE @MyString varchar(max)
    
    SET @MyString = ''
    
    SELECT @MyString = @MyString + ',' + MyColumn
      FROM MyTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: name code group john 12 smith 15 how
I have table like this: id number value 1 300 233 2 343 434
I have a table like this: Item { int ItemID int ParentID string Name
I have a table like the following table: UserID Num1 Num2 Code Name Cat
I have table like this -> http://jsfiddle.net/jurisKaste/FvgeQ/ And I need to get array of
Lets say I have table like this: String | Int1 | Int2 "foo" 5
I have a table like this: create table `test` ( `id` bigint(20) NOT NULL
I have a table like this: create table SomeTable ( tableKey int identity(1,1) not
I have a table like the following : code ---> INTEGER name ---> CHAR
I have table that has a column of type DateTime, I would like to

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.