Hi Guys can you take a look at this SP. When I start with a fresh clean table it seems to work for a little while, But after awhile it gets stuck messed up and dosen't update correctly.
Is there a better way to write this? Thank You
Update SalesPersonelUPCNumber
SET
MoneyOwed = SalesPersonelNewCustomerSalesCount.Level1MoneyEarned +Level2MoneyEarned +Level3MoneyEarned
FROM
SalesPersonelNewCustomerSalesCount
WHERE
SalesPersonelUPCNumber.UserName IN (@.UserName)
Can you tell us what you mean by "it gets stuck messed up and dosen't update correctly"?
Are these columns SalesPersonelNewCustomerSalesCount.Level1MoneyEarned ,Level2MoneyEarned ,Level3MoneyEarned in table SalesPersonelUPCNumber?
Also modify your WHERE clause to
WHERE SalesPersonelUPCNumber.UserName = (@.UserName)
for efficient query plan.
Can you tell us what you mean by "it gets stuck messed up and dosen't update correctly"? --> When I click on the Update Button It ddin't Update values stayed the same.
Are these columns SalesPersonelNewCustomerSalesCount.Level1MoneyEarned ,Level2MoneyEarned ,Level3MoneyEarned in table SalesPersonelUPCNumber? -->Yes These are Columns containing different total amounts
Want all 3 Column Values and Update at (MoneyEarned Column) at Correct UserName SalesPersonelNewCustomerSalesCount.Level1MoneyEarned +Level2MoneyEarned +Level3MoneyEarned in table SalesPersonelUPCNumber
|||
Not knowing exactly how it's table is getting messed up I would first try to rewrite the procedure to something like:
Declare @.MoneyOwed int
Select @.MoneyOwed = Level1MoneyEarned +Level2MoneyEarned +Level3MoneyEarned
FROM SalesPersonelNewCustomerSalesCount
WHERE SalesPersonelUPCNumber.UserName = @.UserName
Update SalesPersonelUPCNumber
SET MoneyOwed = @.MoneyOwned
This would make it more readable (obviously just a style choice) butmay help with the updates as well. Also, are all the Level* columns the same data type? Is it possible that any of the values in the Level columns could be NULL?
|||
Just to add to Andrew's post above, what is the db you are dealing with? SQL Server or Access?
|||Hi Guys,
Thanks Andrew, & Dinakar for you Advice Help... Here's what seems to have have worked so far. I only had to modify it just a little bit.and add aWHERE statement. It seems to be working though. I will test it some more just to be sure.
Dinakar I'm using SQL Dbase. ThankYou Again Guys!!! Cheers!!!
Select @.MoneyOwed = Level1MoneyEarned + Level2MoneyEarned + Level3MoneyEarned
FROM SalesPersonelNewCustomerSalesCount
WHERE SalesPersonelNewCustomerSalesCount.UserName = @.UserName
Update SalesPersonelUPCNumber
SET MoneyOwed = @.MoneyOwed
WHERE
SalesPersonelUPCNumber.UserName IN (@.UserName)
|||
Use the "=" instead of IN. "=" will perform better, it will use an index seek. Also, just to be on safe side you might want to check for NULLs.
Select@.MoneyOwed=Coalesce(Level1MoneyEarned,0)+Coalesce(Level2MoneyEarned,0)+Coalesce(Level3MoneyEarned,0)
FROM SalesPersonelNewCustomerSalesCount
WHERE SalesPersonelNewCustomerSalesCount.UserName=@.UserName
Update SalesPersonelUPCNumber
SET MoneyOwed=@.MoneyOwed
WHERE SalesPersonelUPCNumber.UserName=@.UserName
|||Andrew.Hanson:
Not knowing exactly how it's table is getting messed up I would first try to rewrite the procedure to something like:
Declare @.MoneyOwed int
Select @.MoneyOwed = Level1MoneyEarned +Level2MoneyEarned +Level3MoneyEarned
FROM SalesPersonelNewCustomerSalesCount
WHERE SalesPersonelUPCNumber.UserName = @.UserNameUpdate SalesPersonelUPCNumber
SET MoneyOwed = @.MoneyOwned
This would make it more readable (obviously just a style choice) butmay help with the updates as well. Also, are all the Level* columns the same data type? Is it possible that any of the values in the Level columns could be NULL?
Doing the calculation in two steps (select into holding variable and updating afterwards) is bad form. It is possible, in a multi-user system, for a transaction to be overwritten and therefore lost.
Please list the two tables and the column names in each. Please specify which columns are mandatory and which are optional (i.e., can contain a null value).
Without this information, we are all just guessing.
Thanks Guys,
Seems to be working fine now, If I notice any problems with it I Post the 2 tables but for now it seems to be working. Thanks again Guys!!! Cheers
No comments:
Post a Comment