Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. USE Master;
  2. GO
  3.  
  4. -- Create test database
  5. CREATE DATABASE [TemporalTables];
  6. GO
  7.  
  8. USE TemporalTables;
  9. GO
  10.  
  11. SET ANSI_NULLS ON
  12. GO
  13.  
  14. SET QUOTED_IDENTIFIER ON
  15. GO
  16.  
  17. -- Hidden period columns
  18. CREATE TABLE [dbo].[PersonHistory](
  19. [PersonID] [int] NOT NULL,
  20. [EmailAddress] [varchar](75) NULL,
  21. [IsSubscribed] [bit] NULL,
  22. [SysStartTime] datetime2 NOT NULL,
  23. [SysEndTime] datetime2 NOT NULL
  24. );
  25.  
  26. CREATE CLUSTERED COLUMNSTORE INDEX IX_PersonHistory
  27. ON PersonHistory;
  28. CREATE NONCLUSTERED INDEX IX_PersonHistory_ID_PERIOD_COLUMNS
  29. ON PersonHistory (SysEndTime, SysStartTime, PersonID);
  30. GO
  31.  
  32. CREATE TABLE [dbo].[Person](
  33. [PersonID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
  34. [EmailAddress] [varchar](75) NULL,
  35. [IsSubscribed] [bit] NULL,
  36. [SysStartTime] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
  37. [SysEndTime] datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
  38. PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
  39. )
  40. WITH
  41. (
  42. SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.PersonHistory)
  43. );
  44.  
  45. -- Current Time UTC
  46. SELECT sysutcdatetime();
  47. -- Value: 2017-03-14 16:11:56.6294004
  48.  
  49. --Insert Rows
  50. INSERT [dbo].[Person] ([EmailAddress], [IsSubscribed]) VALUES ('SD.Burman@gmail.com', 1);
  51. GO
  52.  
  53. INSERT [dbo].[Person] VALUES ('RD.Burman@gmail.com', 1);
  54. GO
  55.  
  56. INSERT [dbo].[Person] ([EmailAddress], [IsSubscribed], [SysStartTime], [SysEndTime])
  57. VALUES ('AR.Rehman@gmail.com', 1, default, default);
  58. GO
  59.  
  60. -- Query the main table
  61. SELECT *, SysStartTime, SysEndTime
  62. FROM dbo.Person
  63. -- Shows all changes for specific PK (for inserts to show up)
  64. SELECT *, SysStartTime, SysEndTime
  65. FROM dbo.Person
  66. FOR SYSTEM_TIME ALL
  67. where Personid IN (1,2,3);
  68.  
  69. -- Current Time UTC
  70. SELECT sysutcdatetime();
  71. -- Value: 2017-03-14 16:13:27.0374068
  72.  
  73. --Update Rows (Actual Updates)
  74. UPDATE [dbo].[Person] SET [EmailAddress] = 'SD.Burman@hotmail.com' WHERE [EmailAddress] = 'SD.Burman@gmail.com';
  75. GO
  76. UPDATE [dbo].[Person] SET [EmailAddress] = 'RD.Burman@hotmail.com' WHERE [EmailAddress] = 'RD.Burman@gmail.com';
  77. GO
  78. --Update Rows (Dummy Update)
  79. UPDATE [dbo].[Person] SET [EmailAddress] = 'AR.Rehman@gmail.com' WHERE [EmailAddress] = 'AR.Rehman@gmail.com';
  80. GO
  81.  
  82. -- Query the history table
  83. SELECT *
  84. FROM dbo.PersonHistory
  85.  
  86. -- !!!
  87. -- What I'd want is that the Id = 3 update to not get saved since the data did not change.
  88. -- Also a way I can limit the number of columns I want this behavior on.
  89. -- PersonID EmailAddress IsSubscribed SysStartTime SysEndTime
  90. -- ----------- --------------------------------------------------------------------------- ------------ --------------------------- ---------------------------
  91. -- 1 SD.Burman@gmail.com 1 2017-03-14 16:12:29.5018193 2017-03-14 16:13:27.0374068
  92. -- 2 RD.Burman@gmail.com 1 2017-03-14 16:12:29.5118172 2017-03-14 16:13:27.1484051
  93. -- !!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement