0

我是触发写作的新手。一旦代码/母表中的记录受到影响,我就编写了一个触发器来插入/更新表,即如果我第一次插入记录,它应该将该记录与其他相关新表中的信息。同样适用于更新..下面是我尝试过的代码片段,但它不起作用意味着即使它编译成功..它无法将记录插入目标表..这里 TQA_USER_ASSESSMENT 是核心表,TQA_USER_ASSESSMENT 应该得到根据 TQA_USER_ASSESSMENT 中的数据填充/更新。

               CREATE TRIGGER [dbo].[TRG_RAM_DETAILS_GMAS] ON [dbo].[TQA_USER_ASSESSMENT]           FOR                 INSERT,UPDATE
               AS
              SET XACT_ABORT, NOCOUNT ON

           --Ignore zero row updates, inserts
              IF NOT EXISTS (SELECT * FROM INSERTED) RETURN;

INSERT INTO TQA_RAM_DETAILS_GMAS
SELECT
        tu.name AS User_Name,
        tat.assessmentType AS Assessment_Type,
        tb.name AS Business_Class_Name,
        tr.name AS Region_Name,
        tc.name AS Country_Name,
        tf.facilityName AS Terminal_Name, 
        I.ovr_terminal_rating AS Terminal_Rating,
        CONVERT(VARCHAR(10), I.dateOfInspection, 103) AS Date_of_Inspection, 
        CONVERT(VARCHAR(10), I.dateNextInspectionDue, 103) AS Expiry_date_of_rating,
        CONVERT(VARCHAR(10), I.summ_lastUpdated, 103) AS lastUpdated





FROM
   INSERTED I INNER JOIN
        dbo.TQA_USERS tu ON I.userID=tu.userID INNER JOIN
        dbo.TQA_ASSESSMENT_TYPE AS tat ON tat.assessmentTypeID = I.assessmentTypeID INNER JOIN
        dbo.TQA_FACILITIES AS tf ON tf.facilityID = I.facilityID INNER JOIN
        dbo.TQA_ACCESS ta ON ta.facilityID=tf.facilityID INNER JOIN
        dbo.TQA_BUSINESS_CLASSES tb ON tb.businessClassID= ta.businessClassID INNER JOIN
        dbo.TQA_REGIONS tr ON tr.regionID= ta.regionID INNER JOIN
        dbo.TQA_COUNTRIES tc ON tc.countryID= ta.countryID
4

1 回答 1

0

**

在 sql 中执行此查询并对您的触发器进行故障排除

**

SELECT
        tu.name AS User_Name,
        tat.assessmentType AS Assessment_Type,
        tb.name AS Business_Class_Name,
        tr.name AS Region_Name,
        tc.name AS Country_Name,
        tf.facilityName AS Terminal_Name, 
        myparentTable.ovr_terminal_rating AS Terminal_Rating,
        CONVERT(VARCHAR(10), myparentTable.dateOfInspection, 103) AS Date_of_Inspection, 
        CONVERT(VARCHAR(10), myparentTable.dateNextInspectionDue, 103) AS Expiry_date_of_rating,
        CONVERT(VARCHAR(10), myparentTable.summ_lastUpdated, 103) AS lastUpdated

     from  TQA_USER_ASSESSMENT myParentTable   INNER JOIN
            dbo.TQA_USERS tu ON myParentTable.userID=tu.userID INNER JOIN
            dbo.TQA_ASSESSMENT_TYPE AS tat ON tat.assessmentTypeID = myParentTable.assessmentTypeID INNER JOIN
            dbo.TQA_FACILITIES AS tf ON tf.facilityID = myParentTable.facilityID INNER JOIN
            dbo.TQA_ACCESS ta ON ta.facilityID=tf.facilityID INNER JOIN
            dbo.TQA_BUSINESS_CLASSES tb ON tb.businessClassID= ta.businessClassID INNER JOIN
            dbo.TQA_REGIONS tr ON tr.regionID= ta.regionID INNER JOIN
            dbo.TQA_COUNTRIES tc ON tc.countryID= ta.countryID
        where 
        myParentTable.assessmentTypeID = 'whatever u added'
    and myParentTable.userID='whatever u added'
    and myParentTable.facilityID = 'whatever u added'
于 2013-01-29T07:29:05.513 回答