I need to be able to change a table column name from within my C# code. The system in this part of the application is intended to be highly configurable and column names on the table being operated upon can change. The code adds square brackets to the column name (because the user might set up a column name with one or more spaces) but I'm not sure if I'm doing it right because I'm getting an error.
The code that builds the SQL command is:
SqlCommand =new SqlCommand("ALTER TABLE wto_facilities CHANGE [" +oldAccomType +"][" + accomType.Text +"] varchar(20)", conn);
On the first test run, the code produces the following: ALTER TABLE wto_facilities CHANGE [Hotel] [Hotels] varchar(20)
However, it's giving me the following error: Incorrect syntax near 'Hotels'
It looks fine to me, but it's obviously not.
To change a column name you need to use sp_Rename
EXEC sp_rename 'table.column', 'newcolumnname', 'column'
No comments:
Post a Comment