我在后台写了一个触发器。 在Sqlserver查询分析其中 可以正常 触发触发器。在前台PB中报错:
No changes made to database.
INSERT INTO bom_mb ( temp_item_id, item_id, item_name, meas_unit, quantity, itemfac ) VALUES ( '04M001', 'Y3.0010', '镜背漆唐山威克', 'P', 1.000000, 'R' )
删除触发器后 就不报错误了、我怀疑是游标部分有点问题吧、同时希望各位指点我优化这个触发器。比如事物和执行效率方面。
CREATE trigger bom_mb_update
on bom_mb for update
as
begin transaction
Declare @temp_item_id VarChar(40),
@new_item_id VarChar(40),
@old_item_id VarChar(40),
@quantity VarChar(40),
@itemfac VarChar(40),
@pare_item_id VarChar(40)
Select @temp_item_id = temp_item_id, @new_item_id=item_id,
@quantity= quantity ,@itemfac= itemfac
from inserted
Select @old_item_id = item_id from deleted
if @itemfac <>'R'
begin
update bom set item_id = @new_item_id, quantity = @quantity
where pare_item_id = @temp_item_id
and item_id = @old_item_id
end
if @itemfac ='R'
--
Declare pare_id_cursor CURSOR for
select item_id from bom
where pare_item_id = @temp_item_id
For UPDATE OF item_id, quantity
Open pare_id_cursor
Fetch next from pare_id_cursor into @pare_item_id
WHILE @@FETCH_STATUS <> -1
begin
if (@@FETCH_STATUS = 0)
begin
print [email protected]_item_id'+ @pare_item_id
update bom set item_id = @new_item_id, quantity = @quantity
where item_id = @old_item_id
and pare_item_id = @pare_item_id
end
Fetch next from pare_id_cursor into @pare_item_id
end
close pare_id_cursor
deallocate pare_id_cursor
if @@error != 0
begin
rollback transaction
return
end
commit transaction
return