Queries for UPDLOCK Table Hint


1. Usage of UPDLOCK table hint (to be used in transaction)

SQL Server Query 1

            
 BEGIN TRANSACTION;  
SELECT OrderID, CustomerID
FROM Orders WITH (UPDLOCK, READPAST, ROWLOCK) -- Will try to acquire a U lock
WHERE ShippedDate IS NULL
ORDER BY OrderDate ASC, OrderID ASC;
COMMIT TRANSACTION;

Create SQL query with SqlQueryBuilder 1

            
 var (sql1, parameters1) = new SqlQueryBuilder()  
.Select()
.Columns("OrderID","CustomerID")
.From("Orders", new List<IHint>() { new UPDLOCK(), new READPAST(), new ROWLOCK() })
.Where(new Where(new IS_NULL(new Column("ShippedDate"))))
.OrderBy(new OrderBy().SetColumnAscending("OrderDate")
.SetColumnAscending("OrderID"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT OrderID,
       CustomerID
FROM Orders WITH (UPDLOCK, READPAST, ROWLOCK)
WHERE ShippedDate IS NULL
ORDER BY OrderDate ASC, OrderID ASC;


            
        

Parameters (If used)

Name Value

Query Results 1:

  OrderID CustomerID
1 11008 ERNSH
2 11019 RANCH
3 11039 LINOD
4 11040 GREAL
5 11045 BOTTM
6 11051 LAMAI
7 11054 CACTU
8 11058 BLAUS
9 11059 RICAR
10 11061 GREAL
11 11062 REGGC
12 11065 LILAS
13 11068 QUEEN
14 11070 LEHMS
15 11071 LILAS
16 11072 ERNSH
17 11073 PERIC
18 11074 SIMOB
19 11075 RICSU
20 11076 BONAP
21 11077 RATTC