SQL Server Deadlock Lock chosen for fail

The code below  is part of a SQL Job and periodically fails as the deadlock victim

Is there a way I can stop this?

Perhaps with an "If Exists ...

Or just putting a Print statement after the first update?

I changed this...
--sometimes the amount ends with just a . not .00

UPDATE ewPayments
SET    Amount = REPLACE(Amount, '.', '.00')
WHERE  RIGHT(Amount, 1) = '.';

UPDATE ewPaymentsActivity
SET    Amount = REPLACE(Amount, '.', '.00')
WHERE  RIGHT(Amount, 1) = '.';

Open in new window


To This
--sometimes the amount ends with just a . not .00
-- LB  Job had excessive amount of deadlock fails.
-- Previous code is archived after current

IF EXISTS (   SELECT 1
              FROM   ewPayments WITH ( NOLOCK )
              WHERE  RIGHT(Amount, 1) = '.' )
    BEGIN
        UPDATE ewPayments
        SET    Amount = REPLACE(Amount, '.', '.00')
        WHERE  RIGHT(Amount, 1) = '.';
    END;

IF EXISTS (   SELECT 1
              FROM   ewPaymentsActivity WITH ( NOLOCK )
              WHERE  RIGHT(Amount, 1) = '.' )
    BEGIN
        UPDATE ewPaymentsActivity
        SET    Amount = REPLACE(Amount, '.', '.00')
        WHERE  RIGHT(Amount, 1) = '.';
    END;


/*
UPDATE ewPayments
SET    Amount = REPLACE(Amount, '.', '.00')
WHERE  RIGHT(Amount, 1) = '.';

UPDATE ewPaymentsActivity
SET    Amount = REPLACE(Amount, '.', '.00')
WHERE  RIGHT(Amount, 1) = '.';
*/

Open in new window


Screen Print
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Try to use this code instead:
--sometimes the amount ends with just a . not .00

UPDATE ewPayments
SET    Amount = Amount + '00'
WHERE  Amount LIKE '%.';

UPDATE ewPaymentsActivity
SET    Amount = Amount + '00'
WHERE  Amount LIKE '%.';

Open in new window

NOTE: If possible create and index on AMOUNT column to speed up the update.

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
Larry Bristersr. DeveloperAuthor Commented:
Vitor... help me understand why the % works better than a RIGHT statement?

Not against it at all

Just want to understand why.
_agx_Commented:
Not an answer to your question, but ... does this field store currency? Either

A) changing the data type to currency so formatting is unnecessary OR
B) formatting the value properly, when INSERTed

would avoid the need for the current update, which probably does a table scan, increasing the likelihood of a deadlock.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Larry Bristersr. DeveloperAuthor Commented:
Hey _agx
Yes it does store currency

And I am in the middle of making changes but it is a huge table and will take me a bit to "model"

I just took over and discovered this.
Vitor MontalvãoMSSQL Senior EngineerCommented:
help me understand why the % works better than a RIGHT statement?
When using a function over a table column, you're taking all the ability to the engine to use any index, that's why you should avoid as much of possible the usage of functions in any search criteria.
Larry Bristersr. DeveloperAuthor Commented:
Got it. Thanks.
Valid answer... awarding now
Larry Bristersr. DeveloperAuthor Commented:
Thanks
_agx_Commented:
Okay,  I'm sure you already know that currency type would be better (long term) for a number of reasons (no formatting needed, query performance, etc..) so ultimately that's the option I'd suggest.
Larry Bristersr. DeveloperAuthor Commented:
_ahg... totally agree.

It seems that this table was brought over from MYSQL during a API update and they simply missed this and add bandaids all over the place rather than fix the underlying issue.
_agx_Commented:
missed this and add bandaids all over the place rather than fix the underlying issue.

Yep, been there, done that ;-)
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

From novice to tech pro — start learning today.