SQL Server触发器标头表和详细信息

时间:2019-03-11 10:18:18

标签: sql sql-server triggers database-trigger

我的应用程序使用分布式数据库,该数据库用于另一家商店,而一个主数据库则包含所有数据。 一些商店充当客户,其他商店充当供应商。 当在一个商店中创建供应商订单并且数据到达主数据库时,我希望主数据库创建一个CustomerOrder。 我发现的方法是使用触发器。因此,我想创建一个触发器,当我下达一个供应商订单(带有标题-COM_SUPPLIER_ORDER和产品-COM_SUPPLIER_ORDER_PRODUCTS)时,该触发器将使用SupplierOrder数据创建CustomerOrder。基本上,我希望同一个触发器将数据插入2个表(COM_SUPPLIER_ORDER和COM_SUPPLIER_ORDER_PRODUCTS)的2个表(COM_CUSTOMER_ORDER和COM_CUSTOMER_ORDER_PRODUCTS)中。 到目前为止,我创建的内容适用于标题,但不幸的是,它不适用于产品。 我想问的是这是否正确?我做错了什么?还有另一种方法吗?

CREATE TRIGGER dbo.SACWEBTG_STYLUSCUSTOMERORDERS_TO_STORESSUPPLIERORDER 
   ON  dbo.COM_SUPPLIER_ORDER 
   FOR INSERT
AS 
    DECLARE @StylusNode INT
    DECLARE @LastCustomerOrderID INT
    DECLARE @LastCustomerOrderProductID INT
    DECLARE @SupplierOrderID INT
    DECLARE @CustomerOrderID INT
    DECLARE @CustomerID INT
    DECLARE @SourceBranchID INT

    SET @StylusNode = 210
    EXEC @LastCustomerOrderID = dbo.SACWEBSP_NEXTSEQUENCEID @StylusNode, 'CUSORD';
    EXEC @LastCustomerOrderProductID = dbo.SACWEBSP_NEXTSEQUENCEID @StylusNode, 'CUSORDPRD';
    SELECT @SupplierOrderID = ins.SUPPLIER_ORDER_ID FROM INSERTED ins;
    SELECT @SourceBranchID = ins.SOURCE_BRANCH_ID FROM INSERTED ins;
    SET @CustomerOrderID = CAST(CONCAT(CAST(@StylusNode AS VARCHAR(3)), CAST((@LastCustomerOrderID + 1) AS VARCHAR(10))) AS INT)
    SET @CustomerID = (SELECT CUSTOMER_ID FROM SPC_RELATION_BRANCH_CUSTOMER WHERE BRANCH_ID = @SourceBranchID);


BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here
     /* START: FOR HEADER */
    INSERT INTO COM_CUSTOMER_ORDER ([CUSTOMER_ORDER_ID]
          ,[DOCUMENT_ID]
          ,[SOURCE_BRANCH_ID]
          ,[BRANCH_ID]
          ,[WAREHOUSE_ID]
          ,[CUSTOMER_ID]
          ,[ENTITY_ADDRESS_ID]
          ,[DOCUMENT_ORIGIN_ID]
          ,[ORIGIN_ID]
          ,[CUSTOMER_RECEIVE_ID]
          ,[CUSTOMER_CONTACT_ID]
          ,[CUSTOMER_CONTRACT_ID]
          ,[CURRENCY_ID]
          ,[PRICE_CATALOG_ID]
          ,[PAYMENT_TERMS_ID]
          ,[ATTACHED_CATALOG_ID]
          ,[DOCUMENT_REFERENCE]
          ,[DOCUMENT_NUMBER]
          ,[DOCUMENT_DATE]
          ,[DOCUMENT_STATUS]
          ,[DOCUMENT_RATE]
          ,[DOCUMENT_TOTAL_GROSS]
          ,[DOCUMENT_TOTAL_DEDUCTION]
          ,[DOCUMENT_TOTAL_DISCOUNT]
          ,[DOCUMENT_TOTAL_NET]
          ,[CUSTOMER_REQUEST_REFERENCE]
          ,[SPECIAL_ORDER_REFERENCE]
          ,[ORDER_TYPE]
          ,[DELIVERY_DEADLINE]
          ,[DELIVERY_DEADLINE_MANDATORY]
          ,[NOTES_NATIVE]
          ,[NOTES_OTHER]
          ,[NOTES_PUBLIC_NATIVE]
          ,[NOTES_PUBLIC_OTHER]
          ,[CAN_INVOICE_NEXT]
          ,[ACTIVE]
          ,[CREATED_USER]
          ,[CREATED_DATE]
          ,[UPDATED_USER]
          ,[UPDATED_DATE]
          ,[DELETED]
          ,[SYSTEM_RECORD])
    SELECT 
           @CustomerOrderID
          ,10019                                    /* [DOCUMENT_ID] */
          ,SOURCE_BRANCH_ID                         /* [SOURCE_BRANCH_ID] */
          ,1008                                     /* [BRANCH_ID] */
          ,[WAREHOUSE_ID]                   /* ???? */
          ,@CustomerID                              /* [CUSTOMER_ID] */
          ,null                                     /* [ENTITY_ADDRESS_ID] */
          ,10010                                    /* [DOCUMENT_ORIGIN_ID] */
          ,SUPPLIER_ORDER_ID                        /* [ORIGIN_ID] */
          ,null                                     /* [CUSTOMER_RECEIVE_ID] */
          ,null                                     /* [CUSTOMER_CONTACT_ID] */
          ,null                                     /* [CUSTOMER_CONTRACT_ID] */
          ,[CURRENCY_ID]
          ,1001                                     /* [PRICE_CATALOG_ID] ?????? */
          ,[PAYMENT_TERMS_ID]
          ,null                                     /* [ATTACHED_CATALOG_ID] */
          ,CAST(CONCAT('UUEC-', (SELECT dbo.SACWEBFNC_LASTDOCUMENTNUMBER(1008, 10019, 'CUSORD', YEAR(DOCUMENT_DATE)) + 1), '-', CAST(YEAR(DOCUMENT_DATE) AS VARCHAR(4))) AS VARCHAR(50)) /* [DOCUMENT_REFERENCE] */
          ,((SELECT dbo.SACWEBFNC_LASTDOCUMENTNUMBER(1008, 10019, 'CUSORD', YEAR(DOCUMENT_DATE)) AS LDN) + 1) /* [DOCUMENT_NUMBER] */
          ,GETDATE()
          ,1                                        /* [DOCUMENT_STATUS] */
          ,[DOCUMENT_RATE]
          ,[DOCUMENT_TOTAL_GROSS]
          ,[DOCUMENT_TOTAL_DEDUCTION]
          ,[DOCUMENT_TOTAL_DISCOUNT]
          ,[DOCUMENT_TOTAL_NET]
          ,null                                     /* [CUSTOMER_REQUEST_REFERENCE] */
          ,[SPECIAL_ORDER_REFERENCE]
          ,[ORDER_TYPE]
          ,[DELIVERY_DEADLINE]
          ,[DELIVERY_DEADLINE_MANDATORY]
          ,[NOTES_NATIVE]
          ,[NOTES_OTHER]
          ,[NOTES_PUBLIC_NATIVE]
          ,[NOTES_PUBLIC_OTHER]
          ,1                                        /* [CAN_INVOICE_NEXT] */
          ,[ACTIVE]
          ,[CREATED_USER]
          ,GETDATE()
          ,[UPDATED_USER]
          ,GETDATE()
          ,[DELETED]
          ,[SYSTEM_RECORD]
     FROM INSERTED
     /* END: FOR HEADER */

     /* START: FOR PRODUCTS */
    INSERT INTO COM_CUSTOMER_ORDER_PRODUCT ([CUSTOMER_ORDER_PRODUCT_ID]
          ,[CUSTOMER_ORDER_ID]
          ,[WAREHOUSE_ID]
          ,[PRODUCT_ID]
          ,[SUPPLIER_PROPOSAL_ID]
          ,[PRICE]
          ,[STOCK_RESERVE]
          ,[QUANTITY]
          ,[QUANTITY_DELIVERED]
          ,[SALES_CAMPAIGN_ID]
          ,[DISCOUNT_HEADER]
          ,[DISCOUNT]
          ,[DEDUCTION]
          ,[ISBUNDLE]
          ,[FREE_COMMENT]
          ,[STATUS]
          ,[HP_BUNDLE_ID]
          ,[ORDER]
          ,[ACTIVE]
          ,[CREATED_USER]
          ,[CREATED_DATE]
          ,[UPDATED_USER]
          ,[UPDATED_DATE]
          ,[DELETED]
          ,[SYSTEM_RECORD])
    SELECT 
           CAST(CONCAT(CAST(@StylusNode AS VARCHAR(3)), CAST((@LastCustomerOrderProductID + ROW_NUMBER() OVER (ORDER BY [ORDER])) AS VARCHAR(10))) AS INT)
          ,@CustomerOrderID
          ,21011                                    /* [WAREHOUSE_ID] -> Armazem 22 ?????? */
          ,[PRODUCT_ID]
          ,null                                     /* [SUPPLIER_PROPOSAL_ID] */
          ,[PRICE]
          ,0                                        /* [STOCK_RESERVE] */
          ,[QUANTITY]
          ,0                                        /* [QUANTITY_DELIVERED] */
          ,null                                     /* [SALES_CAMPAIGN_ID] */
          ,[DISCOUNT_HEADER]
          ,[DISCOUNT_1]                             /* [DISCOUNT] */
          ,[DEDUCTION]
          ,0                                        /* [ISBUNDLE] ?????? */
          ,[FREE_COMMENT]
          ,1                                        /* [STATUS] -> Open = 1*/
          ,[HP_BUNDLE_ID]
          ,[ORDER]
          ,[ACTIVE]
          ,[CREATED_USER]
          ,[CREATED_DATE]
          ,[UPDATED_USER]
          ,[UPDATED_DATE]
          ,[DELETED]
          ,[SYSTEM_RECORD]
     FROM COM_SUPPLIER_ORDER_PRODUCT 
     WHERE SUPPLIER_ORDER_PRODUCT_ID = @SupplierOrderID
     /* END: FOR PRODUCTS */

END
GO

任何帮助将不胜感激。

0 个答案:

没有答案