号称最基础的一个事务问题!!!!!!!!!!!!!!!!!!!1
create procedure pd_Trans @id int,@name varchar(30)
as
begin transaction BookTrans
insert into books(bookid,bookname) values(@id,@name)
insert into newbook(bookid,bookname) values(@id,@name)
commit transaction BookTrans
//////////////////////////////////////////想在两个表中插入相同数据,第2个插入失败 那么第一个也不能执行 我这么写事务错了吗?
////////////////////这里我在newbook这个表中加个check约束 使不能插入名叫C++
的书:
alter table newbook
add constraint Ck_BookName check(bookname!= 'C++ ')
//执行这个存储过程,为什么显示了newbook插入的数据与check冲突,插入失败,但是为什么book表中还是插入了数据 我的事务为什么没起作用:
execute pd_Trans 1, 'C++ '
------解决方案--------------------create procedure pd_Trans @id int,@name varchar(30)
as
begin transaction BookTrans
insert into books(bookid,bookname) values(@id,@name)
insert into newbook(bookid,bookname) values(@id,@name)
if @@error = 0
commit transaction BookTrans
else
rollback transaction BookTrans
go