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 8801365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:48:58+00:00 2026-06-14T00:48:58+00:00

I’m trying to create a trigger that will update my Orders Table with the

  • 0

I’m trying to create a trigger that will update my Orders Table with the order information that is found in the OrderDetail table. The detail table can have multiple rows for multiple items in an order. These are linked together by OrderID. What I have so far is:

CREATE TRIGGER AfterOrderDetailInsert ON OrderDetail 
AFTER INSERT AS
UPDATE Orders 
SET OrderTotal = (
    SELECT (
        SELECT Sum(orderdetail.quantity * OrderDetail.Price * 
            ( 1.0 -OrderDetail.ItemDiscount / 100.0 )
        )
        FROM orderdetail 
        WHERE OrderDetail.OrderId = Orders.OrderId
    ) * 
    ( 1 - Orders.OrderDiscount / 100.0 ) * 
    ( 1.0 - Orders.CustomerDiscount / 100.0 )     
    FROM OrderDetail 
    INNER JOIN Orders on OrderDetail.OrderID=Orders.OrderID 
    WHERE OrderDetail.OrderID IN (SELECT OrderID FROM Inserted)
) 

I’m getting an error now saying that the subquery returned more than 1 value. Shouldn’t this be just returning a single total value? I’ve tried adding SUM so that its UPDATE Orders Set OrderTotal = SUM(SELECT (SELECT OrderTotal Stuff)), but that is giving me a syntax error before the first SELECT.

This is on SQL Server 2000.

EDIT: I’ve changed the query to AFTER insert since I want ordertotal to be updated after any insert

2nd Edit: I’m trying to include the inserted meta-table since that should contain the order ID of the latest inserted row and only one row will ever be inserted at a time. Now though I’m getting an error saying “Key column information is insufficient or incorrect. Too many rows were affected by update”. And looking at the order total column, every row in my table gets updated with that order total. So I think I’m close, but I’m still missing something.

  • 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-14T00:49:00+00:00Added an answer on June 14, 2026 at 12:49 am

    Okay, finally got it figured out. The trick ended up using the Inserted table’s Order ID for everywhere OrderID is referenced in the query and for the Update Query so my final query looked like this:

    CREATE TRIGGER AfterOrderDetailInsert ON OrderDetail 
    AFTER INSERT AS
    UPDATE Orders 
    SET OrderTotal = (
    SELECT (
        SELECT Sum(orderdetail.quantity * OrderDetail.Price * 
            ( 1.0 -OrderDetail.ItemDiscount / 100.0 )
        )
        FROM orderdetail 
        WHERE OrderDetail.OrderId = Orders.OrderId AND OrderDetail.OrderID in
       (SELECT TOP 1 OrderID From Inserted)) * 
    ( 1 - Orders.OrderDiscount / 100.0 ) * 
    ( 1.0 - Orders.CustomerDiscount / 100.0 )     
    FROM OrderDetail 
    INNER JOIN Orders on OrderDetail.OrderID=Orders.OrderID 
    INNER JOIN INSERTED on Orders.OrderID=Inserted.OrderID
    ) WHERE Orders.OrderID IN (SELECT TOP 1 OrderID FROM Inserted)
    

    The main difference was I was never setting any condition for the update clause since I was mixing it up with the condition for the SELECT query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.