views:

302

answers:

2

I am having trouble with a ALTER TABLE command that I am trying to use on a MS Access database in a C# project. I am trying to rename a column and change the type at the same time.

Here is my command:

string sqlCommand = "ALTER TABLE " + tableName + " CHANGE [" + oldColName + "] [" 
    + newColName + "] " + colType;

What is wrong in this command and what do I need to do to make this it work?


*Edits:

-The type and the names of the table, new column and old column are not the problem!

-The exception that is catched is :

Syntax error in ALTER TABLE statement.

-The final string looks like this:

ALTER TABLE [Big List] CHANGE [num] [test] CHARACTER

-Connection provider:

Microsoft.ACE.OLEDB.12.0

A: 

Try ALTER TABLE [Big List] ALTER COLUMN [num] [test] CHARACTER

Russell Steen
That does not work :S
Partial
The only other thing I can see wrong is perhaps the lack of field sizeCHARACTER(10) for instance.
Russell Steen
That did not work either.
Partial
Are you folks testing your DDL in Access/Jet/ACE before posting it? If not, you really are wasting your time. Every db engine has its own SQL dialects, and Jet/ACE has always been a latecomer to SQL compatibility (it was created in 1991 and SQL was not introduced to the engine until the following year, and it was a dialect of SQL 89, because SQL 92 was not even accepted yet).
David-W-Fenton
@David W. Fenton: Your history is wrong. ANSI-92 Query Mode was introduced into Jet 4.0 circa 1999 but the SQL Server team was prevented from making it compliant with the SQL-92 standard by the Windows team because doing so would break their apps. The usual MS politics (rolls eyes).
onedaywhen
Renaming a column is outside the scope of the SQL-92 standard. The only alter column actions SQL-92 defines is <set column default clause> and <drop column default clause> and the Access database engine fully supports the SQL-92 syntax here. If a SQL DDL syntax provides means to rename a column then these will be vendor extensions and SQL-92 will not apply.
onedaywhen
Yes, I ran mine in my copy of Access before posting, but I'm not on the same version as he is, and depending on details, he could be calling a different engine.
Russell Steen
+2  A: 

I don't think you can rename a column with SQL and access.

The best way to achieve this is to create a new column with the new name, update the new column and drop the old one.

ALTER  TABLE [Big List] ADD COLUMN [num] YOURTYPE;
UPDATE [Big List] SET [num] = [test];
ALTER  TABLE [Big List] DROP COLUMN [test];
Francis B.
Thats brilliant! I'll try that out!
Partial
Finally, it did not work.. The update table had a syntax error so I had column after set. Then I got another error but this time the error was: The record is too large.
Partial
Never mind about the record error... I had added too many columns hahaha! But I still have a syntax error with the update statement you gave me
Partial
I think there needs to be a value instead of [test] which is column in the update statement...
Partial
I'm not an expert with sql command for Access but this command for MS SQL works: UPDATE mytable SET mycol1 = mycol2 so probably have to tweek the update command a little bit.
Francis B.
Damn Access.. why does it have to do everything different :S
Partial
It finally works! I needed to remove the TABLE.. so basically the command has to be : UPDATE tableName DROP COLUMN oldColumn
Partial
lololololol :P This is cool
Partial
Glad you got it working!
Russell Steen
@Partial: Oups! I forgot to remove TABLE after UPDATE...My fault :) Thanks to onedaywhen for the correction
Francis B.
Thank you everyone for your time!
Partial