我在 SQL Server 2008 版本中进行了一些初步开发,因为它是测试脚本,所以我在事务中编写了脚本以不影响数据库表。
但似乎记录没有输入数据库,但每次执行脚本时种子值都会增加。
T-SQL 脚本:
Begin Tran
DECLARE @hdoc int
DECLARE @doc VARCHAR(MAX)
SET @doc =
'<?xml version="1.0" encoding="UTF-8"?>
<root>
<places>
<Row>
<Field_0>53.570438</Field_0>
<Field_1>-113.390992</Field_1>
<Field_2>Address details 1</Field_2>
<Field_3>Lab 1</Field_3>
</Row>
<Row>
<Field_0>53.542062</Field_0>
<Field_1>-113.528646</Field_1>
<Field_2>Address details 2</Field_2>
<Field_3>Lab 2</Field_3>
</Row>
</places>
</root>'
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
INSERT INTO dbo.Laboratories
(Name, Latitude, Longitude, [Image], ContentDetails, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy)
SELECT ROW_NUMBER() OVER (ORDER BY Name)
, cast(Latitude as decimal(11,8)), cast(Longitude as decimal(11,8)), null, Content, GETDATE(), 1, null, null
FROM OPENXML (@hdoc, '/root/places/Row', 2)
WITH (
Name nvarchar(100) 'Field_3'
,Latitude varchar(20) 'Field_0'
,Longitude varchar(18) 'Field_1'
,Content nvarchar(100) 'Field_2'
)
EXEC sp_xml_removedocument @hdoc
select * from dbo.Laboratories
rollback tran
我有以下脚本来识别当前的种子值
SELECT
IDENT_SEED(TABLE_NAME) AS Seed,
IDENT_INCR(TABLE_NAME) AS Increment,
IDENT_CURRENT(TABLE_NAME) AS Current_Identity,
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1
AND TABLE_TYPE = 'BASE TABLE'
从这里引用
基本上问题是
- 为什么即使交易回滚,它也会增加身份价值?
- 如何在下次未提交事务时解决此问题以不影响种子值?
PS:我可以重新设置身份,这我知道。