Tuesday, June 23, 2015

Demo to produce identity gap in SQL Server

C:\Users\Administrator>sqlcmd -S  VMMDB01\PROD -W -e -i d:\ident.sql

USE AdventureWorks2012;
IF OBJECT_ID ('dbo.t1', 'U') IS NOT NULL
   DROP TABLE t1;

Changed database context to 'AdventureWorks2012'.
CREATE TABLE t1 (id int IDENTITY(1,1), name char(20))

insert into t1 (name) values ('a')


(1 rows affected)
SELECT @@IDENTITY AS 'Identity'

Identity
--------
1

(1 rows affected)
begin transaction
insert into t1 (name) values ('a');
rollback transaction;


(1 rows affected)
SELECT @@IDENTITY AS 'Identity';

Identity
--------
2

(1 rows affected)
insert into t1 (name) values ('a');


(1 rows affected)
SELECT @@IDENTITY AS 'Identity';

Identity
--------
3

(1 rows affected)
select * from t1;

id name
-- ----
1 a
3 a

(2 rows affected)


/******************************
USE AdventureWorks2012;
IF OBJECT_ID ('dbo.t1', 'U') IS NOT NULL
   DROP TABLE t1;
GO
CREATE TABLE t1 (id int IDENTITY(1,1), name char(20))
GO
insert into t1 (name) values ('a')
GO
SELECT @@IDENTITY AS 'Identity'
GO
begin transaction
insert into t1 (name) values ('a');
rollback transaction;
GO
SELECT @@IDENTITY AS 'Identity';
GO
insert into t1 (name) values ('a');
GO
SELECT @@IDENTITY AS 'Identity';
GO
select * from t1;

GO
******************************/

No comments:

Post a Comment