How to use WITH (NOLOCK) in SQL statements generated by MS-Excel

Greetings Experts!

We've been advised to use the WITH (NOLOCK) syntax when creating ODBC queries to our accounting system using Excel.  When the MS Query Wizard is used to create query, Excel generates the following SQL statement:

SELECT Table1.WH, Table1.BLoc, Table2.Lot, Table1.SKU, Table2.QtyOH, Table3.StockUom, Table2.Note  
FROM SQLDB.dbo.Table3 Table3, SQLDB.dbo.Table1 Table1, SQLDB.dbo.Table2 Table2
WHERE Table3.SKU = Table1.SKU AND Table3.SKU = Table2.SKU AND Table1.WH = Table2.WH 
AND Table1.BLoc = Table2.BLoc AND Table1.SKU = Table2.SKU AND ((Table1.WH='XXXX') AND (Table2.QtyOH>$0))
ORDER BY  Table1.BLoc

Open in new window


I know how it works if I'm using only one table and I know how it works if I'm joining 2 tables and the word JOIN appears in the statement.  I can't figure out where to add the WITH (NOLOCK) syntax here.

Here's my best guess.  Is this correct?

SELECT Table1.WH, Table1.BLoc, Table2.Lot, Table1.SKU, Table2.QtyOH, Table3.StockUom, Table2.Note  
FROM SQLDB.dbo.Table3 Table3 WITH (NOLOCK), SQLDB.dbo.Table1 Table1 WITH (NOLOCK),
 SQLDB.dbo.Table2 Table2 WITH (NOLOCK)
WHERE Table3.SKU = Table1.SKU AND Table3.SKU = Table2.SKU AND Table1.WH = Table2.WH 
AND Table1.BLoc = Table2.BLoc AND Table1.SKU = Table2.SKU AND ((Table1.WH='XXXX') AND (Table2.QtyOH>$0))
ORDER BY  Table1.BLoc

Open in new window


Or can anyone suggest a better statement that includes the required WITH (NOLOCK) syntax that I can use in Excel?

Many thanks!
monkeybiz12345Asked:
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.

DBAduck - Ben MillerPrincipal ConsultantCommented:
Well, first off it is not a good practice to use NOLOCK.  But in this case as you have been instructed to do so, the use of NOLOCK is out of scope for this question, but I had to say that.

For using WITH (NOLOCK) in this case, I would not use comma syntax anyway.
Convert them into JOINs and use them that way.  

Then you can be clear what the query should be.  Don't worry about not using the MSQuery generated query as the JOIN query will be better and more readable.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Here's my best guess.  Is this correct?
Yes. Locking hints must be used after table name so you did it correctly.

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 Excel

From novice to tech pro — start learning today.