日期:2014-05-16 浏览次数:20538 次
declare @xml XML,@idoc INT
set @xml=N'<root>
<Test>
<ID>0</ID>
<Name>張三</Name>
</Test>
<Test>
<ID>1</ID>
<Name>李四</Name>
</Test>
<Test>
<ID>2</ID>
<Name>王五</Name>
</Test>
</root>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @xml
BEGIN TRY
BEGIN TRAN
--SELECT @xml.value('(root/Test/ID/text())[1]','TINYINT')
SELECT
*
FROM OPENXML(@idoc,'root/Test/ID',3)
WITH
([ID] TINYINT '.[1]')
COMMIT TRAN
EXEC sp_xml_removedocument @idoc
END TRY
BEGIN CATCH
DECLARE @ERROR VARCHAR(MAX)
SELECT @Error=ERROR_MESSAGE()
RAISERROR 50001 @Error
ROLLBACK TRAN
END CATCH
/*ID
----
0
1
2
(3 行受影响)*/
create table ttt(id int primary key ,v varchar(10))
go
--只要报错,就回滚
set xact_abort on
begin tran
insert into ttt
values(1,'a')
insert into ttt
values(2,'xxxxxxxxxxxxxxxx')
insert into ttt
values(3,'c')
commit
/*
(1 行受影响)
消息 8152,级别 16,状态 14,第 5 行
将截断字符串或二进制数据。
*/
--由于上面报错,所以一条都没有插入
select * from ttt