CHECKSUM SQL function
1. Usage of CHECKSUM using integer value
SQL Server Query 1
SELECT
CHECKSUM(100) AS TestCheckSum; -- Returns an integer checksum value
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM(100), "TestCheckSum")
.Build();
Query build by SqlQueryBuilder 1
SELECT CHECKSUM(@pMAIN_2507200147312989380) AS TestCheckSum;
Parameters (If used)
Name |
Value |
@pMAIN_2507200147312989380 |
100 |
Query Results 1:
2. Usage of CHECKSUM using string value
SQL Server Query 2
SELECT
CHECKSUM('Hello') AS TestCheckSum; -- Returns an integer checksum value
Create SQL query with SqlQueryBuilder 2
var (sql2, parameters2) = new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM("Hello"), "TestCheckSum")
.Build();
Query build by SqlQueryBuilder 2
SELECT CHECKSUM(@pMAIN_2507200147313010520) AS TestCheckSum;
Parameters (If used)
Name |
Value |
@pMAIN_2507200147313010520 |
Hello |
Query Results 2:
3. Usage of CHECKSUM using multiple values
SQL Server Query 3
SELECT
CHECKSUM(10, 'Test', 10.5) AS TestCheckSum;
Create SQL query with SqlQueryBuilder 3
var (sql3, parameters3) = new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM(10, "Test", 10.5), "TestCheckSum")
.Build();
Query build by SqlQueryBuilder 3
SELECT CHECKSUM(@pMAIN_2507200147313021280, @pMAIN_2507200147313021281, @pMAIN_2507200147313021282) AS TestCheckSum;
Parameters (If used)
Name |
Value |
@pMAIN_2507200147313021280 |
10 |
@pMAIN_2507200147313021281 |
Test |
@pMAIN_2507200147313021282 |
10.5 |
Query Results 3:
|
TestCheckSum |
1 |
1468046841
|
4. Usage of CHECKSUM using different data types
SQL Server Query 4
SELECT
CHECKSUM(1, 'abc', GETDATE(), 123.45) AS TestCheckSum;
Create SQL query with SqlQueryBuilder 4
var (sql4, parameters4) = new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM(new CHECKSUM(1, "abc", new GETDATE(), 10.5), "TestCheckSum")
.Build();
Query build by SqlQueryBuilder 4
SELECT CHECKSUM(@pMAIN_2507200147313032140, @pMAIN_2507200147313032141, GETDATE(), @pMAIN_2507200147313032142) AS TestCheckSum;
Parameters (If used)
Name |
Value |
@pMAIN_2507200147313032140 |
1 |
@pMAIN_2507200147313032141 |
abc |
@pMAIN_2507200147313032142 |
10.5 |
Query Results 4:
|
TestCheckSum |
1 |
-1049273437
|
5. Usage of CHECKSUM to demonstrate that orders matter
SQL Server Query 5
SELECT CHECKSUM('abc', 'def') AS TestCheckSum
UNION
SELECT CHECKSUM('def', 'abc') AS TestCheckSum; -- Different order, different checksum
Create SQL query with SqlQueryBuilder 5
var (sql5, parameters5) = new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM("abc", "def"), "TestCheckSum")
.Union(new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM("def", "abc"), "TestCheckSum")
)
.Build();
Query build by SqlQueryBuilder 5
SELECT CHECKSUM(@pMAIN_2507200147313041510, @pMAIN_2507200147313041511) AS TestCheckSum
UNION
SELECT CHECKSUM(@pMAIN_2507200147313041512, @pMAIN_2507200147313041513) AS TestCheckSum;
Parameters (If used)
Name |
Value |
@pMAIN_2507200147313041510 |
abc |
@pMAIN_2507200147313041511 |
def |
@pMAIN_2507200147313041512 |
def |
@pMAIN_2507200147313041513 |
abc |
Query Results 5:
|
TestCheckSum |
1 |
2072951903
|
2 |
2078850636
|
6. Usage of CHECKSUM for a column in products table
SQL Server Query 6
SELECT CHECKSUM(ProductName) AS ProductNameCheckSum FROM Products;
Create SQL query with SqlQueryBuilder 6
var (sql6, parameters6) = new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM(new Column("ProductName")), "ProductNameCheckSum")
.From("Products")
.Build();
Query build by SqlQueryBuilder 6
SELECT CHECKSUM(ProductName) AS ProductNameCheckSum
FROM Products;
Parameters (If used)
Query Results 6:
|
ProductNameCheckSum |
1 |
-1752486018
|
2 |
911420344
|
3 |
-1233618777
|
4 |
-280163406
|
5 |
-1763189045
|
6 |
1384010367
|
7 |
529833873
|
8 |
146082270
|
9 |
-1712497368
|
10 |
1499143079
|
11 |
2109696480
|
12 |
-1988692044
|
13 |
-1533169257
|
14 |
833240527
|
15 |
251631552
|
16 |
-1112802193
|
17 |
-1614878110
|
18 |
56161483
|
19 |
-2082772544
|
20 |
194648455
|
21 |
-1683332010
|
22 |
-379964905
|
23 |
204776320
|
24 |
-1141032727
|
25 |
1934947976
|
26 |
426044970
|
27 |
-1614553156
|
28 |
300330007
|
29 |
1231962368
|
30 |
702062079
|
31 |
1609572245
|
32 |
-1970137337
|
33 |
-1442774311
|
34 |
-665066830
|
35 |
-1113173263
|
36 |
-1596411336
|
37 |
851159640
|
38 |
1912991253
|
39 |
276294134
|
40 |
-2083465718
|
41 |
-1162283934
|
42 |
-1280014247
|
43 |
330088718
|
44 |
-130274435
|
45 |
324504421
|
46 |
-929726556
|
47 |
946167026
|
48 |
2033975146
|
49 |
-961070451
|
50 |
227916359
|
51 |
1020450920
|
52 |
580547765
|
53 |
-412874150
|
54 |
934022397
|
55 |
2137156155
|
56 |
-996766089
|
57 |
1213212237
|
58 |
-835380344
|
59 |
1162035158
|
60 |
-1822277790
|
61 |
-1950938790
|
62 |
-1555879841
|
63 |
354939606
|
64 |
1238240572
|
65 |
-1252441203
|
66 |
-1752605578
|
67 |
2110868025
|
68 |
1420418230
|
69 |
-2008710191
|
70 |
2138762109
|
71 |
1003325744
|
72 |
1422928600
|
73 |
175741067
|
74 |
935628581
|
75 |
-405067053
|
76 |
-1369847996
|
77 |
-567575104
|
7. Usage of CHECKSUM for a column in customers table
SQL Server Query 7
SELECT
CustomerID,
ContactName,
CHECKSUM(CustomerID, ContactName) AS CustomerInfoChecksum
FROM Customers;
Create SQL query with SqlQueryBuilder 7
var (sql7, parameters7) = new SqlQueryBuilder()
.Select()
.Columns("CustomerID","ContactName")
.Column(new CHECKSUM(new Column("CustomerID"), new Column("ContactName")), "CustomerInfoChecksum")
.From("Customers")
.Build();
Query build by SqlQueryBuilder 7
SELECT CustomerID,
ContactName,
CHECKSUM(CustomerID, ContactName) AS CustomerInfoChecksum
FROM Customers;
Parameters (If used)
Query Results 7:
|
CustomerID |
ContactName |
CustomerInfoChecksum |
1 |
ALFKI
|
Maria Anders
|
-1408183751
|
2 |
ANATR
|
Ana Trujillo
|
-343661305
|
3 |
ANTON
|
Antonio Moreno
|
546149642
|
4 |
AROUT
|
Thomas Hardy
|
-1173585879
|
5 |
BERGS
|
Christina Berglund
|
607301330
|
6 |
BLAUS
|
Hanna Moos
|
-139102640
|
7 |
BLONP
|
Frédérique Citeaux
|
1252141989
|
8 |
BOLID
|
Martín Sommer
|
1471204062
|
9 |
BONAP
|
Laurence Lebihan
|
986684384
|
10 |
BOTTM
|
Elizabeth Lincoln
|
800010386
|
11 |
BSBEV
|
Victoria Ashworth
|
2031888813
|
12 |
CACTU
|
Patricio Simpson
|
21069611
|
13 |
CENTC
|
Francisco Chang
|
-1087486840
|
14 |
CHOPS
|
Yang Wang
|
1404214719
|
15 |
COMMI
|
Pedro Afonso
|
396878899
|
16 |
CONSH
|
Elizabeth Brown
|
-1024570944
|
17 |
DRACD
|
Sven Ottlieb
|
-1761577489
|
18 |
DUMON
|
Janine Labrune
|
2009379926
|
19 |
EASTC
|
Ann Devon
|
-1744913793
|
20 |
ERNSH
|
Roland Mendel
|
469074026
|
21 |
FAMIA
|
Aria Cruz
|
-1062938652
|
22 |
FISSA
|
Diego Roel
|
711095986
|
23 |
FOLIG
|
Martine Rancé
|
19140633
|
24 |
FOLKO
|
Maria Larsson
|
-1886469641
|
25 |
FRANK
|
Peter Franken
|
556482527
|
26 |
FRANR
|
Carine Schmitt
|
1621920216
|
27 |
FRANS
|
Paolo Accorti
|
-872285736
|
28 |
FURIB
|
Lino Rodriguez
|
-931924763
|
29 |
GALED
|
Eduardo Saavedra
|
-564115602
|
30 |
GODOS
|
José Pedro Freyre
|
2118499622
|
31 |
GOURL
|
André Fonseca
|
989226944
|
32 |
GREAL
|
Howard Snyder
|
395544861
|
33 |
GROSR
|
Manuel Pereira
|
1854769338
|
34 |
HANAR
|
Mario Pontes
|
1752967983
|
35 |
HILAA
|
Carlos Hernández
|
-1378083521
|
36 |
HUNGC
|
Yoshi Latimer
|
2056741646
|
37 |
HUNGO
|
Patricia McKenna
|
-1100639691
|
38 |
ISLAT
|
Helen Bennett
|
1499659769
|
39 |
KOENE
|
Philip Cramer
|
1344160375
|
40 |
LACOR
|
Daniel Tonini
|
-1627210430
|
41 |
LAMAI
|
Annette Roulet
|
-42892383
|
42 |
LAUGB
|
Yoshi Tannamuri
|
551043433
|
43 |
LAZYK
|
John Steel
|
257387782
|
44 |
LEHMS
|
Renate Messner
|
1575514147
|
45 |
LETSS
|
Jaime Yorres
|
-967591293
|
46 |
LILAS
|
Carlos González
|
-137113239
|
47 |
LINOD
|
Felipe Izquierdo
|
-594493521
|
48 |
LONEP
|
Fran Wilson
|
-2112044395
|
49 |
MAGAA
|
Giovanni Rovelli
|
-1465212557
|
50 |
MAISD
|
Catherine Dewey
|
1839111294
|
51 |
MEREP
|
Jean Fresnière
|
-220629013
|
52 |
MORGK
|
Alexander Feuer
|
-34942180
|
53 |
NORTS
|
Simon Crowther
|
442964522
|
54 |
OCEAN
|
Yvonne Moncada
|
-1075975132
|
55 |
OLDWO
|
Rene Phillips
|
181048688
|
56 |
OTTIK
|
Henriette Pfalzheim
|
-473851387
|
57 |
PARIS
|
Marie Bertrand
|
-754141155
|
58 |
PERIC
|
Guillermo Fernández
|
958155816
|
59 |
PICCO
|
Georg Pipps
|
-1327152980
|
60 |
PRINI
|
Isabel de Castro
|
2013143302
|
61 |
QUEDE
|
Bernardo Batista
|
-721147541
|
62 |
QUEEN
|
Lúcia Carvalho
|
527486379
|
63 |
QUICK
|
Horst Kloss
|
984918243
|
64 |
RANCH
|
Sergio Gutiérrez
|
-1080890410
|
65 |
RATTC
|
Paula Wilson
|
1208171504
|
66 |
REGGC
|
Maurizio Moroni
|
679232882
|
67 |
RICAR
|
Janete Limeira
|
556860477
|
68 |
RICSU
|
Michael Holz
|
-169394547
|
69 |
ROMEY
|
Alejandra Camino
|
-1398171439
|
70 |
SANTG
|
Jonas Bergulfsen
|
1154315067
|
71 |
SAVEA
|
Jose Pavarotti
|
-238121178
|
72 |
SEVES
|
Hari Kumar
|
-1769507520
|
73 |
SIMOB
|
Jytte Petersen
|
-1494686194
|
74 |
SPECD
|
Dominique Perrier
|
-1686843575
|
75 |
SPLIR
|
Art Braunschweiger
|
-72051112
|
76 |
SUPRD
|
Pascale Cartrain
|
586858343
|
77 |
THEBI
|
Liz Nixon
|
-1434738855
|
78 |
THECR
|
Liu Wong
|
-1280737904
|
79 |
TOMSP
|
Karin Josephs
|
1182205234
|
80 |
TORTU
|
Miguel Angel Paolino
|
-1774402209
|
81 |
TRADH
|
Anabela Domingues
|
134322471
|
82 |
TRAIH
|
Helvetius Nagy
|
-1208276186
|
83 |
VAFFE
|
Palle Ibsen
|
488795737
|
84 |
VICTE
|
Mary Saveley
|
1421195188
|
85 |
VINET
|
Paul Henriot
|
763393129
|
86 |
WANDK
|
Rita Müller
|
930242438
|
87 |
WARTH
|
Pirkko Koskitalo
|
259995047
|
88 |
WELLI
|
Paula Parente
|
1748461122
|
89 |
WHITC
|
Karl Jablonski
|
884805341
|
90 |
WILMK
|
Matti Karttunen
|
-1488901414
|
91 |
WOLZA
|
Zbyszek Piestrzeniewicz
|
-1063856833
|
8. Usage of CHECKSUM to find changes in the values
SQL Server Query 8
-- Keep original contactname, customerid current and changed checksum in temp table
SELECT CustomerID, ContactName, 0 AS OriginalChecksum, 0 AS CurrentChecksum
INTO #ForCustomerChecksum
FROM Customers
WHERE CustomerID = 'ALFKI';
-- Update temp table with current checksum
UPDATE #ForCustomerChecksum
SET OriginalChecksum = (SELECT CHECKSUM(CustomerID, ContactName, City)
FROM Customers
WHERE CustomerID = 'ALFKI')
WHERE CustomerID = 'ALFKI';
-- update customer contact number for a customerid
UPDATE Customers
SET ContactName = 'New Contact Name'
WHERE CustomerID = 'ALFKI';
-- update temp table with new/changed checksum
UPDATE #ForCustomerChecksum
SET CurrentChecksum = (SELECT CHECKSUM(CustomerID, ContactName, City)
FROM Customers
WHERE CustomerID = 'ALFKI')
WHERE CustomerID = 'ALFKI';
-- compare old and new checksum
SELECT
OriginalChecksum,
CurrentChecksum,
CASE
WHEN OriginalChecksum = CurrentChecksum THEN 'No Change'
ELSE 'Change Detected'
END AS ChangeStatus
FROM #ForCustomerChecksum
-- update back the original contact name for selected customer id
UPDATE Customers
SET ContactName = (SELECT ContactName
FROM #ForCustomerChecksum
WHERE CustomerID = 'ALFKI')
WHERE CustomerID = 'ALFKI';
-- drop temp table
DROP TABLE #ForCustomerChecksum
Create SQL query with SqlQueryBuilder 8
var (sql8, parameters8) = new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "ContactName")
.Column(0, "OriginalChecksum")
.Column(0, "CurrentChecksum")
.INTO(new Table("#ForCustomerChecksum"))
.From("Customers")
.Where(new Where(new Column("CustomerID").Equale("ALFKI")))
.Update("#ForCustomerChecksum", new List<ISetValue>()
{
new SetValue("OriginalChecksum", new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM(new Column("CustomerID"), new Column("ContactName"), new Column("City")), "CheckSum")
.From("Customers")
.Where(new Where(new Column("CustomerID").Equale("ALFKI"))))
})
.Update("Customers", new List<ISetValue>()
{
new SetValue("ContactName", "New Contact Name")
})
.Where(new Where(new Column("CustomerID").Equale("ALFKI")))
.Update("#ForCustomerChecksum", new List<ISetValue>()
{
new SetValue("CurrentChecksum", new SqlQueryBuilder()
.Select()
.Column(new CHECKSUM(new Column("CustomerID"), new Column("ContactName"), new Column("City")), "CheckSum")
.From("Customers")
.Where(new Where(new Column("CustomerID").Equale("ALFKI"))))
})
.Where(new Where(new Column("CustomerID").Equale("ALFKI")))
.Select()
.Columns("OriginalChecksum","CurrentChecksum")
.Column(new CASE()
.When(new Column("OriginalChecksum").Equale(new Column("CurrentChecksum"))).Then("No Change")
.Else("Change Detected")
, "ChangeStatus")
.From("#ForCustomerChecksum")
.Update("Customers", new List<ISetValue>()
{
new SetValue("ContactName", new SqlQueryBuilder()
.Select()
.Column("ContactName", "ContactName")
.From("#ForCustomerChecksum")
.Where(new Where(new Column("CustomerID").Equale("ALFKI"))))
})
.Where(new Where(new Column("CustomerID").Equale("ALFKI")))
.DropTable(new Table("#ForCustomerChecksum"))
.Build();
Query build by SqlQueryBuilder 8
SELECT CustomerID,
ContactName,
@pMAIN_2507200147313188260 AS OriginalChecksum,
@pMAIN_2507200147313188261 AS CurrentChecksum
INTO #ForCustomerChecksum
FROM Customers
WHERE CustomerID = @pMAIN_2507200147313188262;
UPDATE #ForCustomerChecksum
SET OriginalChecksum = (SELECT CHECKSUM(CustomerID, ContactName, City) AS CheckSum
FROM Customers
WHERE CustomerID = @pMAIN_2507200147313188263);
UPDATE Customers
SET ContactName = @pMAIN_2507200147313188264
WHERE CustomerID = @pMAIN_2507200147313188265;
UPDATE #ForCustomerChecksum
SET CurrentChecksum = (SELECT CHECKSUM(CustomerID, ContactName, City) AS CheckSum
FROM Customers
WHERE CustomerID = @pMAIN_2507200147313188266)
WHERE CustomerID = @pMAIN_2507200147313188267;
SELECT OriginalChecksum,
CurrentChecksum,
CASE WHEN OriginalChecksum = CurrentChecksum THEN @pMAIN_2507200147313188268 ELSE @pMAIN_2507200147313188269 END AS ChangeStatus
FROM #ForCustomerChecksum;
UPDATE Customers
SET ContactName = (SELECT ContactName AS ContactName
FROM #ForCustomerChecksum
WHERE CustomerID = @pMAIN_250720014731318826_10)
WHERE CustomerID = @pMAIN_250720014731318826_11;
DROP TABLE #ForCustomerChecksum;
Parameters (If used)
Name |
Value |
@pMAIN_2507200147313188260 |
0 |
@pMAIN_2507200147313188261 |
0 |
@pMAIN_2507200147313188262 |
ALFKI |
@pMAIN_2507200147313188263 |
ALFKI |
@pMAIN_2507200147313188264 |
New Contact Name |
@pMAIN_2507200147313188265 |
ALFKI |
@pMAIN_2507200147313188266 |
ALFKI |
@pMAIN_2507200147313188267 |
ALFKI |
@pMAIN_2507200147313188268 |
No Change |
@pMAIN_2507200147313188269 |
Change Detected |
@pMAIN_250720014731318826_10 |
ALFKI |
@pMAIN_250720014731318826_11 |
ALFKI |
Query Results 8:
|
OriginalChecksum |
CurrentChecksum |
ChangeStatus |
1 |
-1292565906
|
157316278
|
Change Detected
|