Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.48 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. T-SQL: How can I compare two variables of type XML when length > VarChar(MAX)?
  2. DECLARE @XmlA   XML
  3. DECLARE @XmlB   XML
  4.  
  5. SET @XmlA = '[Really long Xml value]'
  6. SET @XmlB = '[Really long Xml value]'
  7.  
  8. IF @XmlA = @XmlB
  9.     SELECT 'Matching Xml!'
  10.        
  11. using System;
  12. using System.Data.SqlTypes;
  13. using System.IO;
  14.  
  15. namespace ClrHelpers
  16. {
  17.     public partial class UserDefinedFunctions {
  18.         [Microsoft.SqlServer.Server.SqlFunction]
  19.         public static Guid HashMD5(SqlBytes data) {
  20.             System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  21.             md5.Initialize();
  22.             int len = 0;
  23.             byte[] b = new byte[8192];
  24.             Stream s = data.Stream;
  25.             do {
  26.                 len = s.Read(b, 0, 8192);
  27.                 md5.TransformBlock(b, 0, len, b, 0);
  28.             } while(len > 0);
  29.             md5.TransformFinalBlock(b, 0, 0);
  30.             Guid g = new Guid(md5.Hash);
  31.             return g;
  32.         }
  33.     };
  34. }
  35.        
  36. CREATE FUNCTION dbo.GetMyLongHash(@data VARBINARY(MAX))
  37. RETURNS VARBINARY(MAX)
  38. WITH RETURNS NULL ON NULL INPUT
  39. AS
  40. BEGIN
  41.     DECLARE @res VARBINARY(MAX) = 0x
  42.     DECLARE @position INT = 1, @len INT = DATALENGTH(@data)
  43.  
  44.     WHILE 1 = 1
  45.     BEGIN
  46.         SET @res = @res + HASHBYTES('MD5', SUBSTRING(@data, @position, 8000))
  47.         SET @position = @position+8000
  48.         IF @Position > @len
  49.           BREAK
  50.     END
  51.     WHILE DATALENGTH(@res) > 16 SET @res= dbo.GetMyLongHash(@res)
  52.     RETURN @res
  53. END