我有一个名为[RlasRmSysUsers]的MS SQL(2012)表,它有一个三个不同字段的超级密钥。我试图复制记录,但将它们保存回具有不同ID的表。我决定创建一个临时表,并用输入的ID号绑定条目。然后我更改了临时表中的ID号,最后我将临时表插回到原始表中。我可以创建,填充和编辑临时表而没有任何问题,但是当我尝试将临时表插入主表时出现错误。它说我违反了密钥并取消了INSERT流程。我检查并且ID还没有存在于原始表中。想法?
我创建了新的临时表 -
SELECT [UserID]
,[UserRoleID]
,[UserFinBlockCd]
,[UserAOR]
,[UserOrgID]
,[UserName]
,[UserStatus]
,[UserActiveProfile]
,[LastChangeDt]
,[LastChangeBy]
into #TempRLAS FROM [RlasRmSysUsers] WHERE UserID='12345.tmp' and UserActiveProfile = 1
我更新临时表
UPDATE #TempRLAS SET UserID='998877.POS', UserName = '', [LastChangeDt]= GETDATE(), [LastChangeBy]='990011.me';
最后,我尝试将其添加到原始表中,然后将其删除
INSERT INTO [RlasRmSysUsers] SELECT * FROM #TempRLAS;
DROP TABLE #TempRLAS;
表格结构
CREATE TABLE [dbo].[RlasRmSysUsers](
[UserID] [varchar](35) NOT NULL,
[UserRoleID] [varchar](12) NOT NULL,
[UserFinBlockCd] [char](4) NOT NULL,
[UserAOR] [varchar](12) NOT NULL,
[UserOrgID] [varchar](10) NULL,
[UserName] [varchar](35) NOT NULL,
[UserStatus] [char](1) NOT NULL,
[UserActiveProfile] [char](1) NOT NULL,
[LastChangeDt] [datetime] NOT NULL,
[LastChangeBy] [varchar](35) NOT NULL,
CONSTRAINT [PK_RlasRmSysUsers_UserID_UserRoleID_UserFinBlockCd] PRIMARY KEY NONCLUSTERED
(
[UserID] ASC,
[UserRoleID] ASC,
[UserFinBlockCd] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
答案 0 :(得分:0)