Queries for TABLOCKX Table Hint


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

SQL Server Query 1

            
 BEGIN TRANSACTION;  
-- Set Temp table
SELECT 0 AS ProductID, CAST('MyProduct' AS NVARCHAR(50)) AS ProductName, CAST(0.00 AS MONEY) AS UnitPrice
INTO #TempNewProducts
-- Clear Temp Table
DELETE FROM #TempNewProducts WHERE ProductID = 0
-- Perform a bulk insert with TABLOCK.
-- This will acquire an Exclusive (X) table lock on #TempNewProducts.
-- Since it's a new table and no other connections are expected,
-- the lock is easily granted, and the insert can proceed very efficiently.
INSERT INTO #TempNewProducts WITH (TABLOCKX) (ProductID, ProductName, UnitPrice)
SELECT ProductID + 1000, ProductName + ' (New)', UnitPrice * 1.1
FROM Products;
-- In a real scenario, you'd then merge this data into the main table
-- INSERT INTO Products ... SELECT FROM #TempNewProducts;
-- Or UPDATE Products ... FROM #TempNewProducts;
SELECT * FROM #TempNewProducts
DROP TABLE #TempNewProducts;
COMMIT TRANSACTION;

Create SQL query with SqlQueryBuilder 1

            
 var (sql1, parameters1) = new SqlQueryBuilder()  
