SlideShare a Scribd company logo
1 of 4
Download to read offline
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

What's hot

Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android yugandhar vadlamudi
 
cPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturescPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturesDave Stokes
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentalsMadhuri Kavade
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)SqliChema Alonso
 
Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republicKaing Menglieng
 
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptJavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptDave Stokes
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationRandy Connolly
 
Preethi apex-basics-jan19
Preethi apex-basics-jan19Preethi apex-basics-jan19
Preethi apex-basics-jan19Preethi Harris
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursorsinfo_zybotech
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsDave Stokes
 
Session06 handling xml data
Session06  handling xml dataSession06  handling xml data
Session06 handling xml datakendyhuu
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Salman Memon
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for AndroidJakir Hossain
 

What's hot (20)

Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
 
cPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturescPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven Features
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)Sqli
 
Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republic
 
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptJavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
 
Schema webinar
Schema webinarSchema webinar
Schema webinar
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Oracle Database View
Oracle Database ViewOracle Database View
Oracle Database View
 
Preethi apex-basics-jan19
Preethi apex-basics-jan19Preethi apex-basics-jan19
Preethi apex-basics-jan19
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
 
Schema201 webinar
Schema201 webinarSchema201 webinar
Schema201 webinar
 
Ch04
Ch04Ch04
Ch04
 
Ch05
Ch05Ch05
Ch05
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & Histograms
 
Session06 handling xml data
Session06  handling xml dataSession06  handling xml data
Session06 handling xml data
 
Module 3
Module 3Module 3
Module 3
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
 

Viewers also liked

EXPANSION-24-10-2015-RUGBY Y VALORES
EXPANSION-24-10-2015-RUGBY Y VALORESEXPANSION-24-10-2015-RUGBY Y VALORES
EXPANSION-24-10-2015-RUGBY Y VALORESSergi Loughney
 
Идентификация пользователей Госуслуг+Финансы
Идентификация пользователей Госуслуг+ФинансыИдентификация пользователей Госуслуг+Финансы
Идентификация пользователей Госуслуг+ФинансыInstitute of development of the Internet
 
เกาะหลีเป๊ะ
เกาะหลีเป๊ะเกาะหลีเป๊ะ
เกาะหลีเป๊ะfahjongrak
 
M. BOURGEOIS 2015 resume
M. BOURGEOIS 2015 resumeM. BOURGEOIS 2015 resume
M. BOURGEOIS 2015 resumeMARIE KORNAGO
 
Mujeres y RSC. Consideraciones finales
Mujeres y RSC. Consideraciones finalesMujeres y RSC. Consideraciones finales
Mujeres y RSC. Consideraciones finalesVictoria Bazaine
 
Мой Пенсионер РФ
Мой Пенсионер РФМой Пенсионер РФ
Мой Пенсионер РФalexwalker007
 
trabajo de 40x40 deportes
trabajo de 40x40 deportes trabajo de 40x40 deportes
trabajo de 40x40 deportes stivenrpt
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answersKaing Menglieng
 

Viewers also liked (14)

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

Similar to Using hash fields in sql server tech republic

Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republicKaing Menglieng
 
Introduction to change data capture in sql server 2008 tech republic
Introduction to change data capture in sql server 2008   tech republicIntroduction to change data capture in sql server 2008   tech republic
Introduction to change data capture in sql server 2008 tech republicKaing Menglieng
 
See sql server graphical execution plans in action tech republic
See sql server graphical execution plans in action   tech republicSee sql server graphical execution plans in action   tech republic
See sql server graphical execution plans in action tech republicKaing Menglieng
 
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final MileKye Hittle
 
SQLCAT: Tier-1 BI in the World of Big Data
SQLCAT: Tier-1 BI in the World of Big DataSQLCAT: Tier-1 BI in the World of Big Data
SQLCAT: Tier-1 BI in the World of Big DataDenny Lee
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developersukdpe
 
Hive 3 - a new horizon
Hive 3 - a new horizonHive 3 - a new horizon
Hive 3 - a new horizonThejas Nair
 
Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)James Serra
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's Newdpcobb
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Miningllangit
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Miningllangit
 
An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008Klaudiia Jacome
 
Migrating Very Large Site Collections (SPSDC)
Migrating Very Large Site Collections (SPSDC)Migrating Very Large Site Collections (SPSDC)
Migrating Very Large Site Collections (SPSDC)kiwiboris
 
Big Data Analytics from Azure Cloud to Power BI Mobile
Big Data Analytics from Azure Cloud to Power BI MobileBig Data Analytics from Azure Cloud to Power BI Mobile
Big Data Analytics from Azure Cloud to Power BI MobileRoy Kim
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Miningllangit
 
Sql Server 2008 Enhancements
Sql Server 2008 EnhancementsSql Server 2008 Enhancements
Sql Server 2008 Enhancementskobico10
 
Building the Perfect SharePoint 2010 Farm
Building the Perfect SharePoint 2010 FarmBuilding the Perfect SharePoint 2010 Farm
Building the Perfect SharePoint 2010 FarmMichael Noel
 

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

Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 
Introduction to change data capture in sql server 2008 tech republic
Introduction to change data capture in sql server 2008   tech republicIntroduction to change data capture 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
See sql server graphical execution plans in action   tech republicSee sql server graphical execution plans in action   tech republic
See sql server graphical execution plans in action tech republic
 
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
2019 -04-23 Austin, TX Tableau Users Group - Deployment: The Final Mile
 
SQLCAT: Tier-1 BI in the World of Big Data
SQLCAT: Tier-1 BI in the World of Big DataSQLCAT: Tier-1 BI in the World of Big Data
SQLCAT: Tier-1 BI in the World of Big Data
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developers
 
Exchange 2010 SP2 & Tips
Exchange 2010 SP2 & TipsExchange 2010 SP2 & Tips
Exchange 2010 SP2 & Tips
 
Hive 3 - a new horizon
Hive 3 - a new horizonHive 3 - a new horizon
Hive 3 - a new horizon
 
Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's New
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Mining
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Mining
 
Hive 3 a new horizon
Hive 3  a new horizonHive 3  a new horizon
Hive 3 a new horizon
 
An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008
 
Migrating Very Large Site Collections (SPSDC)
Migrating Very Large Site Collections (SPSDC)Migrating Very Large Site Collections (SPSDC)
Migrating Very Large Site Collections (SPSDC)
 
Big Data Analytics from Azure Cloud to Power BI Mobile
Big Data Analytics from Azure Cloud to Power BI MobileBig Data Analytics from Azure Cloud to Power BI Mobile
Big Data Analytics from Azure Cloud to Power BI Mobile
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Mining
 
Sql Server 2008 Enhancements
Sql Server 2008 EnhancementsSql Server 2008 Enhancements
Sql Server 2008 Enhancements
 
Building the Perfect SharePoint 2010 Farm
Building the Perfect SharePoint 2010 FarmBuilding the Perfect SharePoint 2010 Farm
Building the Perfect SharePoint 2010 Farm
 
It ready dw_day3_rev00
It ready dw_day3_rev00It ready dw_day3_rev00
It ready dw_day3_rev00
 

More from Kaing Menglieng

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

More from Kaing Menglieng (20)

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