Link to home
Start Free TrialLog in
Avatar of monkeybiz12345
monkeybiz12345

asked on

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!
Avatar of DBAduck - Ben Miller
DBAduck - Ben Miller
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland 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