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 0 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    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 ?

    V L M M 4 Replies 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 ?

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

      [How can I get column names from a table in SQL Server? - Stack Overflow](https://stackoverflow.com/questions/1054984/how-can-i-get-column-names-from-a-table-in-sql-server) [sql - How can I get column names from a table in Oracle? - Stack Overflow](https://stackoverflow.com/questions/452464/how-can-i-get-column-names-from-a-table-in-oracle) [information schema - How to find all the tables in MySQL with specific column names in them? - Stack Overflow](https://stackoverflow.com/questions/193780/how-to-find-all-the-tables-in-mysql-with-specific-column-names-in-them) [postgresql - List all columns for a specified table - Database Administrators Stack Exchange](https://dba.stackexchange.com/questions/22362/list-all-columns-for-a-specified-table)

      _ 1 Reply Last reply
      0
      • V Victor Nijegorodov

        [How can I get column names from a table in SQL Server? - Stack Overflow](https://stackoverflow.com/questions/1054984/how-can-i-get-column-names-from-a-table-in-sql-server) [sql - How can I get column names from a table in Oracle? - Stack Overflow](https://stackoverflow.com/questions/452464/how-can-i-get-column-names-from-a-table-in-oracle) [information schema - How to find all the tables in MySQL with specific column names in them? - Stack Overflow](https://stackoverflow.com/questions/193780/how-to-find-all-the-tables-in-mysql-with-specific-column-names-in-them) [postgresql - List all columns for a specified table - Database Administrators Stack Exchange](https://dba.stackexchange.com/questions/22362/list-all-columns-for-a-specified-table)

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

        Yes, I know that, but the question is, how can I know the columns name for the following SELECT (programmatically):

        SELECT * FROM table1, table 2 WHERE table1.id = table2.id;

        ?

        V 1 Reply Last reply
        0
        • _ _Flaviu

          Yes, I know that, but the question is, how can I know the columns name for the following SELECT (programmatically):

          SELECT * FROM table1, table 2 WHERE table1.id = table2.id;

          ?

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

          I'd just do it the same way as for a single table: - obtain column names for table1; - obtain column names for table2; - ...

          _ 1 Reply Last reply
          0
          • V Victor Nijegorodov

            I'd just do it the same way as for a single table: - obtain column names for table1; - obtain column names for table2; - ...

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

            So, you are saying that I need to do a static analyze on SELECT text and see what columns I have, if I enumerate the columns in the SELECT statement, then I have it, if I don't (I have '*') then I have to query the tables names / columns name and will find them. I thought there is a SQL trick to find the columns name in any circumstances.

            V 1 Reply Last reply
            0
            • _ _Flaviu

              So, you are saying that I need to do a static analyze on SELECT text and see what columns I have, if I enumerate the columns in the SELECT statement, then I have it, if I don't (I have '*') then I have to query the tables names / columns name and will find them. I thought there is a SQL trick to find the columns name in any circumstances.

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

              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.

              _ 1 Reply Last reply
              0
              • 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