将具有数据的列更改为现有的表
我有一个如下图所示的SQL表,但有更多的行(大约500):
当前表
+----------------+--------+ | EmployeeID | EMP | +----------------+--------+ | 01 | val | +----------------+--------+ | 02 | val | +----------------+--------+ | 03 | val | +----------------+--------+ | 04 | val | +----------------+--------+
和下面的Excel工作表除了其他的OtherID列以外,具有相同的数据:
Excel工作表
+------------+----------------+--------+ | OtherID | EmployeeID | EMP | +------------+----------------+--------+ | 001 | 01 | val | +------------+----------------+--------+ | 002 | 02 | val | +------------+----------------+--------+ | 003 | 03 | val | +------------+----------------+--------+ | 004 | 04 | val | +------------+----------------+--------+
如何通过添加缺less的OtherID列来更新原始表(不删除并创build新表),并确保OutherID与EmployeeID相关?
SQL中的所需结果
+------------+----------------+--------+ | OtherID | EmployeeID | EMP | +------------+----------------+--------+ | 001 | 01 | val | +------------+----------------+--------+ | 002 | 02 | val | +------------+----------------+--------+ | 003 | 03 | val | +------------+----------------+--------+ | 004 | 04 | val | +------------+----------------+--------+
谢谢。
1)将Excel工作表数据插入虚拟表。 (例如: BulkInsert
)
2)使用下一个代码:
Alter table Originaltable ADD OtherID datatype; go UPDATE Originaltable SET Originaltable.OtherID = DummyTable.OtherID FROM OriginaltableINNER JOIN DummyTable ON Originaltable.id = DummyTable.id