將顯式值插入表的標識列中

2022-06-29 06:30:08 字數 1744 閱讀 9841

允許將顯式值插入表的標識列中。

set identity_insert [ database_name . [ schema_name ] . ] table

任何時候,乙個會話中只有乙個表的 identity_insert 屬性可以設定為 on。如果某個表已將此屬性設定為 on,則對另乙個表發出 set identity_insert on 語句時,sql server 2005 將返回乙個錯誤資訊,指出 set identity_insert 已設定為 on,並報告已將其屬性設定為 on 的表。

如果插入值大於表的當前標識值,則 sql server 自動將新插入值作為當前標識值使用。

set identity_insert 的設定是在執行或執行時設定的,而不是在分析時設定的。

database_name

指定的表所在的資料庫的名稱。

schema_name

表所屬的架構的名稱。

table

包含標識列的表的名稱。

使用者必須是物件的所有者,或者是 sysadmin 固定伺服器角色的成員,或者是 db_owner 或 db_ddladmin 固定資料庫角色的成員。

以下示例將建立乙個包含標識列的表,並顯示如何使用set identity_insert設定來填充由delete語句導致的標識值中的空隙。

複製**

use adventureworks;

go-- create tool table.

create table dbo.tool(

id int identity not null primary key,

name varchar(40) not null)go

-- inserting values into products table.

insert into dbo.tool(name) values ('screwdriver')

insert into dbo.tool(name) values ('hammer')

insert into dbo.tool(name) values ('saw')

insert into dbo.tool(name) values ('shovel')

go-- create a gap in the identity values.

delete dbo.tool

where name = 'saw'

goselect *

from dbo.tool

go-- try to insert an explicit id value of 3;

-- should return a warning.

insert into dbo.tool (id, name) values (3, 'garden shovel')

go-- set identity_insert to on.

set identity_insert dbo.tool on

go-- try to insert an explicit id value of 3.

insert into dbo.tool (id, name) values (3, 'garden shovel')

goselect *

from dbo.tool

go-- drop products table.

drop table dbo.tool

go

MSSQL 為表中的標識列插入顯式值

今天來介紹一下set identity insert mssql 為表中的標識列插入顯式值 set identity insert tb on set identity insert tb off 其中 set identity insert tb on 是開啟表示插入顯示值 set identit...

將一列值拆分成兩列,並插入到新錶中

已有表tt1,表中資料為 tt1913214718202 747122981926 406016403234 476939375473 256025427480 189140511709 643936412265 509032309425 987106029772 718506902948 表tt2...

Oracle中為表設定自動增長的標識列

建立序列 create sequence 序列名稱 start with 1 起始值 increment by 1 增量 maxvalue 99999999 最大值 nocycle 達到最大值後是否重新計算,當前為不重新計算,cycle為重新計算 nocache 不要快取,容易跳號 建立觸發器 cr...