我如何从C#运行sqlserver触发器

时间:2017-07-16 00:27:13

标签: c# sql-server

我在单个sqlserver表中有多个触发器,有些通过连接不同的字段来创建产品的描述,一些从另一个表获取数据,一个触发器将产品插入到另一个表中。

我想运行触发器,在C#按钮单击时将数据插入另一个数据库表。

这是我的代码,它将产品插入或设置到另一个表

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 ----------------------------------------------------------------

我想手动或按钮点击事件运行它的主要原因是因为某些原因这个触发器在计算字段和一些触发器之前运行所以我没有得到完整的信息插入到另一个表中插入或更新后第一次。我试过EXEC sp_settriggerorder @triggername=N'[dbo].[WHL-MISYSSETUP]', @order=N'Last', @stmttype=N'INSERT',但这对我没有帮助我得到同样的问题

当新产品创建或更新时,我想从按钮click_event上的C#运行它。任何想法都会赞赏

1 个答案:

答案 0 :(得分:3)

无法调用触发器。在您的情况下,它们应自动触发并针对每一行触发: 插入后,更新。

如果您需要在单击某个按钮后运行此查询,而不是使用触发器,请创建一个新的存储过程。 这里的问题是你必须知道在不使用inserted表的情况下需要更新的[STOCK NO]是什么。