我正在使用以下触发器将一条记录插入(或尝试)到表中:
create trigger kv_trg_AssetAdjustment_AW
on _btblInvoiceLines --select * from _btblInvoiceLines
with encryption
after insert, update
as
if (select trigger_nestlevel(object_id('kv_trg_AssetAdjustment_AW'))) > 1
return
begin
declare @Asset varchar(40)
, @Desc varchar(100)
, @AssetValue float
, @Price float
, @Qty float
, @Code varchar(100)
, @Valid int
, @Exists int
, @SL int
, @NextNum int
, @Variable varchar(100)
, @NewCode varchar(100)
, @Err nvarchar(500)
select @Err = '--------------------------';
select @Err = @Err + @Err + CHAR(10);
select @Err = @Err + CHAR(10);
select @Err = @Err + 'Please specify a hyphen ("-") between Item Code & Description in the Description!';
if exists(select cDescription from inserted where cDescription like '%-%')
select @Valid = 1
else
begin
raiserror(@Err, 16, 1)
return;
end
select
@Asset = ulIDPOrdTxGLVAT
, @Code = LEFT(cDescription, CHARINDEX('-', cDescription) - 1)
, @Desc = REPLACE(SUBSTRING(cDescription, CHARINDEX('-', cDescription), LEN(cDescription)), '-', '')
, @AssetValue = fQuantityLineTotExcl
, @Qty = fQuantity
from inserted
if exists(select Code from StkItem where Code = @Code)
select @Exists = 1
else
select @Exists = 0
select @Variable = (select substring(@Code,1,len(@Code)-1))
select @NextNum = (select max(RIGHT(@Code,1))+1 from StkItem where Code like (@Variable + '%'))
select @NewCode = substring(@Code,1,len(@Code)-1) + cast(@NextNum as varchar)
begin
if (@Asset = 'Asset')
begin
if (@Exists = 0)
begin
insert into StkItem (
Code
, cSimpleCode
, Description_1
, Description_2
, TTI
, TTC
, TTG
, TTR
, WhseItem
, ubIIAsset
)
select @Code
, @Code
, @Desc
, @AssetValue
, 1
, 1
, 1
, 1
, 1
, 1
end
else
begin
insert into StkItem (
Code
, cSimpleCode
, Description_1
, Description_2
, TTI
, TTC
, TTG
, TTR
, WhseItem
, ubIIAsset
)
select @NewCode
, @NewCode
, @Desc
, @AssetValue
, 1
, 1
, 1
, 1
, 1
, 1
end
end
begin
update _btblInvoiceLines
set ufIDPOrdTxGLAssetValue = @AssetValue
from _btblInvoiceLines L
join inserted on L.idInvoiceLines = inserted.idInvoiceLines
end
end
END;
go
我要插入的记录在StkItem中。
在此表上,我还有第二个触发器,该触发器在插入后触发。
此触发器将该记录(库存项目)链接到所有仓库。
当我分别运行这些触发器时,它们可以工作,但是它们不想一起工作。
我确实注意到触发器一添加了多个项,但我不确定这是否是导致问题的原因。
检查第二个触发器的代码:
set ansi_nulls on
go
set quoted_identifier on
go
set nocount on
go
create trigger kv_trg_LinkAssetToAllWhses_AW on StkItem
with encryption
after insert, update
as
if trigger_nestlevel() > 5
return
begin
declare @SL int
, @Asset int
, @Loop int
, @sqlCommand nvarchar(1000)
declare @WHs table
(
ID int identity primary key,
WhseID int
)
insert into @WHs (WhseID)
select WhseLink from WhseMst
order by WhseLink
select @SL = StockLink
, @Asset = ubIIAsset
from inserted
if (@Asset = 1)
begin
select @Loop = min(ID) FROM @WHs
while @Loop IS NOT NULL
begin
set @sqlCommand = 'exec _bspWhUtilLinkStkToWH;1 '+cast(@SL as varchar)+','+cast((select WhseID from @WHs where ID = @Loop) as varchar)+',0,0,1'
exec(@sqlCommand)
select @Loop = min(ID) FROM @WHs where ID>@Loop
end
end
end
go
我已经在互联网上搜索了一个解决方案,但找不到与我的确切问题有关的任何信息。
我设法将问题缩小到第二个触发点。
但是,第二个触发器执行了我没有开发的存储过程。
第二个触发器是否有问题,或者它是存储过程?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[_bspWhUtilLinkStkToWH] @StockID int, @WhseID int, @IsDefaultWH bit, @AllowNegative bit, @iBranchID int = 0
AS
Set nocount on
Begin tran
Declare @SPError int
if (not exists (select * from WhseStk where WHWhseID=@WhseID and WHStockLink=@StockID)) begin
--Add to WhseStk
insert into WhseStk (WHWhseID, WHStockLink, bWHAllowNegStock, WhseStk_iBranchID)
values (@WhseID, @StockID, @AllowNegative, @iBranchID)
set @SPError = @@ERROR
if @SPError <> 0 goto AbortTran
if ((select top 1 bCostPerWarehouse from stdftbl where IsNull(StDfTbl_iBranchID,0)=0) = 1) begin
exec _espPriceUtilGetItemDef @StockId, @WhseID
end
end
goto CommitTran
AbortTran:
rollback tran
RAISERROR (@SPError, 16, 1)
return @SPError
CommitTran:
commit tran
return 0
GO
感谢您的协助。