Displaying the order of the columns of a tableWhy should I create an ID column when I can use others as key fields?How do you create a relationship to a non-primary key in SQL Server?Oracle GoldenGate add trandata errorsError while executing SSIS package which contains Script component through SQL Server Agent JobShould I mark a composite index as unique if it contains the primary key?Can I rely on reading SQL Server Identity values in order?Order by custom filter without certain dataSQL Server query problem when selecting data from child table based on column in parent tableQuery runs slowly when a non-indexed column is added to the WHERE clauseProper table design for sparse primary key

Star/Wye electrical connection math symbol

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

HashMap containsKey() returns false although hashCode() and equals() are true

How do I define a right arrow with bar in LaTeX?

Was the picture area of a CRT a parallelogram (instead of a true rectangle)?

What is the opposite of 'gravitas'?

Do I need a multiple entry visa for a trip UK -> Sweden -> UK?

how to analyze "是其于主也至忠矣"

What are the ramifications of creating a homebrew world without an Astral Plane?

Unattended/Unattended to?

How to avoid InDesign adding pages automatically?

If a character can use a +X magic weapon as a spellcasting focus, does it add the bonus to spell attacks or spell save DCs?

Why does John Bercow say “unlock” after reading out the results of a vote?

How can I use the arrow sign in my bash prompt?

What would happen if the UK refused to take part in EU Parliamentary elections?

What would be the benefits of having both a state and local currencies?

Personal Teleportation as a Weapon

Was Spock the First Vulcan in Starfleet?

Is there a problem with hiding "forgot password" until it's needed?

What is the intuitive meaning of having a linear relationship between the logs of two variables?

MaTeX, font size, and PlotLegends

How to be diplomatic in refusing to write code that breaches the privacy of our users

Mapping a list into a phase plot

Displaying the order of the columns of a table



Displaying the order of the columns of a table


Why should I create an ID column when I can use others as key fields?How do you create a relationship to a non-primary key in SQL Server?Oracle GoldenGate add trandata errorsError while executing SSIS package which contains Script component through SQL Server Agent JobShould I mark a composite index as unique if it contains the primary key?Can I rely on reading SQL Server Identity values in order?Order by custom filter without certain dataSQL Server query problem when selecting data from child table based on column in parent tableQuery runs slowly when a non-indexed column is added to the WHERE clauseProper table design for sparse primary key













3















I created a table, and want to find the display the order of its columns.
Should I use the following query to display the info ordered by column_id?



select * from sys.columns c
where c.object_id = object_id('Customer')
order by column_id


create table dbo.Customer
(
CustomerId int primary key,
CustomerName varchar(255),
CustomerAddress varchar(255),
EnrollmentDate date
)


Reading Microsoft SQL Server documentation, I am seeing the information below, so want to be sure:



Column name Data type Description
----------- --------- ----------------------------------------------
column_id: int ID of the column. Is unique within the object.
Column IDs might not be sequential.









share|improve this question









New contributor




