I am starting a transaction and executing multiple truncate table statements but if last fails then it rolls back all truncate. I am trying to depict the same behavior on SQL Server command prompt.
I am trying to execute following SQL statements in SQL Server 2012:
create table pkt1(id integer)
GO
create table pkt2(id integer)
GO
insert into pkt1 values(10)
insert into pkt1 values(20)
insert into pkt2 values(10)
insert into pkt2 values(20)
select * from pkt1
Output:
id
------
10
20
select * from pkt2
Result:
id
------
10
20
begin transaction
truncate table pkt1;
truncate table pkt2;
truncate table pkt3;
commit;
Error:
Msg 4701, Level 16, State 1, Server PNI6W11198, Line 4
Cannot find the object "pkt3" because it does not exist or you do not have permissions.
select * from pkt2
Output:
id
------
10
20
select * from pkt1
Output:
id
-----
10
20
TRUNCATE
is supposed to be Transact SQL and should commit earlier truncate for pkt1 and pkt2. But it does rollback for all.
Is this expected behavior?