首页 > 代码库 > SQL SERVER ->> IDENTITY相关函数

SQL SERVER ->> IDENTITY相关函数

IDENTITY函数 -- 只能用在SELECT INTO语句中,用于在插入数据的时候模拟IDENTITY属性的作用生成自增长值。

SELECT IDENTITY(int, 1,1) AS ID_Num  
INTO NewTable  
FROM OldTable;  

 

IDENT_INCR 函数 -- 返回表的自动增长值,比如我们希望每次增长1,那这个函数就会返回1.

USE AdventureWorks2012;  
GO  
SELECT TABLE_SCHEMA, TABLE_NAME,   
   IDENT_INCR(TABLE_SCHEMA + . + TABLE_NAME) AS IDENT_INCR  
FROM INFORMATION_SCHEMA.TABLES  
WHERE IDENT_INCR(TABLE_SCHEMA + . + TABLE_NAME) IS NOT NULL;  

 

IDENT_SEED 函数 -- 自动增长列的初始种子值

USE AdventureWorks2012;  
GO  
SELECT TABLE_SCHEMA, TABLE_NAME,   
   IDENT_SEED(TABLE_SCHEMA + . + TABLE_NAME) AS IDENT_SEED  
FROM INFORMATION_SCHEMA.TABLES  
WHERE IDENT_SEED(TABLE_SCHEMA + . + TABLE_NAME) IS NOT NULL;  
GO  

 

IDENT_CURRENT -- 表的IDENTITY列的当前自动增长值

USE AdventureWorks2012;  
GO  
SELECT TABLE_SCHEMA, TABLE_NAME,   
   IDENT_CURRENT (TABLE_SCHEMA + . + TABLE_NAME) AS IDENT_SEED  
FROM INFORMATION_SCHEMA.TABLES  
WHERE IDENT_CURRENT (TABLE_SCHEMA + . + TABLE_NAME) IS NOT NULL;  
GO  

 

 

SCOPE_IDENTITY() -- 当前模块内(存储过程)生成的最大自动增长值

 

@@IDENTITY -- 当前会话内生成的最大自动增长值

 

SQL SERVER ->> IDENTITY相关函数