John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    3















    I created a table, and want to find the display the order of its columns.
    Should I use the following query to display the info ordered by column_id?



    select * from sys.columns c
    where c.object_id = object_id('Customer')
    order by column_id


    create table dbo.Customer
    (
    CustomerId int primary key,
    CustomerName varchar(255),
    CustomerAddress varchar(255),
    EnrollmentDate date
    )


    Reading Microsoft SQL Server documentation, I am seeing the information below, so want to be sure:



    Column name Data type Description
    ----------- --------- ----------------------------------------------
    column_id: int ID of the column. Is unique within the object.
    Column IDs might not be sequential.









    share|improve this question









    New contributor




    John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      3












      3








      3








      I created a table, and want to find the display the order of its columns.
      Should I use the following query to display the info ordered by column_id?



      select * from sys.columns c
      where c.object_id = object_id('Customer')
      order by column_id


      create table dbo.Customer
      (
      CustomerId int primary key,
      CustomerName varchar(255),
      CustomerAddress varchar(255),
      EnrollmentDate date
      )


      Reading Microsoft SQL Server documentation, I am seeing the information below, so want to be sure:



      Column name Data type Description
      ----------- --------- ----------------------------------------------
      column_id: int ID of the column. Is unique within the object.
      Column IDs might not be sequential.









      share|improve this question









      New contributor




      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I created a table, and want to find the display the order of its columns.
      Should I use the following query to display the info ordered by column_id?



      select * from sys.columns c
      where c.object_id = object_id('Customer')
      order by column_id


      create table dbo.Customer
      (
      CustomerId int primary key,
      CustomerName varchar(255),
      CustomerAddress varchar(255),
      EnrollmentDate date
      )


      Reading Microsoft SQL Server documentation, I am seeing the information below, so want to be sure:



      Column name Data type Description
      ----------- --------- ----------------------------------------------
      column_id: int ID of the column. Is unique within the object.
      Column IDs might not be sequential.






      sql-server sql-server-2016






      share|improve this question









      New contributor




      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 51 mins ago









      MDCCL

      6,85331745




      6,85331745






      New contributor




      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      John ThomasJohn Thomas

      211




      211




      New contributor




      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      John Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          3














          column_id is a reasonable proxy for the column ordinal, since it is impossible to insert a column between two existing columns in SQL Server without dropping and recreating the table.



          As the documentation states, column_id values may not be sequential if you drop a column from a table.



          You can also make use of the COLUMNPROPERTY() function to return the actual ordinal for each column.



          Consider a quick example:



          IF OBJECT_ID(N'dbo.t', N'U') IS NOT NULL
          DROP TABLE dbo.t;
          CREATE TABLE dbo.t
          (
          c1 int
          , c2 int
          , c3 int
          , c4 int
          );

          ALTER TABLE dbo.t DROP COLUMN c1;
          ALTER TABLE dbo.t ADD c5 int;
          ALTER TABLE dbo.t ALTER COLUMN c2 char(3);

          SELECT o.name
          , c.name
          , c.column_id
          , ordinal = COLUMNPROPERTY(c.object_id, c.name, 'ordinal')
          FROM sys.columns c
          INNER JOIN sys.objects o ON c.object_id = o.object_id
          WHERE o.name = N't'


          The output looks like:



          ╔══════╦══════╦═══════════╦═════════╗
          ║ name ║ name ║ column_id ║ ordinal ║
          ╠══════╬══════╬═══════════╬═════════╣
          ║ t ║ c2 ║ 2 ║ 1 ║
          ║ t ║ c3 ║ 3 ║ 2 ║
          ║ t ║ c4 ║ 4 ║ 3 ║
          ║ t ║ c5 ║ 5 ║ 4 ║
          ╚══════╩══════╩═══════════╩═════════╝




          share

























          • thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

            – John Thomas
            1 hour ago











          • another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

            – John Thomas
            1 hour ago


















          1














          Just to propose an additional answer that will tell you the actual column position instead of column_id



          select column_name, ORDINAL_POSITION 
          from INFORMATION_SCHEMA.COLUMNS
          where table_name = 'your_table'





          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "182"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            John Thomas is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233179%2fdisplaying-the-order-of-the-columns-of-a-table%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            column_id is a reasonable proxy for the column ordinal, since it is impossible to insert a column between two existing columns in SQL Server without dropping and recreating the table.



            As the documentation states, column_id values may not be sequential if you drop a column from a table.



            You can also make use of the COLUMNPROPERTY() function to return the actual ordinal for each column.



            Consider a quick example:



            IF OBJECT_ID(N'dbo.t', N'U') IS NOT NULL
            DROP TABLE dbo.t;
            CREATE TABLE dbo.t
            (
            c1 int
            , c2 int
            , c3 int
            , c4 int
            );

            ALTER TABLE dbo.t DROP COLUMN c1;
            ALTER TABLE dbo.t ADD c5 int;
            ALTER TABLE dbo.t ALTER COLUMN c2 char(3);

            SELECT o.name
            , c.name
            , c.column_id
            , ordinal = COLUMNPROPERTY(c.object_id, c.name, 'ordinal')
            FROM sys.columns c
            INNER JOIN sys.objects o ON c.object_id = o.object_id
            WHERE o.name = N't'


            The output looks like:



            ╔══════╦══════╦═══════════╦═════════╗
            ║ name ║ name ║ column_id ║ ordinal ║
            ╠══════╬══════╬═══════════╬═════════╣
            ║ t ║ c2 ║ 2 ║ 1 ║
            ║ t ║ c3 ║ 3 ║ 2 ║
            ║ t ║ c4 ║ 4 ║ 3 ║
            ║ t ║ c5 ║ 5 ║ 4 ║
            ╚══════╩══════╩═══════════╩═════════╝




            share

























            • thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

              – John Thomas
              1 hour ago











            • another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

              – John Thomas
              1 hour ago















            3














            column_id is a reasonable proxy for the column ordinal, since it is impossible to insert a column between two existing columns in SQL Server without dropping and recreating the table.



            As the documentation states, column_id values may not be sequential if you drop a column from a table.



            You can also make use of the COLUMNPROPERTY() function to return the actual ordinal for each column.



            Consider a quick example:



            IF OBJECT_ID(N'dbo.t', N'U') IS NOT NULL
            DROP TABLE dbo.t;
            CREATE TABLE dbo.t
            (
            c1 int
            , c2 int
            , c3 int
            , c4 int
            );

            ALTER TABLE dbo.t DROP COLUMN c1;
            ALTER TABLE dbo.t ADD c5 int;
            ALTER TABLE dbo.t ALTER COLUMN c2 char(3);

            SELECT o.name
            , c.name
            , c.column_id
            , ordinal = COLUMNPROPERTY(c.object_id, c.name, 'ordinal')
            FROM sys.columns c
            INNER JOIN sys.objects o ON c.object_id = o.object_id
            WHERE o.name = N't'


            The output looks like:



            ╔══════╦══════╦═══════════╦═════════╗
            ║ name ║ name ║ column_id ║ ordinal ║
            ╠══════╬══════╬═══════════╬═════════╣
            ║ t ║ c2 ║ 2 ║ 1 ║
            ║ t ║ c3 ║ 3 ║ 2 ║
            ║ t ║ c4 ║ 4 ║ 3 ║
            ║ t ║ c5 ║ 5 ║ 4 ║
            ╚══════╩══════╩═══════════╩═════════╝




            share

























            • thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

              – John Thomas
              1 hour ago











            • another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

              – John Thomas
              1 hour ago













            3












            3








            3







            column_id is a reasonable proxy for the column ordinal, since it is impossible to insert a column between two existing columns in SQL Server without dropping and recreating the table.



            As the documentation states, column_id values may not be sequential if you drop a column from a table.



            You can also make use of the COLUMNPROPERTY() function to return the actual ordinal for each column.



            Consider a quick example:



            IF OBJECT_ID(N'dbo.t', N'U') IS NOT NULL
            DROP TABLE dbo.t;
            CREATE TABLE dbo.t
            (
            c1 int
            , c2 int
            , c3 int
            , c4 int
            );

            ALTER TABLE dbo.t DROP COLUMN c1;
            ALTER TABLE dbo.t ADD c5 int;
            ALTER TABLE dbo.t ALTER COLUMN c2 char(3);

            SELECT o.name
            , c.name
            , c.column_id
            , ordinal = COLUMNPROPERTY(c.object_id, c.name, 'ordinal')
            FROM sys.columns c
            INNER JOIN sys.objects o ON c.object_id = o.object_id
            WHERE o.name = N't'


            The output looks like:



            ╔══════╦══════╦═══════════╦═════════╗
            ║ name ║ name ║ column_id ║ ordinal ║
            ╠══════╬══════╬═══════════╬═════════╣
            ║ t ║ c2 ║ 2 ║ 1 ║
            ║ t ║ c3 ║ 3 ║ 2 ║
            ║ t ║ c4 ║ 4 ║ 3 ║
            ║ t ║ c5 ║ 5 ║ 4 ║
            ╚══════╩══════╩═══════════╩═════════╝




            share















            column_id is a reasonable proxy for the column ordinal, since it is impossible to insert a column between two existing columns in SQL Server without dropping and recreating the table.



            As the documentation states, column_id values may not be sequential if you drop a column from a table.



            You can also make use of the COLUMNPROPERTY() function to return the actual ordinal for each column.



            Consider a quick example:



            IF OBJECT_ID(N'dbo.t', N'U') IS NOT NULL
            DROP TABLE dbo.t;
            CREATE TABLE dbo.t
            (
            c1 int
            , c2 int
            , c3 int
            , c4 int
            );

            ALTER TABLE dbo.t DROP COLUMN c1;
            ALTER TABLE dbo.t ADD c5 int;
            ALTER TABLE dbo.t ALTER COLUMN c2 char(3);

            SELECT o.name
            , c.name
            , c.column_id
            , ordinal = COLUMNPROPERTY(c.object_id, c.name, 'ordinal')
            FROM sys.columns c
            INNER JOIN sys.objects o ON c.object_id = o.object_id
            WHERE o.name = N't'


            The output looks like:



            ╔══════╦══════╦═══════════╦═════════╗
            ║ name ║ name ║ column_id ║ ordinal ║
            ╠══════╬══════╬═══════════╬═════════╣
            ║ t ║ c2 ║ 2 ║ 1 ║
            ║ t ║ c3 ║ 3 ║ 2 ║
            ║ t ║ c4 ║ 4 ║ 3 ║
            ║ t ║ c5 ║ 5 ║ 4 ║
            ╚══════╩══════╩═══════════╩═════════╝





            share













            share


            share








            edited 1 hour ago

























            answered 1 hour ago









            Max VernonMax Vernon

            51.9k13114230




            51.9k13114230












            • thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

              – John Thomas
              1 hour ago











            • another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

              – John Thomas
              1 hour ago

















            • thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

              – John Thomas
              1 hour ago











            • another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

              – John Thomas
              1 hour ago
















            thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

            – John Thomas
            1 hour ago





            thanks, can we assume column order will always go ascending order, so I if I delete columns, we get them sequenced in order, that's all I really care about, example : 1,3,4,7,8, (if we delete some columns?)

            – John Thomas
            1 hour ago













            another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

            – John Thomas
            1 hour ago





            another interesting note, Ordinal is not in Microsoft documentation, wondering if they are trying to deprecate docs.microsoft.com/en-us/sql/t-sql/functions/… , mssqltips.com/sqlservertip/1298/…

            – John Thomas
            1 hour ago













            1














            Just to propose an additional answer that will tell you the actual column position instead of column_id



            select column_name, ORDINAL_POSITION 
            from INFORMATION_SCHEMA.COLUMNS
            where table_name = 'your_table'





            share|improve this answer



























              1














              Just to propose an additional answer that will tell you the actual column position instead of column_id



              select column_name, ORDINAL_POSITION 
              from INFORMATION_SCHEMA.COLUMNS
              where table_name = 'your_table'





              share|improve this answer

























                1












                1








                1







                Just to propose an additional answer that will tell you the actual column position instead of column_id



                select column_name, ORDINAL_POSITION 
                from INFORMATION_SCHEMA.COLUMNS
                where table_name = 'your_table'





                share|improve this answer













                Just to propose an additional answer that will tell you the actual column position instead of column_id



                select column_name, ORDINAL_POSITION 
                from INFORMATION_SCHEMA.COLUMNS
                where table_name = 'your_table'






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                PadwanPadwan

                1916




                1916




















                    John Thomas is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    John Thomas is a new contributor. Be nice, and check out our Code of Conduct.












                    John Thomas is a new contributor. Be nice, and check out our Code of Conduct.











                    John Thomas is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233179%2fdisplaying-the-order-of-the-columns-of-a-table%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                    Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

                    Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar