SQL Server/T-SQL Tutorial/Sequence Indentity/IDENTITYCOL

Материал из SQL эксперт
Перейти к: навигация, поиск

SELECT IDENTITYCOL FROM yourTable

   <source lang="sql">

2> CREATE TABLE customer 3> ( 4> cust_id smallint IDENTITY(100, 20) NOT NULL, 5> cust_name varchar(50) NOT NULL 6> ) 7> GO 1> 2> INSERT customer VALUES ("ACME Widgets") 3> INSERT customer (cust_name) VALUES ("AAA Gadgets") 4> GO (1 rows affected) (1 rows affected) 1> 2> 3> SELECT IDENTITYCOL FROM customer 4> SELECT cust_id FROM customer 5> GO cust_id


   100
   120

(2 rows affected) cust_id


   100
   120

(2 rows affected) 1> 2> drop table customer; 3> GO</source>


Using the IDENTITYCOL Keyword

   <source lang="sql">

24> CREATE TABLE MyTable ( 25> key_col int NOT NULL IDENTITY (1,1), 26> abc char(1) NOT NULL 27> ) 28> INSERT INTO MyTable VALUES ("a") 29> INSERT INTO MyTable VALUES ("b") 30> INSERT INTO MyTable VALUES ("c") 31> 32> 33> SELECT 34> IDENTITYCOL, 35> abc 36> FROM 37> MyTable 38> 39> 40> drop table MyTable 41> GO (1 rows affected) (1 rows affected) (1 rows affected) key_col abc


---
         1 a
         2 b
         3 c

(3 rows affected)</source>