如果我想从其他数据库迁移客户数据,例如:
SET IDENTITY_INSERT TCustomer ON
GO
INSERT INTO TCustomer
(IDCustomer,UnitHolderIDNo,FullName,ExternalUnitHolder)
SELECT
IDCustomer = ROW_NUMBER() OVER (ORDER BY STC.UnitHolderIDNo ASC) --is there a better method?
,UnitHolderIDNo = STC.UnitHolderIDNo
,FullName = STC.FullName2
,ExternalUnitHolder = STC.ExternalUnitHolder
FROM
SIAR.dbo.TCustomer STC
GO
SET IDENTITY_INSERT TCustomer OFF
编辑:我想知道除了使用row_number()之外还有更好的方法吗?因为如果我不迁移表的所有记录,会有一些跳过的数字不是吗?
答案 0 :(得分:1)
啊,我现在看到了你想要的东西。
INSERT INTO TCustomer
(UnitHolderIDNo,FullName,ExternalUnitHolder)
SELECT
UnitHolderIDNo = STC.UnitHolderIDNo
,FullName = STC.FullName2
,ExternalUnitHolder = STC.ExternalUnitHolder
FROM
SIAR.dbo.TCustomer STC
就是这样。只是不要指定自动递增列,让它为您创建ID。如果需要,您可以添加ORDER BY UnitHolderIDNo
。