<p>If I'm adding a column to a table in Microsoft SQL Server, can I control where the column is displayed logically in queries?</p>

<p>I don't want to mess with the physical layout of columns on disk, but I would like to logically group columns together when possible so that tools like SQL Server Management Studio list the contents of the table in a convenient way.</p>

<p>I know that I can do this through SQL Management Studio by going into their "design" mode for tables and dragging the order of columns around, but I'd like to be able to do it in raw SQL so that I can perform the ordering scripted from the command line.</p>

[{'answer_id': 1621, 'author': 'vzczc', 'author_id': 224, 'author_profile': 'https://Stackoverflow.com/users/224', 'pm_score': 7, 'selected': True, 'text': '<p>You can not do this programatically (in a safe way that is) without creating a new table.  </p>\n\n<p>What Enterprise Manager does when you commit a reordering is to create a  new table, move the data and then delete the old table and rename the new table to the existing name.  </p>\n\n<p>If you want your columns in a particular order/grouping without altering their physical order, you can create a view which can be whatever you desire.</p>\n'}, {'answer_id': 1624, 'author': 'tags2k', 'author_id': 192, 'author_profile': 'https://Stackoverflow.com/users/192', 'pm_score': 3, 'selected': False, 'text': "<p>When Management Studio does it, it's creating a temporary table, copying everything across, dropping your original table and renaming the temporary table.  There's no simple equivalent T-SQL statement.</p>\n\n<p>If you don't fancy doing that, you could always create a view of the table with the columns in the order you'd like and use that?</p>\n\n<p>Edit: beaten!</p>\n"}, {'answer_id': 3571, 'author': 'Otto', 'author_id': 519, 'author_profile': 'https://Stackoverflow.com/users/519', 'pm_score': 3, 'selected': False, 'text': '<p>If I understand your question, you want to affect what columns are returned first, second, third, etc in <strong>existing</strong> queries, right?</p>\n\n<p>If all of your queries are written with <code>SELECT * FROM TABLE</code> - then they will show up in the output as they are laid out in SQL. </p>\n\n<p>If your queries are written with <code>SELECT Field1, Field2 FROM TABLE</code> - then the order they are laid out in SQL does not matter.</p>\n'}, {'answer_id': 172605, 'author': 'Milan Babuškov', 'author_id': 14690, 'author_profile': 'https://Stackoverflow.com/users/14690', 'pm_score': 0, 'selected': False, 'text': '<p>It can be done using SQL, by modifying the system tables directly. For example, look here:</p>\n\n<p><a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58912" rel="nofollow noreferrer">Alter table - Add new column in between</a></p>\n\n<p>However, I would not recommend playing with system tables, unless it\'s absolutely necessary.</p>\n'}, {'answer_id': 14637531, 'author': 'Philip Coyner', 'author_id': 2030979, 'author_profile': 'https://Stackoverflow.com/users/2030979', 'pm_score': 6, 'selected': False, 'text': "<p>I think what everyone here is missing is that although not everyone has to deal with 10's, 20's, or 1000's instances of the same software system installed throughout the country and world, those of us that design commercially sold software do so.  As a result, we expand systems over time, expand tables by adding fields as new capability is needed, and as those fields are identified do belong in an existing table, and as such, over a decade of expanding, growing, adding fields, etc to tables, and then having to work with those tables from design, to support, to sometimes digging into raw data/troubleshooting to debug new functionality bugs, it is incredibly aggravating to not have the primary information you want to see within the first handful of fields, when you may have tables with 30, 40, 50, or even 90 fields, and yes, in a strictly normalized database.</p>\n<p>I've often wished I could do this, for this exact reason.  But short of doing exactly what SQL does, building a Create Script for a new Table the way I want it, writing the Insert to it, then dropping all existing constraints, relationships, keys, index, etc etc from the existing table and renaming the &quot;new&quot; table back to the old name, and then reading all those keys, relationships, index, etc etc ....</p>\n<p>It's not only tedious, time-consuming, but ... in five more years, it will need to happen again.</p>\n<p>It's so close to worth that massive amount of work, however the point is, it won't be the last time we need this ability, since our systems will continue to grow, expand, and get fields in a wacked ordered driven by need/design additions.</p>\n<p>A majority of developers think from a single system standpoint that serves a single company or very specific hard box market.</p>\n<p>The &quot;off-the-shelf&quot; but significantly progressive designers and leaders of development in their market space will always have to deal with this problem, over and over, and would love a creative solution if anyone has one.  This could easily save my company a dozen hours a week, just not having to scroll over, or remember where &quot;that&quot; field is in the source data table.</p>\n"}, {'answer_id': 26786408, 'author': 'Steven', 'author_id': 4224071, 'author_profile': 'https://Stackoverflow.com/users/4224071', 'pm_score': 1, 'selected': False, 'text': '<p>There is one way, but its only temporarily for the query itself. For example, </p>\n\n<p>Lets say you have 5 tables. \nTable is called <code>T_Testing</code></p>\n\n<p>FirstName, LastName, PhoneNumber, Email, and Member_ID</p>\n\n<p>you want it to list their ID, then Last Name, then FirstName, then Phone then Email. </p>\n\n<p>You can do it as per the Select. </p>\n\n<pre><code>Select Member_ID, LastName, FirstName, PhoneNumber, Email\nFrom T_Testing\n</code></pre>\n\n<p>Other than that, if you just want the LastName to Show before first name for some reason, you can do it also as follows: </p>\n\n<pre><code>Select LastName, *\nFrom T_Testing\n</code></pre>\n\n<p>The only thing you wanna be sure that you do is that the OrderBy or Where Function needs to be denoted as Table.Column if you are going to be using a Where or OrderBy</p>\n\n<p>Example: </p>\n\n<pre><code>Select LastName, *\nFrom T_Testing\nOrder By T_Testing.LastName Desc\n</code></pre>\n\n<p>I hope this helps, I figured it out because I needed to do this myself. </p>\n'}, {'answer_id': 35870065, 'author': 'Paul', 'author_id': 1351464, 'author_profile': 'https://Stackoverflow.com/users/1351464', 'pm_score': 1, 'selected': False, 'text': '<ol>\n<li>Script your existing table to a query window.</li>\n<li>Run this script against a Test database (remove the Use statement)</li>\n<li>Use SSMS to make the column changes you need</li>\n<li>Click Generate Change Script (left most and bottommost icon on the\nbuttonbar, by default)</li>\n<li>Use this script against your real table</li>\n</ol>\n\n<p>All the script really does is create a second table table with the desired column orders, copies all your data into it, drops the original table and then renames the secondary table to take its place. This does save you writing it yourself though should you want a deploy script.</p>\n'}, {'answer_id': 39306116, 'author': 'Andreas', 'author_id': 941546, 'author_profile': 'https://Stackoverflow.com/users/941546', 'pm_score': 1, 'selected': False, 'text': '<p>It is not possible to change the order of the columns without recreating the whole table. If you have a few instances of the database only, you can use SSMS for this (Select the table and click "design").</p>\n\n<p>In case you have too many instances for a manual process, you should try this script:\n<a href="https://github.com/Epaminaidos/reorder-columns" rel="nofollow">https://github.com/Epaminaidos/reorder-columns</a></p>\n'}]
['https://Stackoverflow.com/questions/1610', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/328/']