我正在使用Merge语句在我的存储过程中插入/更新数据。
MERGE [dbo].[tReserveData_4541] AS Target
USING (SELECT * FROM [dbo].[tblcangrowhitesh]) AS Source
ON ( [Source].[PK_ID] = [Target].[PK_ID])
WHEN MATCHED THEN
UPDATE SET [Target].[mgender] = Source.[mgender]
WHEN NOT MATCHED BY TARGET THEN
INSERT ([Granularity], [PK_ID], [ROWID],
[mgender], [mma1], [mma2], [mma3],
[mmadmincost], [mmcumulativevolume],
[mmcurrency], [mmdate],
[mmfileimporteddate], [mmfilename])
VALUES ([Source].[Granularity], [Source].[PK_ID], [Source].[ROWID],
[Source].[mgender], [Source].[mma1], [Source].[mma2], [Source].[mma3],
[Source].[mmadmincost], [Source].[mmcumulativevolume],
[Source].[mmcurrency], [Source].[mmdate],
[Source].[mmfileimporteddate], [Source].[mmfilename])
如您所见,我将在[PK_ID]
语句中插入标识列,即MERGE
。但我无法这样做。
答案 0 :(得分:9)
我不熟悉MERGE
命令,但是当您直接插入标识列时,您是否不必使用IDENTITY_INSERT命令? e.g。
SET IDENTITY_INSERT [dbo].[tReserveData_4541] on
MERGE...
SET IDENTITY_INSERT [dbo].[tReserveData_4541] off
答案 1 :(得分:0)
如果您不需要在目标上生成值,请删除PK_ID上的IDENTITY属性。
为此,您可以添加PK_ID_temp列UPDATE [dbo].[tReserveData_4541] SET PK_ID_temp = PK_ID
。删除PK_ID列,然后将PK_ID_temp重命名为PK_ID。
这可能还需要删除主键,删除所有关联索引并重建所有非聚簇索引(如果它是聚簇索引)。