Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
calvinfoo
Posting Yak Master
129 Posts |
Posted - 2011-02-17 : 05:10:47
|
| How to UPDATE Quantity into the SALES table if OrderID not found else INSERT? Scenario: 1. OrderID is Primary Key2. OrderNum is repeatable[Sales]SalesID | ItemNum | Quantity1 | 10 | 1002 | 20 | 2003 | 30 | 3004 | 40 | 4001. If the User change the ItemNum of SalesID #2 from 20 to a new ItemNum. If the new ItemNum exist, it should combine the Quantity:Update Quantity=Quantity+200 WHERE SalesID = 1 to become:1 | 10 | 300Then DELETE SalesID #2or another way is INSERT INTO SALES () SELECT Sum(Quantity) WHERE ItemNum = 10, then DELETE all the older records WHERE ItemNum = 10But how to Delete rows but must maintain the new recort just inserted?Or is there any good example or suggestion?How to write that SQL? |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-02-17 : 08:53:54
|
| Why delete the record? Just return the sum of the records to your reporting application.http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
calvinfoo
Posting Yak Master
129 Posts |
Posted - 2011-02-17 : 20:25:37
|
| I thought of that too, but the records can be modified anytime, including update the quantity and delete the row, so using sum or view will be very difficult to mananage. example:If now I have two ItemNum 10, and using Sum I will have 200 in Quantity.SalesID| ItemNum| Quantity1 | 10 | 1002 | 10 | 1003 | 20 | 300using Sum on view I will get the following output:ItemNum | Quantity10 | 20020 | 300Which is fine if I wanted the display the record for read only. But my records are updatable by users.Scenario #1:User wanted to Update ItemNum 10 to quantity 150.- I need to minus 50 from either record. Scenario #2:User wanted to Update ItemNum 10 to quantity 50.- I need to minus 100 + 50 from both records.Scenario #3:User change ItemNum 20 to 10.- Now ItemNum 10 will have sum of 500So I think using Sum and View will be not a good idea here... |
 |
|
|
chris_n_osborne
Starting Member
34 Posts |
Posted - 2011-02-17 : 20:59:26
|
| No one query or view can solve this. You need to create a stored procedure. Inside it, you would have T-SQL statements that decided when to UPDATE, INSERT, or DELETE based on parameters passed from the GUI actions taken by the user. I would have a parent stored procedure call a separate stored procedure for each needed DML action. This way, the parent stored procedure does the deciding, and the other stored procedures manipulate the data. For starters, see:http://msdn.microsoft.com/en-us/library/ms187926.aspx |
 |
|
|
calvinfoo
Posting Yak Master
129 Posts |
Posted - 2011-02-17 : 21:16:04
|
| I see. I'll look into this. Thanks for the tips |
 |
|
|
|
|
|
|
|