如何执行ALTER TABLE查询? [英] How to execute an ALTER TABLE query?
问题描述
我有一个名为tbl的SQL表,我试图向其中添加列A,B和C.
I have an SQL table called tbl, im trying to add the columns A, B and C to it.
当我执行命令时:
String addcolumns = "ALTER TABLE SqlCreatedTbl ADD A char(50) ;";
......
cmd = new SqlCommand(addcolumns, conn);
conn.Open();
cmd.ExecuteNonQuery();
该列已添加!
但是,当我尝试添加多列时,它不起作用,这给了我一个错误.用于添加多列的命令im编写如下:
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) );";
调试器突出显示以下行: cmd.ExecuteNonQuery();
并引发以下异常:
the debugger highlights the line : cmd.ExecuteNonQuery();
and throws the following exception:
异常详细信息:System.Data.SqlClient.SqlException:'('附近的语法不正确.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '('.
推荐答案
如果要与SQL Server数据库进行交互(使用T-SQL),则即使添加多个列,也不能在列定义列表中加上括号:
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);
这篇关于如何执行ALTER TABLE查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!