-1

I am still finding my feet in SQL Server, and I am trying to do a simple column addition and it is throwing an error. I am trying to add the KinAdr3 column after the KinAdr2 column but it is throwing the following error

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'AFTER'.

Below is the SQL statement I am trying to execute

ALTER TABLE TBL_MEDICAL_Patient
ADD KinAdr3 nVARCHAR(50) 
AFTER KinAdr2

Thanks in advance

6
  • This might help. Commented Oct 19, 2015 at 8:27
  • 1
    You don't need the "AFTER" clause.. And when modifying a table, you can't add a column in a specific position (well, not easily).. Commented Oct 19, 2015 at 8:27
  • The error speaks true - there is no after in alter table. You can only add it to the end Commented Oct 19, 2015 at 8:28
  • @RaduGheorghiu - He is trying create a new column AFTER a existing column instead of last column Commented Oct 19, 2015 at 8:28
  • 1
    @Rahul column order does not matter for storage or querying Commented Oct 19, 2015 at 8:35

1 Answer 1

4

The reason is simple as the above syntax is valid in MySQL but is not valid in SQL Server. In SQL Server following syntax is valid:

ALTER TABLE tablename ADD columnname INT

However, a user wanted to add the column between two of the columns. SQL Server is relational engine. The order of the column should not matter in any of the T-SQL operations. It does not matter in most of the cases (except when the table is extra large, and it has many NULL columns it impacts the size of the table). In reality whenever user wants to add a column to the table, he/she should just the column and later retrieve the column in a specific order in the table using column names.

It is always a good idea to specify the name of the column in the T-SQL query (using * is indeed a bad idea but that is out of scope of this blog post).

For more details please see the SQL SERVER – How to Add Column at Specific Location in Table

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.