.Select()
.ColumnWithValue(0, "ProductID")
.Column(new CAST("MyProduct", SqlDataType.NVARCHAR, "50"), "ProductName")
.Column(new CAST(0, SqlDataType.MONEY), "UnitPrice")
.INTO(new Table("#TempNewProducts"))
.DeleteFrom("#TempNewProducts").Where(new Where(new Column("ProductID").Equale(0)))
.InsertInto("#TempNewProducts", new List<IHint>() { new TABLOCKX() },
new List<string>() { "ProductID", "ProductName", "UnitPrice" }, new SqlQueryBuilder()
.Select()
.Column(new ColumnArithmatic("ProductID").ADD(1000))
.Column(new CONCAT(new Column("ProductName"), " (New)"))
.Column(new ColumnArithmatic(new Column("UnitPrice")).MULTIPLY(1.1)))
.From("Products")
.Select().Star().From("#TempNewProducts")
.DropTable(new Table("#TempNewProducts"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT @pMAIN_2507200155145703030 AS ProductID,
       CAST (@pMAIN_2507200155145703031 AS NVARCHAR (50)) AS ProductName,
       CAST (@pMAIN_2507200155145703032 AS MONEY) AS UnitPrice
INTO #TempNewProducts;

DELETE #TempNewProducts
WHERE ProductID = @pMAIN_2507200155145703033;

INSERT INTO #TempNewProducts WITH (TABLOCKX) (ProductID, ProductName, UnitPrice)
SELECT ProductID + @pMAIN_2507200155145703034,
       CONCAT(ProductName, @pMAIN_2507200155145703035),
       UnitPrice * @pMAIN_2507200155145703036
FROM Products;

SELECT *
FROM #TempNewProducts;

DROP TABLE #TempNewProducts;


            
        

Parameters (If used)

Name Value
@pMAIN_2507200155145703030 0
@pMAIN_2507200155145703031 MyProduct
@pMAIN_2507200155145703032 0
@pMAIN_2507200155145703033 0
@pMAIN_2507200155145703034 1000
@pMAIN_2507200155145703035 (New)
@pMAIN_2507200155145703036 1.1

Query Results 1:

  ProductID ProductName UnitPrice
1 1001 Chai (New) 19.8000
2 1002 Chang (New) 20.9000
3 1003 Aniseed Syrup (New) 11.0000
4 1004 Chef Anton's Cajun Seasoning (New) 24.2000
5 1005 Chef Anton's Gumbo Mix (New) 23.4850
6 1006 Grandma's Boysenberry Spread (New) 27.5000
7 1007 Uncle Bob's Organic Dried Pears (New) 33.0000
8 1008 Northwoods Cranberry Sauce (New) 44.0000
9 1009 Mishi Kobe Niku (New) 106.7000
10 1010 Ikura (New) 34.1000
11 1011 Queso Cabrales (New) 23.1000
12 1012 Queso Manchego La Pastora (New) 41.8000
13 1013 Konbu (New) 6.6000
14 1014 Tofu (New) 25.5750
15 1015 Genen Shouyu (New) 17.0500
16 1016 Pavlova (New) 19.1950
17 1017 Alice Mutton (New) 42.9000
18 1018 Carnarvon Tigers (New) 68.7500
19 1019 Teatime Chocolate Biscuits (New) 10.1200
20 1020 Sir Rodney's Marmalade (New) 89.1000
21 1021 Sir Rodney's Scones (New) 11.0000
22 1022 Gustaf's Knäckebröd (New) 23.1000
23 1023 Tunnbröd (New) 9.9000
24 1024 Guaraná Fantástica (New) 4.9500
25 1025 NuNuCa Nuß-Nougat-Creme (New) 15.4000
26 1026 Gumbär Gummibärchen (New) 34.3530
27 1027 Schoggi Schokolade (New) 48.2900
28 1028 Rössle Sauerkraut (New) 50.1600
29 1029 Thüringer Rostbratwurst (New) 136.1690
30 1030 Nord-Ost Matjeshering (New) 28.4790
31 1031 Gorgonzola Telino (New) 13.7500
32 1032 Mascarpone Fabioli (New) 35.2000
33 1033 Geitost (New) 2.7500
34 1034 Sasquatch Ale (New) 15.4000
35 1035 Steeleye Stout (New) 19.8000
36 1036 Inlagd Sill (New) 20.9000
37 1037 Gravad lax (New) 28.6000
38 1038 Côte de Blaye (New) 289.8500
39 1039 Chartreuse verte (New) 19.8000
40 1040 Boston Crab Meat (New) 20.2400
41 1041 Jack's New England Clam Chowder (New) 10.6150
42 1042 Singaporean Hokkien Fried Mee (New) 15.4000
43 1043 Ipoh Coffee (New) 50.6000
44 1044 Gula Malacca (New) 21.3950
45 1045 Rogede sild (New) 10.4500
46 1046 Spegesild (New) 13.2000
47 1047 Zaanse koeken (New) 10.4500
48 1048 Chocolade (New) 14.0250
49 1049 Maxilaku (New) 22.0000
50 1050 Valkoinen suklaa (New) 17.8750
51 1051 Manjimup Dried Apples (New) 58.3000
52 1052 Filo Mix (New) 7.7000
53 1053 Perth Pasties (New) 36.0800
54 1054 Tourtière (New) 8.1950
55 1055 Pâté chinois (New) 26.4000
56 1056 Gnocchi di nonna Alice (New) 41.8000
57 1057 Ravioli Angelo (New) 21.4500
58 1058 Escargots de Bourgogne (New) 14.5750
59 1059 Raclette Courdavault (New) 60.5000
60 1060 Camembert Pierrot (New) 37.4000
61 1061 Sirop d'érable (New) 31.3500
62 1062 Tarte au sucre (New) 54.2300
63 1063 Vegie-spread (New) 48.2900
64 1064 Wimmers gute Semmelknödel (New) 36.5750
65 1065 Louisiana Fiery Hot Pepper Sauce (New) 23.1550
66 1066 Louisiana Hot Spiced Okra (New) 18.7000
67 1067 Laughing Lumberjack Lager (New) 15.4000
68 1068 Scottish Longbreads (New) 13.7500
69 1069 Gudbrandsdalsost (New) 39.6000
70 1070 Outback Lager (New) 16.5000
71 1071 Flotemysost (New) 23.6500
72 1072 Mozzarella di Giovanni (New) 38.2800
73 1073 Röd Kaviar (New) 16.5000
74 1074 Longlife Tofu (New) 11.0000
75 1075 Rhönbräu Klosterbier (New) 8.5250
76 1076 Lakkalikööri (New) 19.8000
77 1077 Original Frankfurter grüne Soße (New) 14.3000