Column data stores in variable

  • Hi everyone,
    I want to column level data store in one variable. like below example.

    Servers
    SQL Server
    Oracle
    Sybase
    DB2
    Teradata
    Hana
    Hadoop

    @a = SQL Server, Oracle, Sybase, DB2, Teradata, Hana, Hadoop

    Thanks 
    Venu

  • You mean you want to create a delimited list? What have you tried so far?This is a very common question for a lot of RDBMS, including SQL Server. Consider having a search of "SQL Server Delimited list" on your favourite Internet Search Engine. As you're using 2014, you'll want to look for a solution using FOR XML PATH; you won't be able to make use of STRING_AGG, as that is only available in SQL Server 2017+. Don't look at answers that use a WHILE loop or a self referencing variable, they perform poorly.

    If you get stuck, reply with the code you've tried and myself, or another user, will be more than happy to guide you in the right direction.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Wednesday, May 9, 2018 7:41 AM

    You mean you want to create a delimited list? What have you tried so far?This is a very common question for a lot of RDBMS, including SQL Server. Consider having a search of "SQL Server Delimited list" on your favourite Internet Search Engine. As you're using 2014, you'll want to look for a solution using FOR XML PATH; you won't be able to make use of STRING_AGG, as that is only available in SQL Server 2017+. Don't look at answers that use a WHILE loop or a self referencing variable, they perform poorly.

    If you get stuck, reply with the code you've tried and myself, or another user, will be more than happy to guide you in the right direction.

    I thought this is not a "delimited list". it is just like "Transpose" (convert vertical to horizontal) . 

    Pls help on this anyone....

  • SSC Venu - Wednesday, May 9, 2018 8:49 AM

    Thom A - Wednesday, May 9, 2018 7:41 AM

    You mean you want to create a delimited list? What have you tried so far?This is a very common question for a lot of RDBMS, including SQL Server. Consider having a search of "SQL Server Delimited list" on your favourite Internet Search Engine. As you're using 2014, you'll want to look for a solution using FOR XML PATH; you won't be able to make use of STRING_AGG, as that is only available in SQL Server 2017+. Don't look at answers that use a WHILE loop or a self referencing variable, they perform poorly.

    If you get stuck, reply with the code you've tried and myself, or another user, will be more than happy to guide you in the right direction.

    I thought this is not a "delimited list". it is just like "Transpose" (convert vertical to horizontal) . 

    Pls help on this anyone....

    You can't store multiple values in a variable, no; not unless it's a table variable. If you were using a table variable though then you'd need to declare it using dynamic SQL, as (I assume) you'll have variable amount of column.

    You could dynamically transpose the data, however, I wouldn't recommend even attempting to try and store that in a variable.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Are you saying that you have data stored in a column and you want to get all of the column's items into a string variable as a comma-delimited string?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I feel like I've done the work for you now, however, maybe this'll show you what I mean.

    CREATE TABLE #Sample ([Server] varchar(30));

    INSERT INTO #Sample
    VALUES ('SQL Server'),
       ('Oracle'),
       ('Sybase'),
       ('DB2'),
       ('Teradata'),
       ('Hana'),
       ('Hadoop');
    GO
    --Delimited List
    DECLARE @a varchar(MAX);

    SET @a = STUFF((SELECT ', ' + [Server]
          FROM #Sample
          FOR XML PATH('')),1,2,''); --Note, as there is no ORDER BY the order that the Servers will appear in random.
    SELECT @a;
    GO

    DECLARE @SQL nvarchar(MAX);

    WITH CTE AS (
      SELECT [Server],
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN --SELECT NULL, as there is no order in your table
      FROM #Sample)
    SELECT @SQL =
       N'SELECT ' +
       STUFF((SELECT N',' + NCHAR(10) + N'   ' + QUOTENAME([Server],N'''') + N' AS ' + QUOTENAME([Server])
         FROM CTE
         FOR XML PATH(N'')),1,9,N'') + N';';
    PRINT @SQL;
    EXEC sp_executesql @SQL;

    GO

    DROP TABLE #Sample;
    Notice that the second dataset isn't stored in a variable; it's a dynamic SQL statement that is executed. You won't be able to (easily) get that data into a variable, as if you add another Server (for example MySQL), you need to create a different variable that has 8 columns in it, rather than 7. It's not a scalable solution.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Wednesday, May 9, 2018 9:14 AM

    I feel like I've done the work for you now, however, maybe this'll show you what I mean.

    CREATE TABLE #Sample ([Server] varchar(30));

    INSERT INTO #Sample
    VALUES ('SQL Server'),
       ('Oracle'),
       ('Sybase'),
       ('DB2'),
       ('Teradata'),
       ('Hana'),
       ('Hadoop');
    GO
    --Delimited List
    DECLARE @a varchar(MAX);

    SET @a = STUFF((SELECT ', ' + [Server]
          FROM #Sample
          FOR XML PATH('')),1,2,''); --Note, as there is no ORDER BY the order that the Servers will appear in random.
    SELECT @a;
    GO

    DECLARE @SQL nvarchar(MAX);

    WITH CTE AS (
      SELECT [Server],
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN --SELECT NULL, as there is no order in your table
      FROM #Sample)
    SELECT @SQL =
       N'SELECT ' +
       STUFF((SELECT N',' + NCHAR(10) + N'   ' + QUOTENAME([Server],N'''') + N' AS ' + QUOTENAME([Server])
         FROM CTE
         FOR XML PATH(N'')),1,9,N'') + N';';
    PRINT @SQL;
    EXEC sp_executesql @SQL;

    GO

    DROP TABLE #Sample;
    Notice that the second dataset isn't stored in a variable; it's a dynamic SQL statement that is executed. You won't be able to (easily) get that data into a variable, as if you add another Server (for example MySQL), you need to create a different variable that has 8 columns in it, rather than 7. It's not a scalable solution.

    Thom A   ---    Thanks a lot. perfect solution.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply