Adding a column after another column within SQL

Sql

Sql Problem Overview


How do I add a column after another column within MS SQL by using SQL query?

TempTable ID int, Type nvarchar(20), Active bit

NewTable ID int, Type nvarchar(20), Description text, Active bit

That is what I want, how do I do that

Sql Solutions


Solution 1 - Sql

Assuming MySQL (EDIT: posted before the SQL variant was supplied):

ALTER TABLE myTable ADD myNewColumn VARCHAR(255) AFTER myOtherColumn

The AFTER keyword tells MySQL where to place the new column. You can also use FIRST to flag the new column as the first column in the table.

Solution 2 - Sql

It depends on what database you are using. In MySQL, you would use the "ALTER TABLE" syntax. I don't remember exactly how, but it would go something like this if you wanted to add a column called 'newcol' that was a 200 character varchar:

ALTER TABLE example ADD newCol VARCHAR(200) AFTER otherCol;

Solution 3 - Sql

In a Firebird database the AFTER myOtherColumn does not work but you can try re-positioning the column using:

ALTER TABLE name ALTER column POSITION new_position

I guess it may work in other cases as well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmattgconView Question on Stackoverflow
Solution 1 - SqlHamishView Answer on Stackoverflow
Solution 2 - SqlHersheezyView Answer on Stackoverflow
Solution 3 - SqlEvangelos BempelisView Answer on Stackoverflow