實例:
--建表,很簡單,就不說了
CREATE TABLE [Photos] (
[PhotoID] [int] IDENTITY (1, 1) NOT NULL ,
[AlbumID] [int] NOT NULL ,
[Caption] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
)
--清空表
truncate table dbo.Photos
GO
--打開容許對被IDENTITY修飾的列進行插入的開關,默認示關閉的,也就是不容許
set IDENTITY_INSERT dbo.Photos on
insert into Photos(PhotoID,AlbumID,Caption)values(1,1,''maql1'')
insert into Photos(PhotoID,AlbumID,Caption)values(1,2,''maql2'')
insert into Photos(PhotoID,AlbumID,Caption)values(1,3,''maql3'')
insert into Photos(PhotoID,AlbumID,Caption)values(2,1,''zhangr1'')
insert into Photos(PhotoID,AlbumID,Caption)values(2,2,''zhangr2'')
insert into Photos(PhotoID,AlbumID,Caption)values(2,3,''zhangr3'')
go
--關閉IDENTITY_INSERT開關,也就是不容許對這一列進行插入,而是讓其自動增長
set IDENTITY_INSERT dbo.Photos off
insert into Photos(AlbumID,Caption)values(1,''maql1'')
insert into Photos(AlbumID,Caption)values(2,''maql2'')
insert into Photos(AlbumID,Caption)values(3,''maql3'')
insert into Photos(AlbumID,Caption)values(1,''zhangr1'')
insert into Photos(AlbumID,Caption)values(2,''zhangr2'')
insert into Photos(AlbumID,Caption)values(3,''zhangr3'')
insert into Photos(AlbumID,Caption)values(3,''maql4'')
go