Link to home
Start Free TrialLog in
Avatar of Tech_Men
Tech_MenFlag for Israel

asked on

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

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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.
Avatar of Tech_Men

ASKER

o.k
can u write me the query that i need to use in SP ?
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

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
Check this.
ROUND(SUM(TMParitSum * TMparitKamot),0)
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

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial