使用IDENTITY列将INSERT记录记录到SQL表

前端之家收集整理的这篇文章主要介绍了使用IDENTITY列将INSERT记录记录到SQL表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个sql
CREATE TABLE Notes(
    NoteID [int] IDENTITY(1,1) NOT NULL,NoteTitle [nvarchar](255) NULL,NoteDescription [nvarchar](4000) NULL
) CONSTRAINT [PK_Notes] PRIMARY KEY CLUSTERED 
(
    NoteID ASC
)WITH (PAD_INDEX  = OFF,STATISTICS_NORECOMPUTE  = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS  = ON,ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

而且我想从临时表中复制记录,包括使用SQL查询的NoteID ..

这是我的脚本:

SET IDENTITY_INSERT Notes OFF

INSERT INTO Notes (NoteID,NoteTitle,NoteDescription)
SELECT NoteID,NoteDescription from Notes_Temp

SET IDENTITY_INSERT Notes ON

用这个脚本,我收到一个错误

Cannot insert explicit value for identity column in table 'Notes' when IDENTITY_INSERT is set to OFF.

有没有其他方法可以使用SQL查询将标识列插入到表中?

解决方法

改变关闭关闭
SET IDENTITY_INSERT Notes ON

INSERT INTO Notes (NoteID,NoteDescription from Notes_Temp

SET IDENTITY_INSERT Notes OFF

猜你在找的MsSQL相关文章