Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:21:54+00:00 2026-06-18T12:21:54+00:00

This is the database diagram of my sample database: I want to UPDATE Ingredient

  • 0

This is the database diagram of my sample database:

enter image description here

I want to UPDATE Ingredient Table when changes are made into OrderDetails table with the help of Table Trigger

But the problem is that the Ingredients table may have one or more entries for an Item. I want to update all entries in Ingredient table which are associated with an Item. For more description, see the algorithm below:

For each Ingredient in Ingredients for the current Item:
Update the Quantity in Ingredient table using the formula:
formula: Ingredient.Quantity = Ingredient.Quantity – (Item.Quantity * Ingredients.Quantity)

(The whole idea is whenever Item(s) for an OrderID is Added/Updated/Deleted, the Ingredient(s) Quantity should be decreased/increased by the Quantity mentioned)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:21:56+00:00Added an answer on June 18, 2026 at 12:21 pm

    We don’t iterate or loop in SQL (unless we can avoid it). We describe what we want to happen, and let SQL work out how.

    We also, generally, don’t store data that we can compute. We can always compute the amount of each ingredient that has been ordered. (If there’s another table tracking deliveries from our suppliers, that could also be computed over). If performance was an issue, we’d then consider creating an indexed view – which does include the calculated values, but SQL Server takes care of maintaining it automatically.

    In that way, you avoid any discrepancies from creeping in (e.g. if your trigger is disabled).

    All that being said, I think the trigger you want is:

    create trigger T_OrderDetails
    on OrderDetails
    after insert,update,delete
    as
    begin
        update ing
        set Quantity = ing.Quantity - ((COALESCE(iod.Quantity,0) - COALESCE(dod.Quantity,0)) * i.Quantity)
        from
        inserted iod
            full outer join
        deleted dod
            on
                iod.ItemID = dod.ItemID
            inner join
        Ingredients i
            on
                i.ItemID = iod.ItemID or
                i.ItemID = dod.ItemID --Cope with OUTER join above
            inner join
        Ingredient ing
            on
                i.IngID = ing.IngID
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to reverse engineer (import into diagram form) the database definition dump of
Please imagine this small database... Diagram removed dead ImageShack link - volunteer database diagram
I am using Sql Server 2005 This is a part of my database diagram.
I have this database structure, SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `announces` (
enter code here My problem is this: in this database the junction table contains
I am using the database diagram to simply drag one column in a table
I have a database and want to output a UML Diagram. Is there software
Database Diagram I'm trying to create master Info table that has all his fields
During my work on this database application, I've apparently managed to corrupt a form
How do I import this database correctly? https://github.com/samanz/cakecart Every time I import then I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.