Using hash fields in sql server tech republic

457 views
348 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
457
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Using hash fields in sql server tech republic

  1. 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 ONhttp://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
  2. 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 thenhttp://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
  3. 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 Youd have the last update to it store the hash, and then compare it with the operational DB, to see if theres any change, and may be which field changed. Its 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 threadhttp://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]
  4. 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 Loginhttp://www.techrepublic.com/blog/datacenter/using-hash-fields-in-sql-server/489[08/29/2012 3:48:29 PM]

×