Guest User

Untitled

a guest
Jul 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlTypes;
  4. using System.IO;
  5. using Microsoft.SqlServer.Server;
  6.  
  7. [Serializable]
  8. [SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000)]
  9. public struct strconcat : IBinarySerialize
  10. {
  11. private List<String> values;
  12.  
  13. public void Init()
  14. {
  15. this.values = new List<String>();
  16. }
  17.  
  18. public void Accumulate(SqlString value = new SqlString())
  19. {
  20. this.values.Add(value.Value);
  21. }
  22.  
  23. public void Merge(strconcat value)
  24. {
  25. this.values.AddRange(value.values.ToArray());
  26. }
  27.  
  28. public SqlString Terminate()
  29. {
  30. return new SqlString(string.Join(", ", this.values.ToArray()));
  31. }
  32.  
  33. public void Read(BinaryReader r)
  34. {
  35. int itemCount = r.ReadInt32();
  36. this.values = new List<String>(itemCount);
  37. for (int i = 0; i <= itemCount - 1; i++)
  38. {
  39. this.values.Add(r.ReadString());
  40. }
  41. }
  42.  
  43. public void Write(BinaryWriter w)
  44. {
  45. w.Write(this.values.Count);
  46. foreach (string s in this.values)
  47. {
  48. w.Write(s);
  49. }
  50. }
  51. }
  52.  
  53. DECLARE @listCol NVARCHAR(2000)
  54.  
  55. SELECT @listCol = STUFF(( SELECT '],[' + A.Name
  56. FROM Attribute A,Category C
  57. WHERE A.CategoryId = C.Id
  58. ORDER BY A.DisplayOrder DESC
  59. FOR XML PATH('')), 1, 2, '') + ']'
  60. DECLARE @query NVARCHAR(2000)
  61. SET @query =
  62.  
  63. N'SELECT * FROM (SELECT P.*,A.Name AttributeName,PA.OriginalValue FROM Product P,Product_Attribute PA, Attribute A WHERE P.Id = PA.ProductId AND A.Id = PA.AttributeId) src
  64. PIVOT
  65. (
  66. dbo.strconcat(OriginalValue) FOR AttributeName
  67. IN ('+@listCol+')) AS pvt'
  68.  
  69. EXECUTE (@query)
  70.  
  71. Msg 406, Level 16, State 1, Line 5
  72. dbo.strconcat cannot be used in the PIVOT operator because it is not invariant to NULLs.
  73.  
  74. [SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000,
  75. IsInvariantToNulls = true)]
Add Comment
Please, Sign In to add comment