create procedure pd_InsertProduct
(
@Name varchar(100),
@Author varchar(30),
@price float,
@DctPrice float,
@PubDate datetime,
@Stock int,
@BusinesserNO varchar(100),
@Discount int,
@Binding varchar(50),
@HevayRecomment varchar(250),
@LanguageType varchar(100),
@Revision int,
@PrintingDate datetime,
@PublishCompany varchar(150),
@Impression int,
@EditorRecommentd varchar(max),
@InstroContent varchar(max),
@Derectory varchar(max),
@MediaDiscuss varchar(max),
@WorderfulContent varchar(max),
@PdTypeID int,
@PicNO varchar(100),
@AttrNameList varchar(250),
@AttrValueList varchar(max)
)
as
declare @id int
begin tran
insert into dbo.Product (Name,Author,Price,DctPrice,PubDate,Stock,ClickNumber,BusinesserNO,Discount,Binding,HevayRecomment,LanguageType,Revision,PrintingDate,PublishCompany,Impression,EditorRecommentd,InstroContent,Derectory,MediaDiscuss,WorderfulContent,PdTypeID,PicNO)
values
(@Name,@Author,@price,@DctPrice,@PubDate,@Stock,default,@BusinesserNO,@Discount,@Binding,@HevayRecomment,@LanguageType,@Revision,@PrintingDate,@PublishCompany,@Impression,@EditorRecommentd,@InstroContent,@Derectory,@MediaDiscuss,@WorderfulContent,@PdTypeID,@PicNO)
select @id=@@IDENTITY
if(@@error!=0)
rollback tran
else
declare @AttrName varchar(250)
declare @AttrValue varchar(250)
declare @AttrNamelog varchar(500)
declare @AttrValuelog varchar(500)
declare @Number int
declare @id2 int
declare @number2 int
set @number=charindex(',',@AttrNameList)
set @AttrName=left(@AttrNameList,@number-1)
set @AttrNamelog=right(@AttrNameList,len(@AttrNameList)-@number)
set @number2=charindex(',',@AttrValueList)
set @AttrValue=left(@AttrValueList,@number2)
set @AttrValuelog=right(@AttrValueList,len(@AttrValueList)-@number2)
while(@number>0)
begin
insert into ProductAtrr(FName,ProductID) values (@AttrName,@id)
set @id2=@@identity
if(@@error!=0)
rollback tran
else
begin
set @number=charindex(',',@AttrNamelog)
set @AttrName=left(@AttrNamelog,@number-1)
set @AttrNamelog=right(@AttrNamelog,len(@AttrNamelog)-@number)
insert into dbo.AtrrVal (Description,AttrID) values (@AttrValue,@id2)
end
if(@@error!=0)
begin
rollback tran
end
else
begin
set @number2=charindex(',',@AttrValuelog)
set @AttrValue=left(@AttrValuelog,@number2)
set @AttrValuelog=right(@AttrValuelog,len(@AttrValuelog)-@number2)
end
if(@@error!=0)
rollback tran
else
commit tran
end
go
实际上tran只有一个,你在上面rollback过了,最后commit当然没有了。每个if都会去判断执行的。rollback不会中断程序。
加return就可以了