Adding column between two other columns in SQL server
The simple answer is no. Is there a reason why column order is important to you?
SQL Server 2005: How to add a column to a table at the beginning of the table?
GUI method: in SQL Server Management Studio if you right click the table and choose "Design" you can then drag the column up to the top and hit save.
Note: Be aware that by doing this, SQL Server drops the table and creates it again. You won't lose your data though.
Add a new table column to specific ordinal position in Microsoft SQL Server
You have to create a temp table that mirrors the original table's schema but with the column order that you want, then copy the contents of the original to temp. Delete the original and rename the temp.
This is what SQL Management Studio does behind the scenes.
With a schema sync tool, you can generate these scripts automatically.
Add a column with a default value to an existing table in SQL Server
Syntax:
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
WITH VALUES
Example:
ALTER TABLE SomeTable
ADD SomeCol Bit NULL --Or NOT NULL.
CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is autogenerated.
DEFAULT (0)--Optional Default-Constraint.
WITH VALUES --Add if Column is Nullable and you want the Default Value for Existing Records.
Notes:
Optional Constraint Name:
If you leave out CONSTRAINT D_SomeTable_SomeCol
then SQL Server will autogenerate
a Default-Contraint with a funny Name like: DF__SomeTa__SomeC__4FB7FEF6
Optional With-Values Statement:
The WITH VALUES
is only needed when your Column is Nullable
and you want the Default Value used for Existing Records.
If your Column is NOT NULL
, then it will automatically use the Default Value
for all Existing Records, whether you specify WITH VALUES
or not.
How Inserts work with a Default-Constraint:
If you insert a Record into SomeTable
and do not Specify SomeCol
's value, then it will Default to 0
.
If you insert a Record and Specify SomeCol
's value as NULL
(and your column allows nulls),
then the Default-Constraint will not be used and NULL
will be inserted as the Value.
Notes were based on everyone's great feedback below.
Special Thanks to:
@Yatrix, @WalterStabosz, @YahooSerious, and @StackMan for their Comments.
How to add columns to a view in SQL Server 2005
You can use ALTER VIEW to achieve the result you are looking for.
This will just act as dropping the existing view and adding new columns from your new select statement. However, this is better than dropping your existing view and creating a new view because the Alter view will retain the permissions granted to users.
Uploading Data For A New Column In MSSQL
I know of two ways:
Edit the spreadsheet, create a formula like
="update mytable set mynewcolumn = " + A1 + " where " + B1 + " = primaryKeyColumn"
where A1 represents the first cell with data and B1 is the primary key value to the row that will be updated. Then drag the cell down by the lower right corner so the formula is repeated. Copy the text produced by the formula, paste into SQL Mgmt Studio and run.
Or, you can use MSSQL's import engine which allows you to select a spreadsheet as your datasource. From there you can map your column to the new column.
Add a column to a table, if it does not already exist
You can use a similar construct by using the sys.columns
table io sys.objects
.
IF NOT EXISTS (
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID(N'[dbo].[Person]')
AND name = 'ColumnName'
)
Adding an identity to an existing column
You can't alter the existing columns for identity.
You have 2 options,
Create a new table with identity & drop the existing table
Create a new column with identity & drop the existing column
Approach 1. (New table) Here you can retain the existing data values on the newly created identity column. Note that you will lose all data if 'if not exists' is not satisfied, so make sure you put the condition on the drop as well!
CREATE TABLE dbo.Tmp_Names
(
Id int NOT NULL
IDENTITY(1, 1),
Name varchar(50) NULL
)
ON [PRIMARY]
go
SET IDENTITY_INSERT dbo.Tmp_Names ON
go
IF EXISTS ( SELECT *
FROM dbo.Names )
INSERT INTO dbo.Tmp_Names ( Id, Name )
SELECT Id,
Name
FROM dbo.Names TABLOCKX
go
SET IDENTITY_INSERT dbo.Tmp_Names OFF
go
DROP TABLE dbo.Names
go
Exec sp_rename 'Tmp_Names', 'Names'
Approach 2 (New column) You can’t retain the existing data values on the newly created identity column, The identity column will hold the sequence of number.
Alter Table Names
Add Id_new Int Identity(1, 1)
Go
Alter Table Names Drop Column ID
Go
Exec sp_rename 'Names.Id_new', 'ID', 'Column'
See the following Microsoft SQL Server Forum post for more details:
How to alter column to identity(1,1)
Related Topics
How to Determine Which Columns Are Shared Between Two Tables
Identify a 3-Column Pk Duplicate in Vba Access
How to Make a SQL Query for Last Transaction of Every Account
SQL Query in Spark/Scala Size Exceeds Integer.Max_Value
Select Count of Rows in Another Table in a Postgres Select Statement
How to Execute Table Valued Function
How to Select Using with Recursive Clause
Simple Graph Search Algorithm in SQL (Postgresql)
How to Check If a Table Is Locked in SQL Server
Multiple Left Joins on Multiple Tables in One Query
Select * from Table1 That Does Not Exist in Table2 with Conditional
How to Do a Find/Replace in T-Sql