Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Get columns name

Get columns name

Scheduled Pinned Locked Moved Database
databasequestion
17 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Victor Nijegorodov

    I'd implement such a "trick" in a stored procedure that gets a query as an argument, then creates the temporary View, uses

    SELECT [TABLE_NAME]
    ,[COLUMN_NAME]
    FROM [INFORMATION_SCHEMA].[COLUMNS]
    where TABLE_NAME =

    to return the recordset with the tables/columns names, then delete the temp view. PS: this should work with SQL Server 2008 and above.

    _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #7

    Sound good ! Because this solution should be cross server platform, it is available on the other SQL servers, like Oracle, Informix, MySQL, and so on ?

    V 1 Reply Last reply
    0
    • _ _Flaviu

      Sound good ! Because this solution should be cross server platform, it is available on the other SQL servers, like Oracle, Informix, MySQL, and so on ?

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #8

      You have to test it yourself! Just check out the links I gave you [here](https://www.codeproject.com/Messages/5808300/Re-Get-columns-name)

      1 Reply Last reply
      0
      • _ _Flaviu

        If I run a SQL statement, like this:

        SELECT * FROM my_table

        is there any SQL statement / trick to find the name of every column from '*' ? Because if I write:

        SELECT id, name, age FROM my_table

        yes, I could extract the columns name, by text analyze. But how about '*' case ?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #9

        If you load your query into a DataTable, you can get the column names from meta data. [datatable - How do I get column names to print in this C# program? - Stack Overflow](https://stackoverflow.com/questions/2557937/how-do-i-get-column-names-to-print-in-this-c-sharp-program)

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        V 1 Reply Last reply
        0
        • L Lost User

          If you load your query into a DataTable, you can get the column names from meta data. [datatable - How do I get column names to print in this C# program? - Stack Overflow](https://stackoverflow.com/questions/2557937/how-do-i-get-column-names-to-print-in-this-c-sharp-program)

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #10

          AFAIK, the OP develops in C++. However, I may be wrong.

          L 1 Reply Last reply
          0
          • V Victor Nijegorodov

            AFAIK, the OP develops in C++. However, I may be wrong.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #11

            [Data Access Using ADO.NET (C++/CLI) | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/dotnet/data-access-using-adonet-cpp-cli?view=msvc-160)

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            V 1 Reply Last reply
            0
            • L Lost User

              [Data Access Using ADO.NET (C++/CLI) | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/dotnet/data-access-using-adonet-cpp-cli?view=msvc-160)

              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #12

              But it is managed C++/CLI. :sigh: It has nothing to do with the native C++. :-O

              L 1 Reply Last reply
              0
              • V Victor Nijegorodov

                But it is managed C++/CLI. :sigh: It has nothing to do with the native C++. :-O

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #13

                Yeah, but ... [Using ADO.NET in MFC Projects](https://www.codeproject.com/Articles/4735/Using-ADO-NET-in-MFC-Projects) (And it's a "database" forum) Before ADO.NET there was ADO, and MFC did ADO.

                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                V 1 Reply Last reply
                0
                • L Lost User

                  Yeah, but ... [Using ADO.NET in MFC Projects](https://www.codeproject.com/Articles/4735/Using-ADO-NET-in-MFC-Projects) (And it's a "database" forum) Before ADO.NET there was ADO, and MFC did ADO.

                  It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #14

                  Interesting idea. Thank you! BTW, I used ADO in my big VS2010 project (C++/MFC with ADO with SQL Server) from 2009 to 2015, of course without any mixture with managed code! ;)

                  1 Reply Last reply
                  0
                  • _ _Flaviu

                    If I run a SQL statement, like this:

                    SELECT * FROM my_table

                    is there any SQL statement / trick to find the name of every column from '*' ? Because if I write:

                    SELECT id, name, age FROM my_table

                    yes, I could extract the columns name, by text analyze. But how about '*' case ?

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #15

                    Try this select * from table1 where 1=1 this should return an empty datatable with all the columns. Simply iterate the columns to get their names.

                    Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                    1 Reply Last reply
                    0
                    • _ _Flaviu

                      If I run a SQL statement, like this:

                      SELECT * FROM my_table

                      is there any SQL statement / trick to find the name of every column from '*' ? Because if I write:

                      SELECT id, name, age FROM my_table

                      yes, I could extract the columns name, by text analyze. But how about '*' case ?

                      M Offline
                      M Offline
                      mverbeke
                      wrote on last edited by
                      #16

                      Try: SELECT [name]. [column_id] AS [Ordinal] FROM sys.columns WHERE [object_id] = OBJECT_ID('MyTable') ORDER BY [column_id]; That will get the column names and ordinal position.

                      Richard DeemingR 1 Reply Last reply
                      0
                      • M mverbeke

                        Try: SELECT [name]. [column_id] AS [Ordinal] FROM sys.columns WHERE [object_id] = OBJECT_ID('MyTable') ORDER BY [column_id]; That will get the column names and ordinal position.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #17

                        I suggest you read the documentation:

                        sys.columns (Transact-SQL) - SQL Server | Microsoft Docs[^]:

                        Column IDs might not be sequential.

                        If a column has ever been dropped from the table, you will end up with gaps in the column ID sequence. To get a true ordinal position, you'd need to use the ROW_NUMBER windowing function - for example:

                        SELECT [name], ROW_NUMBER() OVER (ORDER BY [column_id]) As [Ordinal]
                        FROM sys.columns
                        WHERE [object_id] = OBJECT_ID('MyTable')
                        ORDER BY [column_id]


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups