SlideShare a Scribd company logo
Using Hash Fields in SQL Server | TechRepublic



   ZDNet Asia    SmartPlanet    TechRepublic                                                                                 Log In   Join TechRepublic   FAQ   Go Pro!




                                                     Blogs   Downloads         Newsletters       Galleries      Q&A   Discussions     News
                                                 Research Library


     IT Management             Development           IT Support       Data Center         Networks         Security




     Home / Blogs / The Enterprise Cloud                                                    Follow this blog:

     The Enterprise Cloud


     Using Hash Fields in SQL
     Server
     By Tim Chapman
     December 18, 2008, 8:33 AM PST

     Takeaway: From auditing data changes to capturing data for loading a data warehouse, hash
     fields have a ton of practical uses. In today’s article, database consultant Tim Chapman shows
     how you can use built-in SQL Server functions to create your own hash fields.

      A hash field is a calculated numeric field based on the value(s) contained in one or more fields.
     This calculated value is great for detecting data changes, which can be put to use in auditing
     strategies or datawarehouse ETL (extraction, transformation, loading) operations.

     Creating a hash field in SQL Server is easy. The database engine has a couple of built-in
     functions that can be used to generate the hashed value. Two of these functions are
     CHECKSUM() and BINARY_CHECKSUM(). CHECKSUM() is intended to be used to build hash
     indexes while BINARY_CHECKSUM() is great for detected data changes. I’ll be using the
     BINARY_CHECKSUM funtion for the purposes of todays writing.

     Using the hash field
     In the following example I’ll show you how you can take advantage of the BINARY_CHECKSUM()
     function. First, I’ll create the SalesHistory table and load some data into it.

     CREATE TABLE [dbo].[SalesHistory]

     (

                SaleID int IDENTITY(1,1),

                Product varchar(10) NULL,

                SaleDate datetime NULL,

                StatusID TINYINT NULL,

                SalePrice money NULL

     )

     GO



     SET NOCOUNT ON



http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
Using Hash Fields in SQL Server | TechRepublic




     BEGIN TRANSACTION

     DECLARE @i INT

     SET @i = 1

     WHILE (@i <=500)

     BEGIN



                INSERT INTO [SalesHistory](Product, SaleDate, SalePrice, StatusID)

                VALUES (’Computer’, DATEADD(ww, @i, ‘3/11/1919′),

                DATEPART(ms, GETDATE()) + (@i + 57), 1)



                INSERT INTO [SalesHistory](Product, SaleDate, SalePrice, StatusID)

                VALUES(’BigScreen’, DATEADD(ww, @i, ‘3/11/1927′),

                DATEPART(ms, GETDATE()) + (@i + 13),5)



                INSERT INTO [SalesHistory](Product, SaleDate, SalePrice, StatusID)

                VALUES(’PoolTable’, DATEADD(ww, @i, ‘3/11/1908′),

                DATEPART(ms, GETDATE()) + (@i + 29),8)



                SET @i = @i + 1



     END

     COMMIT TRANSACTION



     ALTER TABLE SalesHistory

     ADD CONSTRAINT pk_SalesHistory_SaleID PRIMARY KEY CLUSTERED (SaleID)

     Once my table is created, I can add a calculated field to the SalesHistory table using the
     BINARY_CHECKSUM() function.

     ALTER TABLE SalesHistory

     ADD HashField AS BINARY_CHECKSUM(SaleID, StatusID, Product)



     The HashField I create above creates a checksum on the SaleID, StatusID, and Product fields.
     The resulting value is a signed integer value based on the values in the SaleID, StatusID, and
     Product fields. As the values in these fields the HashField value will change also. I can then use
     this HashField value to keep track of changes to specific data fields, which makes it great for
     capturing changes for loading a data warehouse.

     Capturing these changes can be as easy as comparing each hash value to a previously stored
     hash value to determine changed rows to as complicated to finding row data changes and then



http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
Using Hash Fields in SQL Server | TechRepublic

     routines to find the changed values.

     Regardless of how you use it, using hash values in SQL Server is a great tool.




     Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublic’s free
     newsletters.




                     About Tim Chapman
                        Full Bio     Contact




                   Disabling the firewall in                      Prerequisites overview for
                   Windows Server 2008 Core                       installing Windows Essential
                   Edition                                        Business Server 2008




         3            Join the conversation!                                               Add Your Opinion
      Comments        Follow via:



       Staff Picks     Top Rated       Most Recent       My Contacts                              See All Comments




                       RE: Using Hash Fields in SQL Server                                                0
                       edbeo@... 19th Jun 2009                                                           Votes



             Hi, I hope you can help me with this question, How can I convert a hash value to
             sqltext? I have a SQL Server 2000 SP4 Standard ed.
             Thank you!


                 View in thread




                       Whereever you like as far as I can gather                                          0
                       Tony Hopkinson 8th Jan 2009                                                       Votes



             In the data warehousing strategy You'd have the last update to it store the hash, and
             then compare it with the operational DB, to see if there's any change, and may be
             which field changed. It's going... Read Whole Comment +


                 View in thread




                       Can you elaborate on a typical data checking process                               0
                       victor.gutzler@... 7th Jan 2009                                                   Votes


             You wrote "Capturing these changes can be as easy as comparing each hash value
             to a previously stored hash value to determine changed rows to as complicated to
             finding row data changes and then... Read Whole Comment +


                 View in thread




http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
Using Hash Fields in SQL Server | TechRepublic



                                                 See all comments



     Join the TechRepublic Community and join the conversation! Signing-up is
     free and quick, Do it now, we want to hear your opinion.

       Join       Login




http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]

More Related Content

PDF
Using object dependencies in sql server 2008 tech republic
PDF
Using sql server 2008's merge statement tech republic
PDF
TSQL Coding Guidelines
PPT
Sql views
DOCX
Android sq lite-chapter 22
PPT
Sql Summit Clr, Service Broker And Xml
PPT
SQL Views
Using object dependencies in sql server 2008 tech republic
Using sql server 2008's merge statement tech republic
TSQL Coding Guidelines
Sql views
Android sq lite-chapter 22
Sql Summit Clr, Service Broker And Xml
SQL Views

What's hot (20)

PPT
Object Relational model for SQLIite in android
PPTX
cPanel now supports MySQL 8.0 - My Top Seven Features
PPTX
Ch06 ado.net fundamentals
PPTX
Playing With (B)Sqli
PDF
Avoiding cursors with sql server 2005 tech republic
PDF
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
PPTX
Schema webinar
PPT
ASP.NET 08 - Data Binding And Representation
PPTX
Oracle Database View
PPTX
Preethi apex-basics-jan19
DOCX
Accessing data with android cursors
DOCX
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
PPTX
Schema201 webinar
PDF
Ch04
PDF
Ch05
PPTX
Confoo 2021 - MySQL Indexes & Histograms
PDF
Session06 handling xml data
DOC
Module 3
PPT
Including Constraints -Oracle Data base
PPTX
Data Handning with Sqlite for Android
Object Relational model for SQLIite in android
cPanel now supports MySQL 8.0 - My Top Seven Features
Ch06 ado.net fundamentals
Playing With (B)Sqli
Avoiding cursors with sql server 2005 tech republic
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
Schema webinar
ASP.NET 08 - Data Binding And Representation
Oracle Database View
Preethi apex-basics-jan19
Accessing data with android cursors
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
Schema201 webinar
Ch04
Ch05
Confoo 2021 - MySQL Indexes & Histograms
Session06 handling xml data
Module 3
Including Constraints -Oracle Data base
Data Handning with Sqlite for Android
Ad

Viewers also liked (14)

PDF
T8 led tube
PDF
EXPANSION-24-10-2015-RUGBY Y VALORES
PPTX
Идентификация пользователей Госуслуг+Финансы
DOCX
เกาะหลีเป๊ะ
PDF
Prova biologia 1º ano
RTF
M. BOURGEOIS 2015 resume
DOCX
Mujeres y RSC. Consideraciones finales
DOCX
Mujeres y RSC. Parte II
PDF
Мой Пенсионер РФ
PDF
Curriculum Vita
PDF
SacsWelcomeFLyer
PPTX
trabajo de 40x40 deportes
PPS
Les journées de Chipo - Jour 288
PDF
Sql server common interview questions and answers
T8 led tube
EXPANSION-24-10-2015-RUGBY Y VALORES
Идентификация пользователей Госуслуг+Финансы
เกาะหลีเป๊ะ
Prova biologia 1º ano
M. BOURGEOIS 2015 resume
Mujeres y RSC. Consideraciones finales
Mujeres y RSC. Parte II
Мой Пенсионер РФ
Curriculum Vita
SacsWelcomeFLyer
trabajo de 40x40 deportes
Les journées de Chipo - Jour 288
Sql server common interview questions and answers
Ad

Similar to Using hash fields in sql server tech republic (20)

PDF
Using grouping sets in sql server 2008 tech republic
PDF
Introduction to change data capture in sql server 2008 tech republic
PDF
See sql server graphical execution plans in action tech republic
PDF
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
PPTX
SQLCAT: Tier-1 BI in the World of Big Data
PPT
SQL Server 2008 for Developers
PPTX
Exchange 2010 SP2 & Tips
PPTX
Hive 3 - a new horizon
PPTX
Azure Synapse Analytics Overview (r1)
PPTX
Sql 2016 - What's New
PPT
SQL Server 2008 Data Mining
PPT
SQL Server 2008 Data Mining
PDF
Hive 3 a new horizon
DOCX
An introduction to new data warehouse scalability features in sql server 2008
PPTX
Migrating Very Large Site Collections (SPSDC)
PDF
Big Data Analytics from Azure Cloud to Power BI Mobile
PPT
SQL Server 2008 Data Mining
PPSX
Sql Server 2008 Enhancements
PPTX
Building the Perfect SharePoint 2010 Farm
PPT
It ready dw_day3_rev00
Using grouping sets in sql server 2008 tech republic
Introduction to change data capture in sql server 2008 tech republic
See sql server graphical execution plans in action tech republic
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
SQLCAT: Tier-1 BI in the World of Big Data
SQL Server 2008 for Developers
Exchange 2010 SP2 & Tips
Hive 3 - a new horizon
Azure Synapse Analytics Overview (r1)
Sql 2016 - What's New
SQL Server 2008 Data Mining
SQL Server 2008 Data Mining
Hive 3 a new horizon
An introduction to new data warehouse scalability features in sql server 2008
Migrating Very Large Site Collections (SPSDC)
Big Data Analytics from Azure Cloud to Power BI Mobile
SQL Server 2008 Data Mining
Sql Server 2008 Enhancements
Building the Perfect SharePoint 2010 Farm
It ready dw_day3_rev00

More from Kaing Menglieng (20)

PDF
What is your sql server backup strategy tech_republic
PDF
Understand when to use user defined functions in sql server tech-republic
PDF
Sql server indexed views speed up your select queries part 1 - code-projec
PDF
Sql server – query optimization – remove bookmark lookup – remove rid lookup
PDF
Sql server common interview questions and answers page 6
PDF
Sql server common interview questions and answers page 5
PDF
Sql server common interview questions and answers page 4
PDF
Sql server common interview questions and answers page 2
PDF
Sql server – 2008 – hardware and software requirements for installing sql se
PDF
Speeding up queries with semi joins and anti-joins
PDF
Speed up sql
PDF
Speed up sql server apps - visual studio magazine
PDF
Reviewing sql server permissions tech republic
PDF
Query optimization how to search millions of record in sql table faster -
PDF
Optimize sql server queries with these advanced tuning techniques tech repu
PDF
New date datatypes in sql server 2008 tech republic
PDF
Introduction to policy based management in sql server 2008 tech-republic
PDF
How to import an excel file into sql server 2005 using integration services
PDF
How do i... reseed a sql server identity column tech_republic
PDF
How do i... query foreign data using sql server's linked servers tech_repu
What is your sql server backup strategy tech_republic
Understand when to use user defined functions in sql server tech-republic
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server common interview questions and answers page 6
Sql server common interview questions and answers page 5
Sql server common interview questions and answers page 4
Sql server common interview questions and answers page 2
Sql server – 2008 – hardware and software requirements for installing sql se
Speeding up queries with semi joins and anti-joins
Speed up sql
Speed up sql server apps - visual studio magazine
Reviewing sql server permissions tech republic
Query optimization how to search millions of record in sql table faster -
Optimize sql server queries with these advanced tuning techniques tech repu
New date datatypes in sql server 2008 tech republic
Introduction to policy based management in sql server 2008 tech-republic
How to import an excel file into sql server 2005 using integration services
How do i... reseed a sql server identity column tech_republic
How do i... query foreign data using sql server's linked servers tech_repu

Using hash fields in sql server tech republic

  • 1. Using Hash Fields in SQL Server | TechRepublic ZDNet Asia SmartPlanet TechRepublic Log In Join TechRepublic FAQ Go Pro! Blogs Downloads Newsletters Galleries Q&A Discussions News Research Library IT Management Development IT Support Data Center Networks Security Home / Blogs / The Enterprise Cloud Follow this blog: The Enterprise Cloud Using Hash Fields in SQL Server By Tim Chapman December 18, 2008, 8:33 AM PST Takeaway: From auditing data changes to capturing data for loading a data warehouse, hash fields have a ton of practical uses. In today’s article, database consultant Tim Chapman shows how you can use built-in SQL Server functions to create your own hash fields. A hash field is a calculated numeric field based on the value(s) contained in one or more fields. This calculated value is great for detecting data changes, which can be put to use in auditing strategies or datawarehouse ETL (extraction, transformation, loading) operations. Creating a hash field in SQL Server is easy. The database engine has a couple of built-in functions that can be used to generate the hashed value. Two of these functions are CHECKSUM() and BINARY_CHECKSUM(). CHECKSUM() is intended to be used to build hash indexes while BINARY_CHECKSUM() is great for detected data changes. I’ll be using the BINARY_CHECKSUM funtion for the purposes of todays writing. Using the hash field In the following example I’ll show you how you can take advantage of the BINARY_CHECKSUM() function. First, I’ll create the SalesHistory table and load some data into it. CREATE TABLE [dbo].[SalesHistory] ( SaleID int IDENTITY(1,1), Product varchar(10) NULL, SaleDate datetime NULL, StatusID TINYINT NULL, SalePrice money NULL ) GO SET NOCOUNT ON http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
  • 2. Using Hash Fields in SQL Server | TechRepublic BEGIN TRANSACTION DECLARE @i INT SET @i = 1 WHILE (@i <=500) BEGIN INSERT INTO [SalesHistory](Product, SaleDate, SalePrice, StatusID) VALUES (’Computer’, DATEADD(ww, @i, ‘3/11/1919′), DATEPART(ms, GETDATE()) + (@i + 57), 1) INSERT INTO [SalesHistory](Product, SaleDate, SalePrice, StatusID) VALUES(’BigScreen’, DATEADD(ww, @i, ‘3/11/1927′), DATEPART(ms, GETDATE()) + (@i + 13),5) INSERT INTO [SalesHistory](Product, SaleDate, SalePrice, StatusID) VALUES(’PoolTable’, DATEADD(ww, @i, ‘3/11/1908′), DATEPART(ms, GETDATE()) + (@i + 29),8) SET @i = @i + 1 END COMMIT TRANSACTION ALTER TABLE SalesHistory ADD CONSTRAINT pk_SalesHistory_SaleID PRIMARY KEY CLUSTERED (SaleID) Once my table is created, I can add a calculated field to the SalesHistory table using the BINARY_CHECKSUM() function. ALTER TABLE SalesHistory ADD HashField AS BINARY_CHECKSUM(SaleID, StatusID, Product) The HashField I create above creates a checksum on the SaleID, StatusID, and Product fields. The resulting value is a signed integer value based on the values in the SaleID, StatusID, and Product fields. As the values in these fields the HashField value will change also. I can then use this HashField value to keep track of changes to specific data fields, which makes it great for capturing changes for loading a data warehouse. Capturing these changes can be as easy as comparing each hash value to a previously stored hash value to determine changed rows to as complicated to finding row data changes and then http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
  • 3. Using Hash Fields in SQL Server | TechRepublic routines to find the changed values. Regardless of how you use it, using hash values in SQL Server is a great tool. Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublic’s free newsletters. About Tim Chapman Full Bio Contact Disabling the firewall in Prerequisites overview for Windows Server 2008 Core installing Windows Essential Edition Business Server 2008 3 Join the conversation! Add Your Opinion Comments Follow via: Staff Picks Top Rated Most Recent My Contacts See All Comments RE: Using Hash Fields in SQL Server 0 edbeo@... 19th Jun 2009 Votes Hi, I hope you can help me with this question, How can I convert a hash value to sqltext? I have a SQL Server 2000 SP4 Standard ed. Thank you! View in thread Whereever you like as far as I can gather 0 Tony Hopkinson 8th Jan 2009 Votes In the data warehousing strategy You'd have the last update to it store the hash, and then compare it with the operational DB, to see if there's any change, and may be which field changed. It's going... Read Whole Comment + View in thread Can you elaborate on a typical data checking process 0 victor.gutzler@... 7th Jan 2009 Votes You wrote "Capturing these changes can be as easy as comparing each hash value to a previously stored hash value to determine changed rows to as complicated to finding row data changes and then... Read Whole Comment + View in thread http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
  • 4. Using Hash Fields in SQL Server | TechRepublic See all comments Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion. Join Login http://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]