1

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

6
  • I'm afraid you can't programmatically run/call a trigger, that doesn't even make sense. Database triggers are ran in response to some command being executed, like inserting, deleting or updating data. If you want the trigger to run AFTER insert,UPDATE, you may need to actually insert and/or update data into your table. What you can do with C# is calling a stored procedure or a function, for example. Commented Jul 16, 2017 at 0:39
  • Thank you @Alisson The main reason for doing this i get the correct value on the second update this trigger run before computed fields and some of the trigger so i dont get complete information. is there any ways to avoid that i tried EXEC sp_settriggerorder @triggername=N'[dbo].[WHL-MISYSSETUP]', @order=N'Last', @stmttype=N'INSERT' but i doesnt help me i get the same problem Commented Jul 16, 2017 at 0:45
  • @Alisson or is there any way on button click set some value to one of the field in the table so i fire the trigger twice so that i get the complete result Commented Jul 16, 2017 at 1:33
  • 1
    If you've reached the point where you need to manually call a trigger, or run triggers in a certain order, that's definitely an indicator of bad design. Even if there was a way to do this, you are going down a path that's going to take more and more effort and have more and more bugs. You need to consider rewriting this properly. For example you should write one stored procedure that does everything, or you should reconsider the design of your tables so that you don't need to store duplicate information. Commented Jul 16, 2017 at 1:42
  • @Nick.McDermaid i really like the inserted table in trigger do i have that in SP? Commented Jul 16, 2017 at 2:10

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Kim Lage The main reason i want to run it manually 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 after update or insert for the first time. 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
Having many triggers for a table is not a very good design as you could have many performance issues. Anyway you could try making a big trigger with all the operations you need in order. I don't think its the best solution for your case but it might just work...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.