I've got this doubt about the value provided on an error for the xact_state function. This is my code:
IF OBJECT_ID(N'dbo.Products', N'U') IS NOT NULL
DROP TABLE dbo.Products
CREATE TABLE Products (
ProductID int NOT NULL PRIMARY KEY,
ProductName nvarchar(100) null,
UnitPrice decimal(18,2) not null,
UnitsInStock int not null,
UnitsOnOrder int null,
DateIn datetime
);
GO
create or alter procedure InsertProduct(
@ProductId int,
@ProductName nvarchar(100),
@UnitPrice decimal(18,2),
@UnitsInStock int,
@UnitsOnOrder int
)
as
begin
begin try
begin transaction
insert into Products(ProductID, ProductName, UnitPrice, UnitsInStock, UnitsOnOrder, DateIn)
values(@ProductID, @ProductName, @UnitPrice, @UnitsInStock, @UnitsOnOrder, GETDATE())
commit transaction
end try
begin catch
print N'value of XACT_STATE()' + convert(nvarchar(20),XACT_STATE());
if XACT_STATE() = 1
begin
print N'Rollback Necessary'
rollback transaction
end;
throw 51000, N'There is an error with the application',1
end catch
end
GO
exec InsertProduct @ProductID = 1, @ProductName = 'B', @UnitPrice = 10.0, @UnitsInStock = 2, @UnitsOnOrder = 1
select * from Products
-- Generating an error
exec InsertProduct @ProductID = 1, @ProductName = 'C', @UnitPrice = 10.0, @UnitsInStock = 2, @UnitsOnOrder = 1
select * from Products
In the end I've received this error message:
My question is, why the value for this function is 1 and not -1 given that the second insert is uncommittable for the violation on the primary key definition.