我尝试了以下,但我有一个语法错误
ALTER TABLE Grades ( DROP COLUMN (Student_FamilyName,Student_Name),ADD Student_id INT );
是否可以在相同的ALTER TABLE语句中执行DROP和ADD?
解决方法
如果你看看
ALTER TABLE SYTAX
你会看到这个
ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name { ALTER COLUMN column_name { [ type_schema_name. ] type_name [ ( { precision [,scale ] | max | xml_schema_collection } ) ] [ COLLATE collation_name ] [ NULL | NOT NULL ] [ SPARSE ] | {ADD | DROP } { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION | SPARSE } } | [ WITH { CHECK | NOCHECK } ] | ADD { <column_definition> | <computed_column_definition> | <table_constraint> | <column_set_definition> } [,...n ] | DROP { [ CONSTRAINT ] { constraint_name [ WITH ( <drop_clustered_constraint_option> [,...n ] ) ] } [,...n ] | COLUMN { column_name } [,...n ] } [,...n ]
这可以减少到
ALTER TABLE {ALTER COLUMN column_name |添加| DROP}
根据Transact-SQL Syntax Conventions (Transact-SQL) (垂直条)
Separates Syntax items enclosed in brackets or braces. You can use only one of the items.
所以你不能在一个语句中改变,删除或添加.你也有括号和逗号不起作用.所以你需要
ALTER TABLE Grades DROP COLUMN (Student_FamilyName,Student_Name); ALTER TABLE Grades ADD Student_id INT;
如果你需要它们是一个原子行为,你只需要包装交易