Avatar of phman1275
phman1275

asked on 

SQL Table

Hi, I need some help about trigger and calculation field. I am thinking set a Status field as a calculation field on the Components table. This field will show either "Ready", "Usable", "Not-Ready" depends on the score on the Inspection table. If greater 90 then consider "Ready", below 90 but above 75 then consider "Usable" otherwise "Not-Ready". I think I need the trigger to do that but I have no clue.

Here are the tables:

Components
ComponentID, Version, OwnerID,  Status
1                        A11        100           Ready
2                        B23        200           Usable

Insepctions
ComponentID, Date,            Score
1                        1/3/2016     95
2                        2/13/2017   55
                          2/15/2017   65
                          2/17/2017   80

*** The third inspection result will reflect on Components table with the status "Usable".

Thanks
ComponentsSQL

Avatar of undefined
Last Comment
ste5an
Avatar of ste5an
ste5an
Flag of Germany image

Normally you would not calculate it. Just query it in a view.

What RDBMS do you use? In T-SQL a trigger would look like e.g.

CREATE TRIGGER tr_Inspections_I
ON dbo.Inspections
AFTER INSERT
AS
    SET NOCOUNT ON;

    WITH LastInspections AS
        (
            SELECT  *,
                    ROW_NUMBER() OVER ( PARTITION BY Ins.ComponentID ORDER BY Ins.Date DESC ) AS RN
            FROM    dbo.Inspections Ins
            WHERE   EXISTS ( SELECT * FROM INSERTED I ON I.ComponentID = Ins.ComponentID )
        )
        UPDATE  C
        SET     C.Status = CASE WHEN LI.Score > 90 THEN 'Ready'
                                WHEN LI.Score <= 90 AND LI.Score > 75 THEN 'Ready'
                                ELSE 'Not-Ready'
                            END
        FROM    Components C
            INNER JOIN LastInspections LI ON LI.ComponentID = C.ComponentID
        WHERE   LI.RN = 1
            AND C.Status != CASE WHEN LI.Score > 90 THEN 'Ready'
                                 WHEN LI.Score <= 90 AND LI.Score > 75 THEN 'Ready'
                                 ELSE 'Not-Ready'
                            END;

Open in new window

Avatar of phman1275
phman1275

ASKER

I am using SQL Server. I got cannot add duplicate error because the primary key. The following is my code.

create trigger [dbo].[ComponentInspection]
      ON [dbo].[Components]
      FOR INSERT
AS
BEGIN
      DECLARE @CompID int
      DECLARE @Status varchar(10)
      DECLARE @Score int

      SELECT @CompID = ComponentID FROM Inserted;

      SET @Score = (select max(Score) as Score from Inspections where componentid = @CompID)

      IF (@Score >= 90)
            BEGIN
                  SET @Status = 'Ready'
            END
      ELSE IF (@Score BETWEEN 75 and 89)
            BEGIN
                  SET @Status = 'Usable'
            END
      ELSE
            BEGIN
                  SET @Status = 'Not-Ready'
            END

            INSERT INTO Components
            (ComponentID, ComponentName, Version, Size, Language, Owner, Status)
            SELECT @CompID, ComponentName, Version, Size, Language, Owner, @Status
            FROM inserted
END
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo