Reputation: 61
I have multiple trigger in a single sqlserver table some create description of a product by concatenating different fields, some get data from another table and a trigger which insert a product to another table.
I want to run the trigger which insert's data to another database table on button click from C#.
here is my code which inserts or setup product into another table
Create TRIGGER [dbo].[WHL-MISYSSETUP]
ON [dbo].[WHEELS]
AFTER insert,UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
---------------------------------------------------// INSERT PRODUCT INFO TO MASTER TABLE ----------------------------------------------------------------
SET ANSI_WARNINGS OFF;
BEGIN
IF NOT EXISTS (SELECT * FROM [MITESTCO].dbo.[MIITEM]
WHERE [MITESTCO].dbo.[MIITEM].itemId IN (select [STOCK NO] from inserted) and [MITESTCO].dbo.[MIITEM].descr IN (select [PURCHASE DESCRIPTION] from inserted))
BEGIN
INSERT INTO [MITESTCO].dbo.MIITEM
([itemId], [descr],[xdesc],[sales] ,[uOfM] ,[poUOfM] ,[uConvFact],[ref],[type],[status])--,[unitWgt]
SELECT [STOCK NO], [PURCHASE DESCRIPTION2], [SALES DESCRIPTION2], [STOCK NO] ,'EA' ,'EA' ,'1',[WORK INSTRUCTION-WHL], '2','0'--,[APPROX. WGT.]
FROM [inserted]
WHERE [STOCK NO] NOT IN (SELECT [itemId] FROM [MITESTCO].dbo.[MIITEM] WHERE itemId NOT LIKE '*-CI')
AND [MAKE / BUY]='Make';
END
END
SET ANSI_WARNINGS ON;
SET ANSI_WARNINGS off;
BEGIN
IF NOT EXISTS (SELECT * FROM [MITESTCO].dbo.[MIITEM]
WHERE [MITESTCO].dbo.[MIITEM].itemId IN (select [STOCK NO] from inserted) and [MITESTCO].dbo.[MIITEM].descr IN (select [PURCHASE DESCRIPTION] from inserted))
BEGIN
INSERT INTO [MITESTCO].dbo.MIITEM
([itemId], [descr],[xdesc],[sales] ,[uOfM] ,[poUOfM] ,[uConvFact],[ref],[type],[status])--,[unitWgt]
SELECT [STOCK NO], [PURCHASE DESCRIPTION2], [SALES DESCRIPTION2], [STOCK NO] ,'EA' ,'EA' ,'1',[WORK INSTRUCTION-WHL], '2','0'--,[APPROX. WGT.]
FROM [inserted]
WHERE [STOCK NO] NOT IN (SELECT [itemId] FROM [MITESTCO].dbo.[MIITEM] WHERE itemId NOT LIKE '*-CI')
AND [MAKE / BUY]='BUY';
END
END
SET ANSI_WARNINGS on;
---------------------------------------------------// INSERT PRODUCT INFO TO BOM HEADER TABLE ----------------------------------------------------------------
SET ANSI_WARNINGS OFF;
DECLARE @d DATETIME = GETDATE();
INSERT INTO [MITESTCO].[dbo].[MIBOMH]
([bomItem], [bomRev], [rollup], [mult], [autoBuild], [assyLead],[revCmnt],[author],[descr],[qPerLead],[lstMainDt],[revDate],[effStartDate],[ovride] )
-- DECLARE @d DATETIME = GETDATE();
SELECT [STOCK NO], [bomRev], '1', '1', '1', '3','SYNC FROM TV','username','WHL FROM PDM','0', FORMAT(@d, 'yyyy-MM-dd HH\:mm\:ss\.fff', 'en-US') AS 'Format#1',FORMAT(@d, 'yyyyMMdd' , 'en-US') AS 'Format#2',FORMAT(@d, 'yyyyMMdd' , 'en-US') AS 'Format#2','0'
FROM [INSERTED]
WHERE [STOCK NO] NOT IN (SELECT [MITESTCO].[dbo].[MIBOMH].[bomItem] FROM [MITESTCO].[dbo].[MIBOMH] where bomRev != [bomRev])
AND [STOCK NO] IN (SELECT [MITESTCO].[dbo].[MIITEM].[ItemId] FROM [MITESTCO].[dbo].[MIITEM] where type='2');
SET ANSI_WARNINGS ON;
---------------------------------------------------// INSERT PRODUCT INFO TO BOM DETAIL TABLE ----------------------------------------------------------------
SET ANSI_WARNINGS OFF;
;with cte as (
select
[STOCK NO]
, u.rev
, bomEntry = row_number() over (order by u.ordinal)
, u.Partid
, u.Qty--='1'
, cmnt = ''
, srcLoc = 'DS'
, dType = '0'
, lead = '0'
, lineNbr = row_number() over (order by u.ordinal)
--, bomRev
from [inserted]
cross apply (values
('1',[bomRev],1,[BOM-WHEEL PN])
,('1',[bomRev],2,[BOM - RIM PN])
,('1',[bomRev],3,[BOM - SECONDARY DISC PN])
,('1',[bomRev],4,[BOM - FIN DISC PN])
,('1',[bomRev],5,[BOM - FLAT FIN DISC PN])
,([WHL BOM QTY 1],[bomRev],6,[WHL BOM PART 1 PN])
,([WHL BOM QTY 2],[bomRev],7,[WHL BOM PART 2 PN])
,([WHL BOM QTY 3],[bomRev],8,[WHL BOM PART 3 PN])
,([WHL BOM QTY 4],[bomRev],9,[WHL BOM PART 4 PN])
,([WHL BOM QTY 5],[bomRev],10,[WHL BOM PART 5 PN])
,('1',[bomRev],11,[COLOR-PN])
) u (Qty,rev, ordinal, partId)
where nullif(u.partId, '') is not null
)
INSERT INTO [MITESTCO].dbo.[MIBOMD]
([bomItem], [bomRev], [bomEntry], [partId], [qty],[cmnt],[srcLoc],[dType],[lead],[lineNbr])
select
cte.[STOCK NO]
, cte.rev
, cte.bomEntry
, cte.Partid
, cte.Qty
, cte.cmnt
, cte.srcLoc
, cte.dType
, cte.lead
, cte.lineNbr
from cte
where not exists (
select 1
from [MITESTCO].dbo.[MIBOMD] w
where w.[bomItem] = cte.[STOCK NO]
and w.[bomRev] = cte.rev
and w.[bomEntry]= cte.bomEntry
);
SET ANSI_WARNINGS ON;
---------------------------------------------------// end Creates BOM STRUCTURE ----------------------------------------------------------------
The main reason that i want to run it manually or on button click event is because for some reason this trigger run before computed fields and some of the triggers so i don't get complete information to insert to the other table for the first time both after insert or update. i tried EXEC sp_settriggerorder @triggername=N'[dbo].[WHL-MISYSSETUP]', @order=N'Last', @stmttype=N'INSERT'
but that doesnt help me i get the same problem
when new product created or updated i want to run this from C# on button click_event. Any idea will appreciated
Upvotes: 1
Views: 608
Reputation: 117
Triggers can't be called. They should be triggered automatically and for every row, in your case: AFTER insert,UPDATE.
If you need to run this query after clicking some button what I suggest is instead of using a trigger, create a new stored procedure. The problem here is that you will have to know what are the [STOCK NO] you need to update without using the inserted table.
Upvotes: 3