tags:

views:

37

answers:

2

I have an SQL table called tbl, im trying to add the columns A, B and C to it.

When i execute the command :

String addcolumns = "ALTER TABLE SqlCreatedTbl ADD  A char(50) ;";
                        ......
             cmd = new SqlCommand(addcolumns, conn);
             conn.Open();
             cmd.ExecuteNonQuery();

The column is added !

However, when i try to add multiple columns, it does NOT work, it gives me an error.. the command im writting for adding multiple columns is the following:

addcolumns = "ALTER TABLE SqlCreatedTbl ADD  ( A char(50),  B char(50), C char(50) );";

the debugger highlights the line : cmd.ExecuteNonQuery(); and throws the following exception:

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '('.

+2  A: 

Get rid of the parentheses you've added in the ADD clause. You don't have them in the single column version, and you don't need them with multiple columns either. Specify ADD once and then just comma-separate your list

rchern
Thank you !! it working now ! how do i mark your answer as corrrect? im new to this forum
PeacefulSoul
There's a green checkmark underneath the score to the left of my post. This marks it as the accepted answer. You can only have one accepted answer. Choose the one that helps you the most. For answers that are basically duplicates, convention is to accept the earliest one.
rchern
+1  A: 

If you're interacting with an SQL Server database (using T-SQL), you must not place parentheses around your list of column definitions even when adding multiple columns:

ALTER TABLE SqlCreatedTbl ADD A char(50), B char(50), C char(50);
BoltClock
Thank you !! it working now ! how do i mark your answer as correct? im new to this forum
PeacefulSoul
Click the tick at the left side just below the number. You may only mark one answer as accepted per question, so choose either one of ours.
BoltClock