Я вставляю (или пытаюсь) одну запись в таблицу, используя следующий триггер:
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.
В этой таблице у меня есть второй триггер, который срабатывает после вставки.
Этот триггер связывает эту запись (Товар) со всеми складами.
Когда я запускаю эти триггеры отдельно, они работают, но не хотят работать вместе.
Я заметил, что Trigger one добавляет более одного элемента, но я не уверен, что именно это и является причиной проблемы.
Контрольный код для второго триггера:
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
Спасибо за вашу помощь.