如何在调用Function的SQL触发器中添加if else语句

时间:2015-03-03 15:14:27

标签: sql if-statement triggers

我有一个SQL触发器,它调用一个函数来从分隔符中分割文件名。问题是第一个字符串决定了如何拆分数据。我的问题是我如何继续添加这个陈述。

USE [dw1]
GO
/****** Object:  Trigger [dbo].[SplitFileName]    Script Date: 03/02/2015 14:18:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[SplitFileName] 
   ON  [dbo].[DOCUMENTS] 
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    declare @varDWDOCID int, @varFILENAME nvarchar(255), @varDEALER_NUM nvarchar(255), @varINVOICE_NUMBER nvarchar(255), @varWEEK_ENDING nvarchar(255), @varDOCTYPE nvarchar(255)
    Select
      @varDWDOCID = inserted.[DWDOCID],
      @varFILENAME = isnull(inserted.[FILENAME],''),
      @varDEALER_NUM = isnull(inserted.[DEALER_NUM],''),
      @varINVOICE_NUMBER = isnull(inserted.[INVOICE_NUMBER], ''),
      @varWEEK_ENDING = isnull(inserted.[WEEK_ENDING],''),
      @varDOCTYPE = isnull(inserted.[DOCTYPE],'')
      from inserted
-- ============================================= 
-- Author:                            <Name> 
-- Create date: 3/2/2015 
-- Description: Split document based on dash and also calling a function... 
-- ============================================= 

;WITH cte AS
  (  
  SELECT t.dwdocid, t.FILENAME, t.DEALER_NUM, t.INVOICE_NUMBER, t.WEEK_ENDING, t.DOCTYPE, 
         NewDOCTYPE = MAX(CASE WHEN o.level = 1 THEN o.val END) OVER(),
         NewDEALER_NUMBER = MAX(CASE WHEN o.level = 2 THEN o.val END) OVER(),
         NewWEEK_ENDING = MAX(CASE WHEN o.level = 3 THEN o.val END) OVER(),
         NewINVOICE_NUMBER = MAX(CASE WHEN o.level = 4 THEN o.val END) OVER()
  FROM dbo.DOCUMENTS_CHRIS t CROSS APPLY dbo.SplitStrings_CTE(t.FILENAME, '-') o where t.DWDOCID=@varDWDOCID and LEFT(FILENAME, 12) ='123InvoiceIn'
  )
Update cte
SET DOCTYPE=NewDOCTYPE,
DEALER_NUM=NewDEALER_NUMBER,
WEEK_ENDING=NewWEEK_ENDING,
INVOICE_NUMBER=NewWEEK_ENDING
FROM inserted
END
/*
;WITH cte AS
 (    
  SELECT r.dwdocid, r.FILENAME, r.DEALER_NUM, r.INVOICE_NUMBER, r.WEEK_ENDING, r.DOCTYPE, 
         NewDOCTYPE = MAX(CASE WHEN o.level = 1 THEN o.val END) OVER(),
         NewINVOICE_NUMBER = MAX(CASE WHEN o.level = 2 THEN o.val END) OVER()
         NewDEALER_NUM = MAX(CASE WHEN o.level = 3 THEN o.val END) OVER(),
         NewWEEK_ENDING = MAX(CASE WHEN o.level = 3 THEN o.val END) OVER(),
         FROM dbo.DOCUMENTS_CHRIS r CROSS APPLY dbo.SplitStrings_CTE(r.FILENAME, '-') o where t.DWDOCID=@varDWDOCID and LEFT(FILENAME, 13) ='123InvoiceOut'
  )
Update cte
SET DOCTYPE=NewDOCTYPE,
DEALER_NUM=NewDEALER_NUMBER,
WEEK_ENDING=NewWEEK_ENDING,
INVOICE_NUMBER=NewWEEK_ENDING
FROM inserted
END*/

如何在If..Else语句中添加'123InvoiceOut',以便它同时执行?

0 个答案:

没有答案