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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
Kevin CrossChief 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.