i need to update more 2 col from the resualt that i get from the query in the father table

hi there i have this code :
after the code is run and the update command succeed
i need to do Sum on the D.TMParitSum in the dbo.TMdetiles WHERE D.TMNum=@TmNum
then after i have the sum of the specific TmNum(ordernum) i need togo to the dbo.TedotMisMain (like orderTable)
and write the sum in the TeodaSum Col then i need to multiply the sum whit the
TmMaamVal col then i need to / 100 the resluat i need to write into the TmMaamMoney then i need to add TeodaSum+TmMaamMoney
and the res i need to write into the TmTotalKolel col
thats all ...
can i combine it to work in the query that i have ?
thanks ....
UPDATE D SET D.TMParitSum=@Price, D.TMafterDiscount = @Price * D.TMparitKamot FROM dbo.TMdetiles D INNER JOIN dbo.TedotMisMain O ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum=@TmNum AND D.TMmkt=@Mkt AND O.TmOpenOrClose=0

Open in new window

Tech_MenAsked:
Who is Participating?
 
Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:
Sorry, forgot your change to TMParitSum * TMparitKamot. :)
CREATE Procedure [dbo].[sp_UpdateTM](
	@TmNum INT
	, @Mkt INT
	, @Price MONEY
) AS
-- changes precision of sum's stored in orders table
DECLARE @numDecimals TINYINT
SET @numDecimals = 2
 
-- update order details table
UPDATE D SET D.TMParitSum=@Price, D.TMafterDiscount = @Price * D.TMparitKamot 
FROM dbo.TMdetiles D INNER JOIN dbo.TedotMisMain O ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum = @TmNum AND D.TMmkt = @Mkt AND O.TmOpenOrClose = 0
 
-- update orders table
UPDATE O
SET O.TeodaSum = ROUND(D.TotalTMParitSum, @numDecimals)
, O.TmMaamMoney = ROUND((D.TotalTMParitSum * O.TmMaamVal * 1.0/100), @numDecimals)
, O.TmTotalKolel = ROUND((D.TotalTMParitSum + (D.TotalTMParitSum * O.TmMaamVal * 1.0/100)), @numDecimals)
FROM dbo.TedotMisMain O 
INNER JOIN (SELECT TMNum, SUM(TMParitSum * TMparitKamot) AS TotalTMParitSum FROM dbo.TMdetiles GROUP BY TMNum) D
ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum = @TmNum

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
No you will have to have a separate update statement since it is for a different table, but you could do this all in on stored procedure to keep it together and use same parameters.
0
 
Tech_MenAuthor Commented:
o.k
can u write me the query that i need to use in SP ?
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
Kevin CrossChief Technology OfficerCommented:
Something like this should work, if I was following your logic correctly.
CREATE Procedure [dbo].[sp_UpdateTM](
	@TmNum INT
	, @Mkt INT
	, @Price MONEY
) AS
 
-- update order details table
UPDATE D SET D.TMParitSum=@Price, D.TMafterDiscount = @Price * D.TMparitKamot 
FROM dbo.TMdetiles D INNER JOIN dbo.TedotMisMain O ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum = @TmNum AND D.TMmkt = @Mkt AND O.TmOpenOrClose = 0
 
-- update orders table
UPDATE O
SET O.TeodaSum = D.TotalTMParitSum
, O.TmMaamMoney = (D.TotalTMParitSum * O.TmMaamVal * 1.0/100)
, O.TmTotalKolel = D.TotalTMParitSum + (D.TotalTMParitSum * O.TmMaamVal * 1.0/100)
FROM dbo.TedotMisMain O 
INNER JOIN (SELECT TMNum, SUM(TMParitSum) AS TotalTMParitSum FROM dbo.TMdetiles GROUP BY TMNum) D
ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum = @TmNum

Open in new window

0
 
Tech_MenAuthor Commented:
i chek it up i added this code :
SUM(TMParitSum * TMparitKamot)
its working very good
i need more 1 thing
i need this code whit round
can u help me whit that ?
i need to round all the sums the write to the db
0
 
SharathData EngineerCommented:
Check this.
ROUND(SUM(TMParitSum * TMparitKamot),0)
0
 
Kevin CrossChief Technology OfficerCommented:
How many decimal places do you need?

i.e. 23.4457
Should this be 23 (0 decimals) or 23.4 (1 decimal) or 23.45 (2 decimals), etc.?

The below example uses 2.  You can just change @numDecimals to be the appropriate number of decimals.
CREATE Procedure [dbo].[sp_UpdateTM](
	@TmNum INT
	, @Mkt INT
	, @Price MONEY
) AS
-- changes precision of sum's stored in orders table
DECLARE @numDecimals TINYINT
SET @numDecimals = 2
 
-- update order details table
UPDATE D SET D.TMParitSum=@Price, D.TMafterDiscount = @Price * D.TMparitKamot 
FROM dbo.TMdetiles D INNER JOIN dbo.TedotMisMain O ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum = @TmNum AND D.TMmkt = @Mkt AND O.TmOpenOrClose = 0
 
-- update orders table
UPDATE O
SET O.TeodaSum = ROUND(D.TotalTMParitSum, @numDecimals)
, O.TmMaamMoney = ROUND((D.TotalTMParitSum * O.TmMaamVal * 1.0/100), @numDecimals)
, O.TmTotalKolel = ROUND((D.TotalTMParitSum + (D.TotalTMParitSum * O.TmMaamVal * 1.0/100)), @numDecimals)
FROM dbo.TedotMisMain O 
INNER JOIN (SELECT TMNum, SUM(TMParitSum) AS TotalTMParitSum FROM dbo.TMdetiles GROUP BY TMNum) D
ON D.TMNum = O.TeodotMisRunId
WHERE D.TMNum = @TmNum

Open in new window

0
All Courses

From novice to tech pro — start learning today.