Advertisement
anchormodeling

Create example model database

Sep 6th, 2013
6,647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 151.46 KB | None | 0 0
  1. -- KNOTS --------------------------------------------------------------------------------------------------------------
  2. --
  3. -- Knots are used to store finite sets of values, normally used to describe states
  4. -- of entities (through knotted attributes) or relationships (through knotted ties).
  5. -- Knots have their own surrogate identities and are therefore immutable.
  6. -- Values can be added to the set over time though.
  7. -- Knots should have values that are mutually exclusive and exhaustive.
  8. --
  9. -- Knot table ---------------------------------------------------------------------------------------------------------
  10. -- PAT_ParentalType table
  11. -----------------------------------------------------------------------------------------------------------------------
  12. IF Object_ID('PAT_ParentalType', 'U') IS NULL
  13. CREATE TABLE [dbo].[PAT_ParentalType] (
  14.     PAT_ID tinyint not null,
  15.     PAT_ParentalType varchar(42) not null,
  16.     Metadata_PAT int not null,
  17.     constraint pkPAT_ParentalType primary key (
  18.         PAT_ID asc
  19.     ),
  20.     constraint uqPAT_ParentalType unique (
  21.         PAT_ParentalType
  22.     )
  23. );
  24. GO
  25. -- Knot table ---------------------------------------------------------------------------------------------------------
  26. -- PLV_ProfessionalLevel table
  27. -----------------------------------------------------------------------------------------------------------------------
  28. IF Object_ID('PLV_ProfessionalLevel', 'U') IS NULL
  29. CREATE TABLE [dbo].[PLV_ProfessionalLevel] (
  30.     PLV_ID tinyint not null,
  31.     PLV_ProfessionalLevel varchar(42) not null,
  32.     Metadata_PLV int not null,
  33.     constraint pkPLV_ProfessionalLevel primary key (
  34.         PLV_ID asc
  35.     ),
  36.     constraint uqPLV_ProfessionalLevel unique (
  37.         PLV_ProfessionalLevel
  38.     )
  39. );
  40. GO
  41. -- Knot table ---------------------------------------------------------------------------------------------------------
  42. -- RAT_Rating table
  43. -----------------------------------------------------------------------------------------------------------------------
  44. IF Object_ID('RAT_Rating', 'U') IS NULL
  45. CREATE TABLE [dbo].[RAT_Rating] (
  46.     RAT_ID tinyint not null,
  47.     RAT_Rating varchar(42) not null,
  48.     Metadata_RAT int not null,
  49.     constraint pkRAT_Rating primary key (
  50.         RAT_ID asc
  51.     ),
  52.     constraint uqRAT_Rating unique (
  53.         RAT_Rating
  54.     )
  55. );
  56. GO
  57. -- Knot table ---------------------------------------------------------------------------------------------------------
  58. -- GEN_Gender table
  59. -----------------------------------------------------------------------------------------------------------------------
  60. IF Object_ID('GEN_Gender', 'U') IS NULL
  61. CREATE TABLE [dbo].[GEN_Gender] (
  62.     GEN_ID bit not null,
  63.     GEN_Gender varchar(42) not null,
  64.     Metadata_GEN int not null,
  65.     constraint pkGEN_Gender primary key (
  66.         GEN_ID asc
  67.     ),
  68.     constraint uqGEN_Gender unique (
  69.         GEN_Gender
  70.     )
  71. );
  72. GO
  73. -- ANCHORS AND ATTRIBUTES ---------------------------------------------------------------------------------------------
  74. --
  75. -- Anchors are used to store the identities of entities.
  76. -- Anchors are immutable.
  77. -- Attributes are used to store values for properties of entities.
  78. -- Attributes are mutable, their values may change over one or more types of time.
  79. -- Attributes have four flavors: static, historized, knotted static, and knotted historized.
  80. -- Anchors may have zero or more adjoined attributes.
  81. --
  82. -- Anchor table -------------------------------------------------------------------------------------------------------
  83. -- EV_Event table (with 0 attributes)
  84. -----------------------------------------------------------------------------------------------------------------------
  85. IF Object_ID('EV_Event', 'U') IS NULL
  86. CREATE TABLE [dbo].[EV_Event] (
  87.     EV_ID int IDENTITY(1,1) not null,
  88.     Metadata_EV int not null,
  89.     constraint pkEV_Event primary key (
  90.         EV_ID asc
  91.     )
  92. );
  93. GO
  94. -- Anchor table -------------------------------------------------------------------------------------------------------
  95. -- PR_Program table (with 1 attributes)
  96. -----------------------------------------------------------------------------------------------------------------------
  97. IF Object_ID('PR_Program', 'U') IS NULL
  98. CREATE TABLE [dbo].[PR_Program] (
  99.     PR_ID int IDENTITY(1,1) not null,
  100.     Metadata_PR int not null,
  101.     constraint pkPR_Program primary key (
  102.         PR_ID asc
  103.     )
  104. );
  105. GO
  106. -- Static attribute table ---------------------------------------------------------------------------------------------
  107. -- PR_NAM_Program_Name table (on PR_Program)
  108. -----------------------------------------------------------------------------------------------------------------------
  109. IF Object_ID('PR_NAM_Program_Name', 'U') IS NULL
  110. CREATE TABLE [dbo].[PR_NAM_Program_Name] (
  111.     PR_NAM_PR_ID int not null,
  112.     PR_NAM_Program_Name varchar(42) not null,
  113.     Metadata_PR_NAM int not null,
  114.     constraint fkPR_NAM_Program_Name foreign key (
  115.         PR_NAM_PR_ID
  116.     ) references [dbo].[PR_Program](PR_ID),
  117.     constraint pkPR_NAM_Program_Name primary key (
  118.         PR_NAM_PR_ID asc
  119.     )
  120. );
  121. GO
  122. -- Anchor table -------------------------------------------------------------------------------------------------------
  123. -- ST_Stage table (with 2 attributes)
  124. -----------------------------------------------------------------------------------------------------------------------
  125. IF Object_ID('ST_Stage', 'U') IS NULL
  126. CREATE TABLE [dbo].[ST_Stage] (
  127.     ST_ID int IDENTITY(1,1) not null,
  128.     Metadata_ST int not null,
  129.     constraint pkST_Stage primary key (
  130.         ST_ID asc
  131.     )
  132. );
  133. GO
  134. -- Historized attribute table -----------------------------------------------------------------------------------------
  135. -- ST_NAM_Stage_Name table (on ST_Stage)
  136. -----------------------------------------------------------------------------------------------------------------------
  137. IF Object_ID('ST_NAM_Stage_Name', 'U') IS NULL
  138. CREATE TABLE [dbo].[ST_NAM_Stage_Name] (
  139.     ST_NAM_ST_ID int not null,
  140.     ST_NAM_Stage_Name varchar(42) not null,
  141.     ST_NAM_ChangedAt datetime not null,
  142.     Metadata_ST_NAM int not null,
  143.     constraint fkST_NAM_Stage_Name foreign key (
  144.         ST_NAM_ST_ID
  145.     ) references [dbo].[ST_Stage](ST_ID),
  146.     constraint pkST_NAM_Stage_Name primary key (
  147.         ST_NAM_ST_ID asc,
  148.         ST_NAM_ChangedAt desc
  149.     )
  150. );
  151. GO
  152. -- Static attribute table ---------------------------------------------------------------------------------------------
  153. -- ST_LOC_Stage_Location table (on ST_Stage)
  154. -----------------------------------------------------------------------------------------------------------------------
  155. IF Object_ID('ST_LOC_Stage_Location', 'U') IS NULL
  156. CREATE TABLE [dbo].[ST_LOC_Stage_Location] (
  157.     ST_LOC_ST_ID int not null,
  158.     ST_LOC_Stage_Location varchar(42) not null,
  159.     Metadata_ST_LOC int not null,
  160.     constraint fkST_LOC_Stage_Location foreign key (
  161.         ST_LOC_ST_ID
  162.     ) references [dbo].[ST_Stage](ST_ID),
  163.     constraint pkST_LOC_Stage_Location primary key (
  164.         ST_LOC_ST_ID asc
  165.     )
  166. );
  167. GO
  168. -- Anchor table -------------------------------------------------------------------------------------------------------
  169. -- PE_Performance table (with 3 attributes)
  170. -----------------------------------------------------------------------------------------------------------------------
  171. IF Object_ID('PE_Performance', 'U') IS NULL
  172. CREATE TABLE [dbo].[PE_Performance] (
  173.     PE_ID int IDENTITY(1,1) not null,
  174.     Metadata_PE int not null,
  175.     constraint pkPE_Performance primary key (
  176.         PE_ID asc
  177.     )
  178. );
  179. GO
  180. -- Static attribute table ---------------------------------------------------------------------------------------------
  181. -- PE_DAT_Performance_Date table (on PE_Performance)
  182. -----------------------------------------------------------------------------------------------------------------------
  183. IF Object_ID('PE_DAT_Performance_Date', 'U') IS NULL
  184. CREATE TABLE [dbo].[PE_DAT_Performance_Date] (
  185.     PE_DAT_PE_ID int not null,
  186.     PE_DAT_Performance_Date datetime not null,
  187.     Metadata_PE_DAT int not null,
  188.     constraint fkPE_DAT_Performance_Date foreign key (
  189.         PE_DAT_PE_ID
  190.     ) references [dbo].[PE_Performance](PE_ID),
  191.     constraint pkPE_DAT_Performance_Date primary key (
  192.         PE_DAT_PE_ID asc
  193.     )
  194. );
  195. GO
  196. -- Static attribute table ---------------------------------------------------------------------------------------------
  197. -- PE_AUD_Performance_Audience table (on PE_Performance)
  198. -----------------------------------------------------------------------------------------------------------------------
  199. IF Object_ID('PE_AUD_Performance_Audience', 'U') IS NULL
  200. CREATE TABLE [dbo].[PE_AUD_Performance_Audience] (
  201.     PE_AUD_PE_ID int not null,
  202.     PE_AUD_Performance_Audience int not null,
  203.     Metadata_PE_AUD int not null,
  204.     constraint fkPE_AUD_Performance_Audience foreign key (
  205.         PE_AUD_PE_ID
  206.     ) references [dbo].[PE_Performance](PE_ID),
  207.     constraint pkPE_AUD_Performance_Audience primary key (
  208.         PE_AUD_PE_ID asc
  209.     )
  210. );
  211. GO
  212. -- Static attribute table ---------------------------------------------------------------------------------------------
  213. -- PE_REV_Performance_Revenue table (on PE_Performance)
  214. -----------------------------------------------------------------------------------------------------------------------
  215. IF Object_ID('PE_REV_Performance_Revenue', 'U') IS NULL
  216. CREATE TABLE [dbo].[PE_REV_Performance_Revenue] (
  217.     PE_REV_PE_ID int not null,
  218.     PE_REV_Performance_Revenue money not null,
  219.     Metadata_PE_REV int not null,
  220.     constraint fkPE_REV_Performance_Revenue foreign key (
  221.         PE_REV_PE_ID
  222.     ) references [dbo].[PE_Performance](PE_ID),
  223.     constraint pkPE_REV_Performance_Revenue primary key (
  224.         PE_REV_PE_ID asc
  225.     )
  226. );
  227. GO
  228. -- Anchor table -------------------------------------------------------------------------------------------------------
  229. -- AC_Actor table (with 3 attributes)
  230. -----------------------------------------------------------------------------------------------------------------------
  231. IF Object_ID('AC_Actor', 'U') IS NULL
  232. CREATE TABLE [dbo].[AC_Actor] (
  233.     AC_ID int IDENTITY(1,1) not null,
  234.     Metadata_AC int not null,
  235.     constraint pkAC_Actor primary key (
  236.         AC_ID asc
  237.     )
  238. );
  239. GO
  240. -- Historized attribute table -----------------------------------------------------------------------------------------
  241. -- AC_NAM_Actor_Name table (on AC_Actor)
  242. -----------------------------------------------------------------------------------------------------------------------
  243. IF Object_ID('AC_NAM_Actor_Name', 'U') IS NULL
  244. CREATE TABLE [dbo].[AC_NAM_Actor_Name] (
  245.     AC_NAM_AC_ID int not null,
  246.     AC_NAM_Actor_Name varchar(42) not null,
  247.     AC_NAM_ChangedAt datetime not null,
  248.     Metadata_AC_NAM int not null,
  249.     constraint fkAC_NAM_Actor_Name foreign key (
  250.         AC_NAM_AC_ID
  251.     ) references [dbo].[AC_Actor](AC_ID),
  252.     constraint pkAC_NAM_Actor_Name primary key (
  253.         AC_NAM_AC_ID asc,
  254.         AC_NAM_ChangedAt desc
  255.     )
  256. );
  257. GO
  258. -- Knotted static attribute table -------------------------------------------------------------------------------------
  259. -- AC_GEN_Actor_Gender table (on AC_Actor)
  260. -----------------------------------------------------------------------------------------------------------------------
  261. IF Object_ID('AC_GEN_Actor_Gender', 'U') IS NULL
  262. CREATE TABLE [dbo].[AC_GEN_Actor_Gender] (
  263.     AC_GEN_AC_ID int not null,
  264.     AC_GEN_GEN_ID bit not null,
  265.     Metadata_AC_GEN int not null,
  266.     constraint fk_A_AC_GEN_Actor_Gender foreign key (
  267.         AC_GEN_AC_ID
  268.     ) references [dbo].[AC_Actor](AC_ID),
  269.     constraint fk_K_AC_GEN_Actor_Gender foreign key (
  270.         AC_GEN_GEN_ID
  271.     ) references [dbo].[GEN_Gender](GEN_ID),
  272.     constraint pkAC_GEN_Actor_Gender primary key (
  273.         AC_GEN_AC_ID asc
  274.     )
  275. );
  276. GO
  277. -- Knotted historized attribute table ---------------------------------------------------------------------------------
  278. -- AC_PLV_Actor_ProfessionalLevel table (on AC_Actor)
  279. -----------------------------------------------------------------------------------------------------------------------
  280. IF Object_ID('AC_PLV_Actor_ProfessionalLevel', 'U') IS NULL
  281. CREATE TABLE [dbo].[AC_PLV_Actor_ProfessionalLevel] (
  282.     AC_PLV_AC_ID int not null,
  283.     AC_PLV_PLV_ID tinyint not null,
  284.     AC_PLV_ChangedAt datetime not null,
  285.     Metadata_AC_PLV int not null,
  286.     constraint fk_A_AC_PLV_Actor_ProfessionalLevel foreign key (
  287.         AC_PLV_AC_ID
  288.     ) references [dbo].[AC_Actor](AC_ID),
  289.     constraint fk_K_AC_PLV_Actor_ProfessionalLevel foreign key (
  290.         AC_PLV_PLV_ID
  291.     ) references [dbo].[PLV_ProfessionalLevel](PLV_ID),
  292.     constraint pkAC_PLV_Actor_ProfessionalLevel primary key (
  293.         AC_PLV_AC_ID asc,
  294.         AC_PLV_ChangedAt desc
  295.     )
  296. );
  297. GO
  298. -- TIES ---------------------------------------------------------------------------------------------------------------
  299. --
  300. -- Ties are used to represent relationships between entities.
  301. -- They come in four flavors: static, historized, knotted static, and knotted historized.
  302. -- Ties have cardinality, constraining how members may participate in the relationship.
  303. -- Every entity that is a member in a tie has a specified role in the relationship.
  304. -- Ties must have at least two anchor roles and zero or more knot roles.
  305. --
  306. -- Knotted static tie table -------------------------------------------------------------------------------------------
  307. -- AC_parent_AC_child_PAT_having table (having 3 roles)
  308. -----------------------------------------------------------------------------------------------------------------------
  309. IF Object_ID('AC_parent_AC_child_PAT_having', 'U') IS NULL
  310. CREATE TABLE [dbo].[AC_parent_AC_child_PAT_having] (
  311.     AC_ID_parent int not null,
  312.     AC_ID_child int not null,
  313.     PAT_ID_having tinyint not null,
  314.     Metadata_AC_parent_AC_child_PAT_having int not null,
  315.     constraint AC_parent_AC_child_PAT_having_fkAC_parent foreign key (
  316.         AC_ID_parent
  317.     ) references AC_Actor(AC_ID),
  318.     constraint AC_parent_AC_child_PAT_having_fkAC_child foreign key (
  319.         AC_ID_child
  320.     ) references AC_Actor(AC_ID),
  321.     constraint AC_parent_AC_child_PAT_having_fkPAT_having foreign key (
  322.         PAT_ID_having
  323.     ) references PAT_ParentalType(PAT_ID),
  324.     constraint pkAC_parent_AC_child_PAT_having primary key (
  325.         AC_ID_parent asc,
  326.         AC_ID_child asc,
  327.         PAT_ID_having asc
  328.     )
  329. );
  330. GO
  331. -- Historized tie table -----------------------------------------------------------------------------------------------
  332. -- ST_atLocation_PR_isPlaying table (having 2 roles)
  333. -----------------------------------------------------------------------------------------------------------------------
  334. IF Object_ID('ST_atLocation_PR_isPlaying', 'U') IS NULL
  335. CREATE TABLE [dbo].[ST_atLocation_PR_isPlaying] (
  336.     ST_ID_atLocation int not null,
  337.     PR_ID_isPlaying int not null,
  338.     ST_atLocation_PR_isPlaying_ChangedAt datetime not null,
  339.     Metadata_ST_atLocation_PR_isPlaying int not null,
  340.     constraint ST_atLocation_PR_isPlaying_fkST_atLocation foreign key (
  341.         ST_ID_atLocation
  342.     ) references ST_Stage(ST_ID),
  343.     constraint ST_atLocation_PR_isPlaying_fkPR_isPlaying foreign key (
  344.         PR_ID_isPlaying
  345.     ) references PR_Program(PR_ID),
  346.     constraint pkST_atLocation_PR_isPlaying primary key (
  347.         ST_ID_atLocation asc,
  348.         PR_ID_isPlaying asc,
  349.         ST_atLocation_PR_isPlaying_ChangedAt desc
  350.     )
  351. );
  352. GO
  353. -- Static tie table ---------------------------------------------------------------------------------------------------
  354. -- PE_at_PR_wasPlayed table (having 2 roles)
  355. -----------------------------------------------------------------------------------------------------------------------
  356. IF Object_ID('PE_at_PR_wasPlayed', 'U') IS NULL
  357. CREATE TABLE [dbo].[PE_at_PR_wasPlayed] (
  358.     PE_ID_at int not null,
  359.     PR_ID_wasPlayed int not null,
  360.     Metadata_PE_at_PR_wasPlayed int not null,
  361.     constraint PE_at_PR_wasPlayed_fkPE_at foreign key (
  362.         PE_ID_at
  363.     ) references PE_Performance(PE_ID),
  364.     constraint PE_at_PR_wasPlayed_fkPR_wasPlayed foreign key (
  365.         PR_ID_wasPlayed
  366.     ) references PR_Program(PR_ID),
  367.     constraint pkPE_at_PR_wasPlayed primary key (
  368.         PE_ID_at asc
  369.     )
  370. );
  371. GO
  372. -- Static tie table ---------------------------------------------------------------------------------------------------
  373. -- PE_wasHeld_ST_atLocation table (having 2 roles)
  374. -----------------------------------------------------------------------------------------------------------------------
  375. IF Object_ID('PE_wasHeld_ST_atLocation', 'U') IS NULL
  376. CREATE TABLE [dbo].[PE_wasHeld_ST_atLocation] (
  377.     PE_ID_wasHeld int not null,
  378.     ST_ID_atLocation int not null,
  379.     Metadata_PE_wasHeld_ST_atLocation int not null,
  380.     constraint PE_wasHeld_ST_atLocation_fkPE_wasHeld foreign key (
  381.         PE_ID_wasHeld
  382.     ) references PE_Performance(PE_ID),
  383.     constraint PE_wasHeld_ST_atLocation_fkST_atLocation foreign key (
  384.         ST_ID_atLocation
  385.     ) references ST_Stage(ST_ID),
  386.     constraint pkPE_wasHeld_ST_atLocation primary key (
  387.         PE_ID_wasHeld asc
  388.     )
  389. );
  390. GO
  391. -- Historized tie table -----------------------------------------------------------------------------------------------
  392. -- AC_exclusive_AC_with table (having 2 roles)
  393. -----------------------------------------------------------------------------------------------------------------------
  394. IF Object_ID('AC_exclusive_AC_with', 'U') IS NULL
  395. CREATE TABLE [dbo].[AC_exclusive_AC_with] (
  396.     AC_ID_exclusive int not null,
  397.     AC_ID_with int not null,
  398.     AC_exclusive_AC_with_ChangedAt datetime not null,
  399.     Metadata_AC_exclusive_AC_with int not null,
  400.     constraint AC_exclusive_AC_with_fkAC_exclusive foreign key (
  401.         AC_ID_exclusive
  402.     ) references AC_Actor(AC_ID),
  403.     constraint AC_exclusive_AC_with_fkAC_with foreign key (
  404.         AC_ID_with
  405.     ) references AC_Actor(AC_ID),
  406.     constraint AC_exclusive_AC_with_uqAC_exclusive unique (
  407.         AC_ID_exclusive,
  408.         AC_exclusive_AC_with_ChangedAt
  409.     ),
  410.     constraint AC_exclusive_AC_with_uqAC_with unique (
  411.         AC_ID_with,
  412.         AC_exclusive_AC_with_ChangedAt
  413.     ),
  414.     constraint pkAC_exclusive_AC_with primary key (
  415.         AC_ID_exclusive asc,
  416.         AC_ID_with asc,
  417.         AC_exclusive_AC_with_ChangedAt desc
  418.     )
  419. );
  420. GO
  421. -- Static tie table ---------------------------------------------------------------------------------------------------
  422. -- PE_subset_EV_of table (having 2 roles)
  423. -----------------------------------------------------------------------------------------------------------------------
  424. IF Object_ID('PE_subset_EV_of', 'U') IS NULL
  425. CREATE TABLE [dbo].[PE_subset_EV_of] (
  426.     PE_ID_subset int not null,
  427.     EV_ID_of int not null,
  428.     Metadata_PE_subset_EV_of int not null,
  429.     constraint PE_subset_EV_of_fkPE_subset foreign key (
  430.         PE_ID_subset
  431.     ) references PE_Performance(PE_ID),
  432.     constraint PE_subset_EV_of_fkEV_of foreign key (
  433.         EV_ID_of
  434.     ) references EV_Event(EV_ID),
  435.     constraint PE_subset_EV_of_uqPE_subset unique (
  436.         PE_ID_subset
  437.     ),
  438.     constraint PE_subset_EV_of_uqEV_of unique (
  439.         EV_ID_of
  440.     ),
  441.     constraint pkPE_subset_EV_of primary key (
  442.         PE_ID_subset asc,
  443.         EV_ID_of asc
  444.     )
  445. );
  446. GO
  447. -- Static tie table ---------------------------------------------------------------------------------------------------
  448. -- PE_in_AC_wasCast table (having 2 roles)
  449. -----------------------------------------------------------------------------------------------------------------------
  450. IF Object_ID('PE_in_AC_wasCast', 'U') IS NULL
  451. CREATE TABLE [dbo].[PE_in_AC_wasCast] (
  452.     PE_ID_in int not null,
  453.     AC_ID_wasCast int not null,
  454.     Metadata_PE_in_AC_wasCast int not null,
  455.     constraint PE_in_AC_wasCast_fkPE_in foreign key (
  456.         PE_ID_in
  457.     ) references PE_Performance(PE_ID),
  458.     constraint PE_in_AC_wasCast_fkAC_wasCast foreign key (
  459.         AC_ID_wasCast
  460.     ) references AC_Actor(AC_ID),
  461.     constraint pkPE_in_AC_wasCast primary key (
  462.         PE_ID_in asc,
  463.         AC_ID_wasCast asc
  464.     )
  465. );
  466. GO
  467. -- Knotted historized tie table ---------------------------------------------------------------------------------------
  468. -- AC_part_PR_in_RAT_got table (having 3 roles)
  469. -----------------------------------------------------------------------------------------------------------------------
  470. IF Object_ID('AC_part_PR_in_RAT_got', 'U') IS NULL
  471. CREATE TABLE [dbo].[AC_part_PR_in_RAT_got] (
  472.     AC_ID_part int not null,
  473.     PR_ID_in int not null,
  474.     RAT_ID_got tinyint not null,
  475.     AC_part_PR_in_RAT_got_ChangedAt datetime not null,
  476.     Metadata_AC_part_PR_in_RAT_got int not null,
  477.     constraint AC_part_PR_in_RAT_got_fkAC_part foreign key (
  478.         AC_ID_part
  479.     ) references AC_Actor(AC_ID),
  480.     constraint AC_part_PR_in_RAT_got_fkPR_in foreign key (
  481.         PR_ID_in
  482.     ) references PR_Program(PR_ID),
  483.     constraint AC_part_PR_in_RAT_got_fkRAT_got foreign key (
  484.         RAT_ID_got
  485.     ) references RAT_Rating(RAT_ID),
  486.     constraint pkAC_part_PR_in_RAT_got primary key (
  487.         AC_ID_part asc,
  488.         PR_ID_in asc,
  489.         AC_part_PR_in_RAT_got_ChangedAt desc
  490.     )
  491. );
  492. GO
  493. -- ATTRIBUTE RESTATEMENT CONSTRAINTS ----------------------------------------------------------------------------------
  494. --
  495. -- Attributes may be prevented from storing restatements.
  496. -- A restatement is when the same value occurs for two adjacent points
  497. -- in changing time.
  498. --
  499. -- returns 1 for at least one equal surrounding value, 0 for different surrounding values
  500. --
  501. -- @id the identity of the anchored entity
  502. -- @value the value of the attribute
  503. -- @changed the point in time from which this value shall represent a change
  504. --
  505. -- Restatement Finder Function and Constraint -------------------------------------------------------------------------
  506. -- rfST_NAM_Stage_Name restatement finder, also used by the insert and update triggers for idempotent attributes
  507. -- rcST_NAM_Stage_Name restatement constraint, with checking made by the finder function
  508. -----------------------------------------------------------------------------------------------------------------------
  509. IF Object_ID('rfST_NAM_Stage_Name', 'FN') IS NULL
  510. BEGIN
  511.     EXEC('
  512.    CREATE FUNCTION [dbo].[rfST_NAM_Stage_Name] (
  513.        @id int,
  514.        @value varchar(42),
  515.        @changed datetime
  516.    )
  517.    RETURNS tinyint AS
  518.    BEGIN RETURN (
  519.        CASE WHEN @value IN ((
  520.            SELECT TOP 1
  521.                pre.ST_NAM_Stage_Name
  522.            FROM
  523.                [dbo].[ST_NAM_Stage_Name] pre
  524.            WHERE
  525.                pre.ST_NAM_ST_ID = @id
  526.            AND
  527.                pre.ST_NAM_ChangedAt < @changed
  528.            ORDER BY
  529.                pre.ST_NAM_ChangedAt DESC
  530.        ),(
  531.            SELECT TOP 1
  532.                fol.ST_NAM_Stage_Name
  533.            FROM
  534.                [dbo].[ST_NAM_Stage_Name] fol
  535.            WHERE
  536.                fol.ST_NAM_ST_ID = @id
  537.            AND
  538.                fol.ST_NAM_ChangedAt > @changed
  539.            ORDER BY
  540.                fol.ST_NAM_ChangedAt ASC
  541.        ))
  542.        THEN 1
  543.        ELSE 0
  544.        END
  545.    );
  546.    END
  547.    ');
  548.     ALTER TABLE [dbo].[ST_NAM_Stage_Name]
  549.     ADD CONSTRAINT [rcST_NAM_Stage_Name] CHECK (
  550.         [dbo].[rfST_NAM_Stage_Name] (
  551.             ST_NAM_ST_ID,
  552.             ST_NAM_Stage_Name,
  553.             ST_NAM_ChangedAt
  554.         ) = 0
  555.     );
  556. END
  557. GO
  558. -- Restatement Finder Function and Constraint -------------------------------------------------------------------------
  559. -- rfAC_NAM_Actor_Name restatement finder, also used by the insert and update triggers for idempotent attributes
  560. -- rcAC_NAM_Actor_Name restatement constraint, with checking made by the finder function
  561. -----------------------------------------------------------------------------------------------------------------------
  562. IF Object_ID('rfAC_NAM_Actor_Name', 'FN') IS NULL
  563. BEGIN
  564.     EXEC('
  565.    CREATE FUNCTION [dbo].[rfAC_NAM_Actor_Name] (
  566.        @id int,
  567.        @value varchar(42),
  568.        @changed datetime
  569.    )
  570.    RETURNS tinyint AS
  571.    BEGIN RETURN (
  572.        CASE WHEN @value IN ((
  573.            SELECT TOP 1
  574.                pre.AC_NAM_Actor_Name
  575.            FROM
  576.                [dbo].[AC_NAM_Actor_Name] pre
  577.            WHERE
  578.                pre.AC_NAM_AC_ID = @id
  579.            AND
  580.                pre.AC_NAM_ChangedAt < @changed
  581.            ORDER BY
  582.                pre.AC_NAM_ChangedAt DESC
  583.        ),(
  584.            SELECT TOP 1
  585.                fol.AC_NAM_Actor_Name
  586.            FROM
  587.                [dbo].[AC_NAM_Actor_Name] fol
  588.            WHERE
  589.                fol.AC_NAM_AC_ID = @id
  590.            AND
  591.                fol.AC_NAM_ChangedAt > @changed
  592.            ORDER BY
  593.                fol.AC_NAM_ChangedAt ASC
  594.        ))
  595.        THEN 1
  596.        ELSE 0
  597.        END
  598.    );
  599.    END
  600.    ');
  601.     ALTER TABLE [dbo].[AC_NAM_Actor_Name]
  602.     ADD CONSTRAINT [rcAC_NAM_Actor_Name] CHECK (
  603.         [dbo].[rfAC_NAM_Actor_Name] (
  604.             AC_NAM_AC_ID,
  605.             AC_NAM_Actor_Name,
  606.             AC_NAM_ChangedAt
  607.         ) = 0
  608.     );
  609. END
  610. GO
  611. -- Restatement Finder Function and Constraint -------------------------------------------------------------------------
  612. -- rfAC_PLV_Actor_ProfessionalLevel restatement finder, also used by the insert and update triggers for idempotent attributes
  613. -- rcAC_PLV_Actor_ProfessionalLevel restatement constraint, with checking made by the finder function
  614. -----------------------------------------------------------------------------------------------------------------------
  615. IF Object_ID('rfAC_PLV_Actor_ProfessionalLevel', 'FN') IS NULL
  616. BEGIN
  617.     EXEC('
  618.    CREATE FUNCTION [dbo].[rfAC_PLV_Actor_ProfessionalLevel] (
  619.        @id int,
  620.        @value tinyint,
  621.        @changed datetime
  622.    )
  623.    RETURNS tinyint AS
  624.    BEGIN RETURN (
  625.        CASE WHEN @value IN ((
  626.            SELECT TOP 1
  627.                pre.AC_PLV_PLV_ID
  628.            FROM
  629.                [dbo].[AC_PLV_Actor_ProfessionalLevel] pre
  630.            WHERE
  631.                pre.AC_PLV_AC_ID = @id
  632.            AND
  633.                pre.AC_PLV_ChangedAt < @changed
  634.            ORDER BY
  635.                pre.AC_PLV_ChangedAt DESC
  636.        ),(
  637.            SELECT TOP 1
  638.                fol.AC_PLV_PLV_ID
  639.            FROM
  640.                [dbo].[AC_PLV_Actor_ProfessionalLevel] fol
  641.            WHERE
  642.                fol.AC_PLV_AC_ID = @id
  643.            AND
  644.                fol.AC_PLV_ChangedAt > @changed
  645.            ORDER BY
  646.                fol.AC_PLV_ChangedAt ASC
  647.        ))
  648.        THEN 1
  649.        ELSE 0
  650.        END
  651.    );
  652.    END
  653.    ');
  654.     ALTER TABLE [dbo].[AC_PLV_Actor_ProfessionalLevel]
  655.     ADD CONSTRAINT [rcAC_PLV_Actor_ProfessionalLevel] CHECK (
  656.         [dbo].[rfAC_PLV_Actor_ProfessionalLevel] (
  657.             AC_PLV_AC_ID,
  658.             AC_PLV_PLV_ID,
  659.             AC_PLV_ChangedAt
  660.         ) = 0
  661.     );
  662. END
  663. GO
  664. -- KEY GENERATORS -----------------------------------------------------------------------------------------------------
  665. --
  666. -- These stored procedures can be used to generate identities of entities.
  667. -- Corresponding anchors must have an incrementing identity column.
  668. --
  669. -- Key Generation Stored Procedure ------------------------------------------------------------------------------------
  670. -- kEV_Event identity by surrogate key generation stored procedure
  671. -----------------------------------------------------------------------------------------------------------------------
  672. IF Object_ID('kEV_Event', 'P') IS NULL
  673. BEGIN
  674.     EXEC('
  675.    CREATE PROCEDURE [dbo].[kEV_Event] (
  676.        @requestedNumberOfIdentities bigint,
  677.        @metadata int
  678.    ) AS
  679.    BEGIN
  680.        SET NOCOUNT ON;
  681.        IF @requestedNumberOfIdentities > 0
  682.        BEGIN
  683.            WITH idGenerator (idNumber) AS (
  684.                SELECT
  685.                    1
  686.                UNION ALL
  687.                SELECT
  688.                    idNumber + 1
  689.                FROM
  690.                    idGenerator
  691.                WHERE
  692.                    idNumber < @requestedNumberOfIdentities
  693.            )
  694.            INSERT INTO [dbo].[EV_Event] (
  695.                Metadata_EV
  696.            )
  697.            OUTPUT
  698.                inserted.EV_ID
  699.            SELECT
  700.                @metadata
  701.            FROM
  702.                idGenerator
  703.            OPTION (maxrecursion 0);
  704.        END
  705.    END
  706.    ');
  707. END
  708. GO
  709. -- Key Generation Stored Procedure ------------------------------------------------------------------------------------
  710. -- kPR_Program identity by surrogate key generation stored procedure
  711. -----------------------------------------------------------------------------------------------------------------------
  712. IF Object_ID('kPR_Program', 'P') IS NULL
  713. BEGIN
  714.     EXEC('
  715.    CREATE PROCEDURE [dbo].[kPR_Program] (
  716.        @requestedNumberOfIdentities bigint,
  717.        @metadata int
  718.    ) AS
  719.    BEGIN
  720.        SET NOCOUNT ON;
  721.        IF @requestedNumberOfIdentities > 0
  722.        BEGIN
  723.            WITH idGenerator (idNumber) AS (
  724.                SELECT
  725.                    1
  726.                UNION ALL
  727.                SELECT
  728.                    idNumber + 1
  729.                FROM
  730.                    idGenerator
  731.                WHERE
  732.                    idNumber < @requestedNumberOfIdentities
  733.            )
  734.            INSERT INTO [dbo].[PR_Program] (
  735.                Metadata_PR
  736.            )
  737.            OUTPUT
  738.                inserted.PR_ID
  739.            SELECT
  740.                @metadata
  741.            FROM
  742.                idGenerator
  743.            OPTION (maxrecursion 0);
  744.        END
  745.    END
  746.    ');
  747. END
  748. GO
  749. -- Key Generation Stored Procedure ------------------------------------------------------------------------------------
  750. -- kST_Stage identity by surrogate key generation stored procedure
  751. -----------------------------------------------------------------------------------------------------------------------
  752. IF Object_ID('kST_Stage', 'P') IS NULL
  753. BEGIN
  754.     EXEC('
  755.    CREATE PROCEDURE [dbo].[kST_Stage] (
  756.        @requestedNumberOfIdentities bigint,
  757.        @metadata int
  758.    ) AS
  759.    BEGIN
  760.        SET NOCOUNT ON;
  761.        IF @requestedNumberOfIdentities > 0
  762.        BEGIN
  763.            WITH idGenerator (idNumber) AS (
  764.                SELECT
  765.                    1
  766.                UNION ALL
  767.                SELECT
  768.                    idNumber + 1
  769.                FROM
  770.                    idGenerator
  771.                WHERE
  772.                    idNumber < @requestedNumberOfIdentities
  773.            )
  774.            INSERT INTO [dbo].[ST_Stage] (
  775.                Metadata_ST
  776.            )
  777.            OUTPUT
  778.                inserted.ST_ID
  779.            SELECT
  780.                @metadata
  781.            FROM
  782.                idGenerator
  783.            OPTION (maxrecursion 0);
  784.        END
  785.    END
  786.    ');
  787. END
  788. GO
  789. -- Key Generation Stored Procedure ------------------------------------------------------------------------------------
  790. -- kPE_Performance identity by surrogate key generation stored procedure
  791. -----------------------------------------------------------------------------------------------------------------------
  792. IF Object_ID('kPE_Performance', 'P') IS NULL
  793. BEGIN
  794.     EXEC('
  795.    CREATE PROCEDURE [dbo].[kPE_Performance] (
  796.        @requestedNumberOfIdentities bigint,
  797.        @metadata int
  798.    ) AS
  799.    BEGIN
  800.        SET NOCOUNT ON;
  801.        IF @requestedNumberOfIdentities > 0
  802.        BEGIN
  803.            WITH idGenerator (idNumber) AS (
  804.                SELECT
  805.                    1
  806.                UNION ALL
  807.                SELECT
  808.                    idNumber + 1
  809.                FROM
  810.                    idGenerator
  811.                WHERE
  812.                    idNumber < @requestedNumberOfIdentities
  813.            )
  814.            INSERT INTO [dbo].[PE_Performance] (
  815.                Metadata_PE
  816.            )
  817.            OUTPUT
  818.                inserted.PE_ID
  819.            SELECT
  820.                @metadata
  821.            FROM
  822.                idGenerator
  823.            OPTION (maxrecursion 0);
  824.        END
  825.    END
  826.    ');
  827. END
  828. GO
  829. -- Key Generation Stored Procedure ------------------------------------------------------------------------------------
  830. -- kAC_Actor identity by surrogate key generation stored procedure
  831. -----------------------------------------------------------------------------------------------------------------------
  832. IF Object_ID('kAC_Actor', 'P') IS NULL
  833. BEGIN
  834.     EXEC('
  835.    CREATE PROCEDURE [dbo].[kAC_Actor] (
  836.        @requestedNumberOfIdentities bigint,
  837.        @metadata int
  838.    ) AS
  839.    BEGIN
  840.        SET NOCOUNT ON;
  841.        IF @requestedNumberOfIdentities > 0
  842.        BEGIN
  843.            WITH idGenerator (idNumber) AS (
  844.                SELECT
  845.                    1
  846.                UNION ALL
  847.                SELECT
  848.                    idNumber + 1
  849.                FROM
  850.                    idGenerator
  851.                WHERE
  852.                    idNumber < @requestedNumberOfIdentities
  853.            )
  854.            INSERT INTO [dbo].[AC_Actor] (
  855.                Metadata_AC
  856.            )
  857.            OUTPUT
  858.                inserted.AC_ID
  859.            SELECT
  860.                @metadata
  861.            FROM
  862.                idGenerator
  863.            OPTION (maxrecursion 0);
  864.        END
  865.    END
  866.    ');
  867. END
  868. GO
  869. -- ATTRIBUTE REWINDERS ------------------------------------------------------------------------------------------------
  870. --
  871. -- These table valued functions rewind an attribute table to the given
  872. -- point in changing time. It does not pick a temporal perspective and
  873. -- instead shows all rows that have been in effect before that point
  874. -- in time.
  875. --
  876. -- @changingTimepoint the point in changing time to rewind to
  877. --
  878. -- Attribute rewinder -------------------------------------------------------------------------------------------------
  879. -- rST_NAM_Stage_Name rewinding over changing time function
  880. -----------------------------------------------------------------------------------------------------------------------
  881. IF Object_ID('rST_NAM_Stage_Name','IF') IS NULL
  882. BEGIN
  883.     EXEC('
  884.    CREATE FUNCTION [dbo].[rST_NAM_Stage_Name] (
  885.        @changingTimepoint datetime
  886.    )
  887.    RETURNS TABLE WITH SCHEMABINDING AS RETURN
  888.    SELECT
  889.        Metadata_ST_NAM,
  890.        ST_NAM_ST_ID,
  891.        ST_NAM_Stage_Name,
  892.        ST_NAM_ChangedAt
  893.    FROM
  894.        [dbo].[ST_NAM_Stage_Name]
  895.    WHERE
  896.        ST_NAM_ChangedAt <= @changingTimepoint;
  897.    ');
  898. END
  899. GO
  900. -- Attribute rewinder -------------------------------------------------------------------------------------------------
  901. -- rAC_NAM_Actor_Name rewinding over changing time function
  902. -----------------------------------------------------------------------------------------------------------------------
  903. IF Object_ID('rAC_NAM_Actor_Name','IF') IS NULL
  904. BEGIN
  905.     EXEC('
  906.    CREATE FUNCTION [dbo].[rAC_NAM_Actor_Name] (
  907.        @changingTimepoint datetime
  908.    )
  909.    RETURNS TABLE WITH SCHEMABINDING AS RETURN
  910.    SELECT
  911.        Metadata_AC_NAM,
  912.        AC_NAM_AC_ID,
  913.        AC_NAM_Actor_Name,
  914.        AC_NAM_ChangedAt
  915.    FROM
  916.        [dbo].[AC_NAM_Actor_Name]
  917.    WHERE
  918.        AC_NAM_ChangedAt <= @changingTimepoint;
  919.    ');
  920. END
  921. GO
  922. -- Attribute rewinder -------------------------------------------------------------------------------------------------
  923. -- rAC_PLV_Actor_ProfessionalLevel rewinding over changing time function
  924. -----------------------------------------------------------------------------------------------------------------------
  925. IF Object_ID('rAC_PLV_Actor_ProfessionalLevel','IF') IS NULL
  926. BEGIN
  927.     EXEC('
  928.    CREATE FUNCTION [dbo].[rAC_PLV_Actor_ProfessionalLevel] (
  929.        @changingTimepoint datetime
  930.    )
  931.    RETURNS TABLE WITH SCHEMABINDING AS RETURN
  932.    SELECT
  933.        Metadata_AC_PLV,
  934.        AC_PLV_AC_ID,
  935.        AC_PLV_PLV_ID,
  936.        AC_PLV_ChangedAt
  937.    FROM
  938.        [dbo].[AC_PLV_Actor_ProfessionalLevel]
  939.    WHERE
  940.        AC_PLV_ChangedAt <= @changingTimepoint;
  941.    ');
  942. END
  943. GO
  944. -- ANCHOR TEMPORAL PERSPECTIVES ---------------------------------------------------------------------------------------
  945. --
  946. -- These table valued functions simplify temporal querying by providing a temporal
  947. -- perspective of each anchor. There are four types of perspectives: latest,
  948. -- point-in-time, difference, and now. They also denormalize the anchor, its attributes,
  949. -- and referenced knots from sixth to third normal form.
  950. --
  951. -- The latest perspective shows the latest available information for each anchor.
  952. -- The now perspective shows the information as it is right now.
  953. -- The point-in-time perspective lets you travel through the information to the given timepoint.
  954. --
  955. -- @changingTimepoint the point in changing time to travel to
  956. --
  957. -- The difference perspective shows changes between the two given timepoints, and for
  958. -- changes in all or a selection of attributes.
  959. --
  960. -- @intervalStart the start of the interval for finding changes
  961. -- @intervalEnd the end of the interval for finding changes
  962. -- @selection a list of mnemonics for tracked attributes, ie 'MNE MON ICS', or null for all
  963. --
  964. -- Drop perspectives --------------------------------------------------------------------------------------------------
  965. IF Object_ID('dEV_Event', 'IF') IS NOT NULL
  966. DROP FUNCTION [dbo].[dEV_Event];
  967. IF Object_ID('nEV_Event', 'V') IS NOT NULL
  968. DROP VIEW [dbo].[nEV_Event];
  969. IF Object_ID('pEV_Event', 'IF') IS NOT NULL
  970. DROP FUNCTION [dbo].[pEV_Event];
  971. IF Object_ID('lEV_Event', 'V') IS NOT NULL
  972. DROP VIEW [dbo].[lEV_Event];
  973. GO
  974. -- Drop perspectives --------------------------------------------------------------------------------------------------
  975. IF Object_ID('dPR_Program', 'IF') IS NOT NULL
  976. DROP FUNCTION [dbo].[dPR_Program];
  977. IF Object_ID('nPR_Program', 'V') IS NOT NULL
  978. DROP VIEW [dbo].[nPR_Program];
  979. IF Object_ID('pPR_Program', 'IF') IS NOT NULL
  980. DROP FUNCTION [dbo].[pPR_Program];
  981. IF Object_ID('lPR_Program', 'V') IS NOT NULL
  982. DROP VIEW [dbo].[lPR_Program];
  983. GO
  984. -- Latest perspective -------------------------------------------------------------------------------------------------
  985. -- lPR_Program viewed by the latest available information (may include future versions)
  986. -----------------------------------------------------------------------------------------------------------------------
  987. CREATE VIEW [dbo].[lPR_Program] WITH SCHEMABINDING AS
  988. SELECT
  989.     [PR].PR_ID,
  990.     [PR].Metadata_PR,
  991.     [NAM].PR_NAM_PR_ID,
  992.     [NAM].Metadata_PR_NAM,
  993.     [NAM].PR_NAM_Program_Name
  994. FROM
  995.     [dbo].[PR_Program] [PR]
  996. LEFT JOIN
  997.     [dbo].[PR_NAM_Program_Name] [NAM]
  998. ON
  999.     [NAM].PR_NAM_PR_ID = [PR].PR_ID;
  1000. GO
  1001. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  1002. -- pPR_Program viewed as it was on the given timepoint
  1003. -----------------------------------------------------------------------------------------------------------------------
  1004. CREATE FUNCTION [dbo].[pPR_Program] (
  1005.     @changingTimepoint datetime2(7)
  1006. )
  1007. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  1008. SELECT
  1009.     [PR].PR_ID,
  1010.     [PR].Metadata_PR,
  1011.     [NAM].PR_NAM_PR_ID,
  1012.     [NAM].Metadata_PR_NAM,
  1013.     [NAM].PR_NAM_Program_Name
  1014. FROM
  1015.     [dbo].[PR_Program] [PR]
  1016. LEFT JOIN
  1017.     [dbo].[PR_NAM_Program_Name] [NAM]
  1018. ON
  1019.     [NAM].PR_NAM_PR_ID = [PR].PR_ID;
  1020. GO
  1021. -- Now perspective ----------------------------------------------------------------------------------------------------
  1022. -- nPR_Program viewed as it currently is (cannot include future versions)
  1023. -----------------------------------------------------------------------------------------------------------------------
  1024. CREATE VIEW [dbo].[nPR_Program]
  1025. AS
  1026. SELECT
  1027.     *
  1028. FROM
  1029.     [dbo].[pPR_Program](sysdatetime());
  1030. GO
  1031. -- Drop perspectives --------------------------------------------------------------------------------------------------
  1032. IF Object_ID('dST_Stage', 'IF') IS NOT NULL
  1033. DROP FUNCTION [dbo].[dST_Stage];
  1034. IF Object_ID('nST_Stage', 'V') IS NOT NULL
  1035. DROP VIEW [dbo].[nST_Stage];
  1036. IF Object_ID('pST_Stage', 'IF') IS NOT NULL
  1037. DROP FUNCTION [dbo].[pST_Stage];
  1038. IF Object_ID('lST_Stage', 'V') IS NOT NULL
  1039. DROP VIEW [dbo].[lST_Stage];
  1040. GO
  1041. -- Latest perspective -------------------------------------------------------------------------------------------------
  1042. -- lST_Stage viewed by the latest available information (may include future versions)
  1043. -----------------------------------------------------------------------------------------------------------------------
  1044. CREATE VIEW [dbo].[lST_Stage] WITH SCHEMABINDING AS
  1045. SELECT
  1046.     [ST].ST_ID,
  1047.     [ST].Metadata_ST,
  1048.     [NAM].ST_NAM_ST_ID,
  1049.     [NAM].Metadata_ST_NAM,
  1050.     [NAM].ST_NAM_ChangedAt,
  1051.     [NAM].ST_NAM_Stage_Name,
  1052.     [LOC].ST_LOC_ST_ID,
  1053.     [LOC].Metadata_ST_LOC,
  1054.     [LOC].ST_LOC_Stage_Location
  1055. FROM
  1056.     [dbo].[ST_Stage] [ST]
  1057. LEFT JOIN
  1058.     [dbo].[ST_NAM_Stage_Name] [NAM]
  1059. ON
  1060.     [NAM].ST_NAM_ST_ID = [ST].ST_ID
  1061. AND
  1062.     [NAM].ST_NAM_ChangedAt = (
  1063.         SELECT
  1064.             max(sub.ST_NAM_ChangedAt)
  1065.         FROM
  1066.             [dbo].[ST_NAM_Stage_Name] sub
  1067.         WHERE
  1068.             sub.ST_NAM_ST_ID = [ST].ST_ID
  1069.    )
  1070. LEFT JOIN
  1071.     [dbo].[ST_LOC_Stage_Location] [LOC]
  1072. ON
  1073.     [LOC].ST_LOC_ST_ID = [ST].ST_ID;
  1074. GO
  1075. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  1076. -- pST_Stage viewed as it was on the given timepoint
  1077. -----------------------------------------------------------------------------------------------------------------------
  1078. CREATE FUNCTION [dbo].[pST_Stage] (
  1079.     @changingTimepoint datetime2(7)
  1080. )
  1081. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  1082. SELECT
  1083.     [ST].ST_ID,
  1084.     [ST].Metadata_ST,
  1085.     [NAM].ST_NAM_ST_ID,
  1086.     [NAM].Metadata_ST_NAM,
  1087.     [NAM].ST_NAM_ChangedAt,
  1088.     [NAM].ST_NAM_Stage_Name,
  1089.     [LOC].ST_LOC_ST_ID,
  1090.     [LOC].Metadata_ST_LOC,
  1091.     [LOC].ST_LOC_Stage_Location
  1092. FROM
  1093.     [dbo].[ST_Stage] [ST]
  1094. LEFT JOIN
  1095.     [dbo].[rST_NAM_Stage_Name](@changingTimepoint) [NAM]
  1096. ON
  1097.     [NAM].ST_NAM_ST_ID = [ST].ST_ID
  1098. AND
  1099.     [NAM].ST_NAM_ChangedAt = (
  1100.         SELECT
  1101.             max(sub.ST_NAM_ChangedAt)
  1102.         FROM
  1103.             [dbo].[rST_NAM_Stage_Name](@changingTimepoint) sub
  1104.         WHERE
  1105.             sub.ST_NAM_ST_ID = [ST].ST_ID
  1106.    )
  1107. LEFT JOIN
  1108.     [dbo].[ST_LOC_Stage_Location] [LOC]
  1109. ON
  1110.     [LOC].ST_LOC_ST_ID = [ST].ST_ID;
  1111. GO
  1112. -- Now perspective ----------------------------------------------------------------------------------------------------
  1113. -- nST_Stage viewed as it currently is (cannot include future versions)
  1114. -----------------------------------------------------------------------------------------------------------------------
  1115. CREATE VIEW [dbo].[nST_Stage]
  1116. AS
  1117. SELECT
  1118.     *
  1119. FROM
  1120.     [dbo].[pST_Stage](sysdatetime());
  1121. GO
  1122. -- Difference perspective ---------------------------------------------------------------------------------------------
  1123. -- dST_Stage showing all differences between the given timepoints and optionally for a subset of attributes
  1124. -----------------------------------------------------------------------------------------------------------------------
  1125. CREATE FUNCTION [dbo].[dST_Stage] (
  1126.     @intervalStart datetime2(7),
  1127.     @intervalEnd datetime2(7),
  1128.     @selection varchar(max) = null
  1129. )
  1130. RETURNS TABLE AS RETURN
  1131. SELECT
  1132.     timepoints.inspectedTimepoint,
  1133.     [pST].*
  1134. FROM (
  1135.     SELECT DISTINCT
  1136.         ST_NAM_ChangedAt AS inspectedTimepoint
  1137.     FROM
  1138.         [dbo].[ST_NAM_Stage_Name]
  1139.     WHERE
  1140.         (@selection is null OR @selection like '%NAM%')
  1141.     AND
  1142.         ST_NAM_ChangedAt BETWEEN @intervalStart AND @intervalEnd
  1143. ) timepoints
  1144. CROSS APPLY
  1145.     [dbo].[pST_Stage](timepoints.inspectedTimepoint) [pST];
  1146. GO
  1147. -- Drop perspectives --------------------------------------------------------------------------------------------------
  1148. IF Object_ID('dPE_Performance', 'IF') IS NOT NULL
  1149. DROP FUNCTION [dbo].[dPE_Performance];
  1150. IF Object_ID('nPE_Performance', 'V') IS NOT NULL
  1151. DROP VIEW [dbo].[nPE_Performance];
  1152. IF Object_ID('pPE_Performance', 'IF') IS NOT NULL
  1153. DROP FUNCTION [dbo].[pPE_Performance];
  1154. IF Object_ID('lPE_Performance', 'V') IS NOT NULL
  1155. DROP VIEW [dbo].[lPE_Performance];
  1156. GO
  1157. -- Latest perspective -------------------------------------------------------------------------------------------------
  1158. -- lPE_Performance viewed by the latest available information (may include future versions)
  1159. -----------------------------------------------------------------------------------------------------------------------
  1160. CREATE VIEW [dbo].[lPE_Performance] WITH SCHEMABINDING AS
  1161. SELECT
  1162.     [PE].PE_ID,
  1163.     [PE].Metadata_PE,
  1164.     [DAT].PE_DAT_PE_ID,
  1165.     [DAT].Metadata_PE_DAT,
  1166.     [DAT].PE_DAT_Performance_Date,
  1167.     [AUD].PE_AUD_PE_ID,
  1168.     [AUD].Metadata_PE_AUD,
  1169.     [AUD].PE_AUD_Performance_Audience,
  1170.     [REV].PE_REV_PE_ID,
  1171.     [REV].Metadata_PE_REV,
  1172.     [REV].PE_REV_Performance_Revenue
  1173. FROM
  1174.     [dbo].[PE_Performance] [PE]
  1175. LEFT JOIN
  1176.     [dbo].[PE_DAT_Performance_Date] [DAT]
  1177. ON
  1178.     [DAT].PE_DAT_PE_ID = [PE].PE_ID
  1179. LEFT JOIN
  1180.     [dbo].[PE_AUD_Performance_Audience] [AUD]
  1181. ON
  1182.     [AUD].PE_AUD_PE_ID = [PE].PE_ID
  1183. LEFT JOIN
  1184.     [dbo].[PE_REV_Performance_Revenue] [REV]
  1185. ON
  1186.     [REV].PE_REV_PE_ID = [PE].PE_ID;
  1187. GO
  1188. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  1189. -- pPE_Performance viewed as it was on the given timepoint
  1190. -----------------------------------------------------------------------------------------------------------------------
  1191. CREATE FUNCTION [dbo].[pPE_Performance] (
  1192.     @changingTimepoint datetime2(7)
  1193. )
  1194. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  1195. SELECT
  1196.     [PE].PE_ID,
  1197.     [PE].Metadata_PE,
  1198.     [DAT].PE_DAT_PE_ID,
  1199.     [DAT].Metadata_PE_DAT,
  1200.     [DAT].PE_DAT_Performance_Date,
  1201.     [AUD].PE_AUD_PE_ID,
  1202.     [AUD].Metadata_PE_AUD,
  1203.     [AUD].PE_AUD_Performance_Audience,
  1204.     [REV].PE_REV_PE_ID,
  1205.     [REV].Metadata_PE_REV,
  1206.     [REV].PE_REV_Performance_Revenue
  1207. FROM
  1208.     [dbo].[PE_Performance] [PE]
  1209. LEFT JOIN
  1210.     [dbo].[PE_DAT_Performance_Date] [DAT]
  1211. ON
  1212.     [DAT].PE_DAT_PE_ID = [PE].PE_ID
  1213. LEFT JOIN
  1214.     [dbo].[PE_AUD_Performance_Audience] [AUD]
  1215. ON
  1216.     [AUD].PE_AUD_PE_ID = [PE].PE_ID
  1217. LEFT JOIN
  1218.     [dbo].[PE_REV_Performance_Revenue] [REV]
  1219. ON
  1220.     [REV].PE_REV_PE_ID = [PE].PE_ID;
  1221. GO
  1222. -- Now perspective ----------------------------------------------------------------------------------------------------
  1223. -- nPE_Performance viewed as it currently is (cannot include future versions)
  1224. -----------------------------------------------------------------------------------------------------------------------
  1225. CREATE VIEW [dbo].[nPE_Performance]
  1226. AS
  1227. SELECT
  1228.     *
  1229. FROM
  1230.     [dbo].[pPE_Performance](sysdatetime());
  1231. GO
  1232. -- Drop perspectives --------------------------------------------------------------------------------------------------
  1233. IF Object_ID('dAC_Actor', 'IF') IS NOT NULL
  1234. DROP FUNCTION [dbo].[dAC_Actor];
  1235. IF Object_ID('nAC_Actor', 'V') IS NOT NULL
  1236. DROP VIEW [dbo].[nAC_Actor];
  1237. IF Object_ID('pAC_Actor', 'IF') IS NOT NULL
  1238. DROP FUNCTION [dbo].[pAC_Actor];
  1239. IF Object_ID('lAC_Actor', 'V') IS NOT NULL
  1240. DROP VIEW [dbo].[lAC_Actor];
  1241. GO
  1242. -- Latest perspective -------------------------------------------------------------------------------------------------
  1243. -- lAC_Actor viewed by the latest available information (may include future versions)
  1244. -----------------------------------------------------------------------------------------------------------------------
  1245. CREATE VIEW [dbo].[lAC_Actor] WITH SCHEMABINDING AS
  1246. SELECT
  1247.     [AC].AC_ID,
  1248.     [AC].Metadata_AC,
  1249.     [NAM].AC_NAM_AC_ID,
  1250.     [NAM].Metadata_AC_NAM,
  1251.     [NAM].AC_NAM_ChangedAt,
  1252.     [NAM].AC_NAM_Actor_Name,
  1253.     [GEN].AC_GEN_AC_ID,
  1254.     [GEN].Metadata_AC_GEN,
  1255.     [kGEN].GEN_Gender AS AC_GEN_GEN_Gender,
  1256.     [kGEN].Metadata_GEN AS AC_GEN_Metadata_GEN,
  1257.     [GEN].AC_GEN_GEN_ID,
  1258.     [PLV].AC_PLV_AC_ID,
  1259.     [PLV].Metadata_AC_PLV,
  1260.     [PLV].AC_PLV_ChangedAt,
  1261.     [kPLV].PLV_ProfessionalLevel AS AC_PLV_PLV_ProfessionalLevel,
  1262.     [kPLV].Metadata_PLV AS AC_PLV_Metadata_PLV,
  1263.     [PLV].AC_PLV_PLV_ID
  1264. FROM
  1265.     [dbo].[AC_Actor] [AC]
  1266. LEFT JOIN
  1267.     [dbo].[AC_NAM_Actor_Name] [NAM]
  1268. ON
  1269.     [NAM].AC_NAM_AC_ID = [AC].AC_ID
  1270. AND
  1271.     [NAM].AC_NAM_ChangedAt = (
  1272.         SELECT
  1273.             max(sub.AC_NAM_ChangedAt)
  1274.         FROM
  1275.             [dbo].[AC_NAM_Actor_Name] sub
  1276.         WHERE
  1277.             sub.AC_NAM_AC_ID = [AC].AC_ID
  1278.    )
  1279. LEFT JOIN
  1280.     [dbo].[AC_GEN_Actor_Gender] [GEN]
  1281. ON
  1282.     [GEN].AC_GEN_AC_ID = [AC].AC_ID
  1283. LEFT JOIN
  1284.     [dbo].[GEN_Gender] [kGEN]
  1285. ON
  1286.     [kGEN].GEN_ID = [GEN].AC_GEN_GEN_ID
  1287. LEFT JOIN
  1288.     [dbo].[AC_PLV_Actor_ProfessionalLevel] [PLV]
  1289. ON
  1290.     [PLV].AC_PLV_AC_ID = [AC].AC_ID
  1291. AND
  1292.     [PLV].AC_PLV_ChangedAt = (
  1293.         SELECT
  1294.             max(sub.AC_PLV_ChangedAt)
  1295.         FROM
  1296.             [dbo].[AC_PLV_Actor_ProfessionalLevel] sub
  1297.         WHERE
  1298.             sub.AC_PLV_AC_ID = [AC].AC_ID
  1299.    )
  1300. LEFT JOIN
  1301.     [dbo].[PLV_ProfessionalLevel] [kPLV]
  1302. ON
  1303.     [kPLV].PLV_ID = [PLV].AC_PLV_PLV_ID;
  1304. GO
  1305. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  1306. -- pAC_Actor viewed as it was on the given timepoint
  1307. -----------------------------------------------------------------------------------------------------------------------
  1308. CREATE FUNCTION [dbo].[pAC_Actor] (
  1309.     @changingTimepoint datetime2(7)
  1310. )
  1311. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  1312. SELECT
  1313.     [AC].AC_ID,
  1314.     [AC].Metadata_AC,
  1315.     [NAM].AC_NAM_AC_ID,
  1316.     [NAM].Metadata_AC_NAM,
  1317.     [NAM].AC_NAM_ChangedAt,
  1318.     [NAM].AC_NAM_Actor_Name,
  1319.     [GEN].AC_GEN_AC_ID,
  1320.     [GEN].Metadata_AC_GEN,
  1321.     [kGEN].GEN_Gender AS AC_GEN_GEN_Gender,
  1322.     [kGEN].Metadata_GEN AS AC_GEN_Metadata_GEN,
  1323.     [GEN].AC_GEN_GEN_ID,
  1324.     [PLV].AC_PLV_AC_ID,
  1325.     [PLV].Metadata_AC_PLV,
  1326.     [PLV].AC_PLV_ChangedAt,
  1327.     [kPLV].PLV_ProfessionalLevel AS AC_PLV_PLV_ProfessionalLevel,
  1328.     [kPLV].Metadata_PLV AS AC_PLV_Metadata_PLV,
  1329.     [PLV].AC_PLV_PLV_ID
  1330. FROM
  1331.     [dbo].[AC_Actor] [AC]
  1332. LEFT JOIN
  1333.     [dbo].[rAC_NAM_Actor_Name](@changingTimepoint) [NAM]
  1334. ON
  1335.     [NAM].AC_NAM_AC_ID = [AC].AC_ID
  1336. AND
  1337.     [NAM].AC_NAM_ChangedAt = (
  1338.         SELECT
  1339.             max(sub.AC_NAM_ChangedAt)
  1340.         FROM
  1341.             [dbo].[rAC_NAM_Actor_Name](@changingTimepoint) sub
  1342.         WHERE
  1343.             sub.AC_NAM_AC_ID = [AC].AC_ID
  1344.    )
  1345. LEFT JOIN
  1346.     [dbo].[AC_GEN_Actor_Gender] [GEN]
  1347. ON
  1348.     [GEN].AC_GEN_AC_ID = [AC].AC_ID
  1349. LEFT JOIN
  1350.     [dbo].[GEN_Gender] [kGEN]
  1351. ON
  1352.     [kGEN].GEN_ID = [GEN].AC_GEN_GEN_ID
  1353. LEFT JOIN
  1354.     [dbo].[rAC_PLV_Actor_ProfessionalLevel](@changingTimepoint) [PLV]
  1355. ON
  1356.     [PLV].AC_PLV_AC_ID = [AC].AC_ID
  1357. AND
  1358.     [PLV].AC_PLV_ChangedAt = (
  1359.         SELECT
  1360.             max(sub.AC_PLV_ChangedAt)
  1361.         FROM
  1362.             [dbo].[rAC_PLV_Actor_ProfessionalLevel](@changingTimepoint) sub
  1363.         WHERE
  1364.             sub.AC_PLV_AC_ID = [AC].AC_ID
  1365.    )
  1366. LEFT JOIN
  1367.     [dbo].[PLV_ProfessionalLevel] [kPLV]
  1368. ON
  1369.     [kPLV].PLV_ID = [PLV].AC_PLV_PLV_ID;
  1370. GO
  1371. -- Now perspective ----------------------------------------------------------------------------------------------------
  1372. -- nAC_Actor viewed as it currently is (cannot include future versions)
  1373. -----------------------------------------------------------------------------------------------------------------------
  1374. CREATE VIEW [dbo].[nAC_Actor]
  1375. AS
  1376. SELECT
  1377.     *
  1378. FROM
  1379.     [dbo].[pAC_Actor](sysdatetime());
  1380. GO
  1381. -- Difference perspective ---------------------------------------------------------------------------------------------
  1382. -- dAC_Actor showing all differences between the given timepoints and optionally for a subset of attributes
  1383. -----------------------------------------------------------------------------------------------------------------------
  1384. CREATE FUNCTION [dbo].[dAC_Actor] (
  1385.     @intervalStart datetime2(7),
  1386.     @intervalEnd datetime2(7),
  1387.     @selection varchar(max) = null
  1388. )
  1389. RETURNS TABLE AS RETURN
  1390. SELECT
  1391.     timepoints.inspectedTimepoint,
  1392.     [pAC].*
  1393. FROM (
  1394.     SELECT DISTINCT
  1395.         AC_NAM_ChangedAt AS inspectedTimepoint
  1396.     FROM
  1397.         [dbo].[AC_NAM_Actor_Name]
  1398.     WHERE
  1399.         (@selection is null OR @selection like '%NAM%')
  1400.     AND
  1401.         AC_NAM_ChangedAt BETWEEN @intervalStart AND @intervalEnd
  1402.     UNION
  1403.     SELECT DISTINCT
  1404.         AC_PLV_ChangedAt AS inspectedTimepoint
  1405.     FROM
  1406.         [dbo].[AC_PLV_Actor_ProfessionalLevel]
  1407.     WHERE
  1408.         (@selection is null OR @selection like '%PLV%')
  1409.     AND
  1410.         AC_PLV_ChangedAt BETWEEN @intervalStart AND @intervalEnd
  1411. ) timepoints
  1412. CROSS APPLY
  1413.     [dbo].[pAC_Actor](timepoints.inspectedTimepoint) [pAC];
  1414. GO
  1415. -- ANCHOR TRIGGERS ---------------------------------------------------------------------------------------------------
  1416. --
  1417. -- The following triggers on the latest view make it behave like a table.
  1418. -- There are three different 'instead of' triggers: insert, update, and delete.
  1419. -- They will ensure that such operations are propagated to the underlying tables
  1420. -- in a consistent way. Default values are used for some columns if not provided
  1421. -- by the corresponding SQL statements.
  1422. --
  1423. -- For idempotent attributes, only changes that represent a value different from
  1424. -- the previous or following value are stored. Others are silently ignored in
  1425. -- order to avoid unnecessary temporal duplicates.
  1426. --
  1427. -- Insert trigger -----------------------------------------------------------------------------------------------------
  1428. -- itPR_Program instead of INSERT trigger on lPR_Program
  1429. -----------------------------------------------------------------------------------------------------------------------
  1430. CREATE TRIGGER [dbo].[itPR_Program] ON [dbo].[lPR_Program]
  1431. INSTEAD OF INSERT
  1432. AS
  1433. BEGIN
  1434.     SET NOCOUNT ON;
  1435.     DECLARE @now datetime2(7) = sysdatetime();
  1436.     DECLARE @maxVersion int;
  1437.     DECLARE @currentVersion int;
  1438.     DECLARE @PR TABLE (
  1439.         Row bigint IDENTITY(1,1) not null primary key,
  1440.         PR_ID int not null
  1441.     );
  1442.     INSERT INTO [dbo].[PR_Program] (
  1443.         Metadata_PR
  1444.     )
  1445.     OUTPUT
  1446.         inserted.PR_ID
  1447.     INTO
  1448.         @PR
  1449.     SELECT
  1450.         Metadata_PR
  1451.     FROM
  1452.         inserted
  1453.     WHERE
  1454.         inserted.PR_ID is null;
  1455.     DECLARE @inserted TABLE (
  1456.         PR_ID int not null,
  1457.         Metadata_PR int not null,
  1458.         PR_NAM_PR_ID int null,
  1459.         Metadata_PR_NAM int null,
  1460.         PR_NAM_Program_Name varchar(42) null
  1461.     );
  1462.     INSERT INTO @inserted
  1463.     SELECT
  1464.         ISNULL(i.PR_ID, a.PR_ID),
  1465.         i.Metadata_PR,
  1466.         ISNULL(ISNULL(i.PR_NAM_PR_ID, i.PR_ID), a.PR_ID),
  1467.         ISNULL(i.Metadata_PR_NAM, i.Metadata_PR),
  1468.         i.PR_NAM_Program_Name
  1469.     FROM (
  1470.         SELECT
  1471.             PR_ID,
  1472.             Metadata_PR,
  1473.             PR_NAM_PR_ID,
  1474.             Metadata_PR_NAM,
  1475.             PR_NAM_Program_Name,
  1476.             ROW_NUMBER() OVER (PARTITION BY PR_ID ORDER BY PR_ID) AS Row
  1477.         FROM
  1478.             inserted
  1479.     ) i
  1480.     LEFT JOIN
  1481.         @PR a
  1482.     ON
  1483.         a.Row = i.Row;
  1484.     INSERT INTO [dbo].[PR_NAM_Program_Name] (
  1485.         PR_NAM_PR_ID,
  1486.         Metadata_PR_NAM,
  1487.         PR_NAM_Program_Name
  1488.     )
  1489.     SELECT
  1490.         i.PR_NAM_PR_ID,
  1491.         i.Metadata_PR_NAM,
  1492.         i.PR_NAM_Program_Name
  1493.     FROM
  1494.         @inserted i
  1495.     LEFT JOIN
  1496.         [dbo].[PR_NAM_Program_Name] [NAM]
  1497.     ON
  1498.         [NAM].PR_NAM_PR_ID = i.PR_NAM_PR_ID
  1499.     WHERE
  1500.         [NAM].PR_NAM_PR_ID is null
  1501.     AND
  1502.         i.PR_NAM_Program_Name is not null;
  1503. END
  1504. GO
  1505. -- Insert trigger -----------------------------------------------------------------------------------------------------
  1506. -- itST_Stage instead of INSERT trigger on lST_Stage
  1507. -----------------------------------------------------------------------------------------------------------------------
  1508. CREATE TRIGGER [dbo].[itST_Stage] ON [dbo].[lST_Stage]
  1509. INSTEAD OF INSERT
  1510. AS
  1511. BEGIN
  1512.     SET NOCOUNT ON;
  1513.     DECLARE @now datetime2(7) = sysdatetime();
  1514.     DECLARE @maxVersion int;
  1515.     DECLARE @currentVersion int;
  1516.     DECLARE @ST TABLE (
  1517.         Row bigint IDENTITY(1,1) not null primary key,
  1518.         ST_ID int not null
  1519.     );
  1520.     INSERT INTO [dbo].[ST_Stage] (
  1521.         Metadata_ST
  1522.     )
  1523.     OUTPUT
  1524.         inserted.ST_ID
  1525.     INTO
  1526.         @ST
  1527.     SELECT
  1528.         Metadata_ST
  1529.     FROM
  1530.         inserted
  1531.     WHERE
  1532.         inserted.ST_ID is null;
  1533.     DECLARE @inserted TABLE (
  1534.         ST_ID int not null,
  1535.         Metadata_ST int not null,
  1536.         ST_NAM_ST_ID int null,
  1537.         Metadata_ST_NAM int null,
  1538.         ST_NAM_ChangedAt datetime null,
  1539.         ST_NAM_Stage_Name varchar(42) null,
  1540.         ST_LOC_ST_ID int null,
  1541.         Metadata_ST_LOC int null,
  1542.         ST_LOC_Stage_Location varchar(42) null
  1543.     );
  1544.     INSERT INTO @inserted
  1545.     SELECT
  1546.         ISNULL(i.ST_ID, a.ST_ID),
  1547.         i.Metadata_ST,
  1548.         ISNULL(ISNULL(i.ST_NAM_ST_ID, i.ST_ID), a.ST_ID),
  1549.         ISNULL(i.Metadata_ST_NAM, i.Metadata_ST),
  1550.         ISNULL(i.ST_NAM_ChangedAt, @now),
  1551.         i.ST_NAM_Stage_Name,
  1552.         ISNULL(ISNULL(i.ST_LOC_ST_ID, i.ST_ID), a.ST_ID),
  1553.         ISNULL(i.Metadata_ST_LOC, i.Metadata_ST),
  1554.         i.ST_LOC_Stage_Location
  1555.     FROM (
  1556.         SELECT
  1557.             ST_ID,
  1558.             Metadata_ST,
  1559.             ST_NAM_ST_ID,
  1560.             Metadata_ST_NAM,
  1561.             ST_NAM_ChangedAt,
  1562.             ST_NAM_Stage_Name,
  1563.             ST_LOC_ST_ID,
  1564.             Metadata_ST_LOC,
  1565.             ST_LOC_Stage_Location,
  1566.             ROW_NUMBER() OVER (PARTITION BY ST_ID ORDER BY ST_ID) AS Row
  1567.         FROM
  1568.             inserted
  1569.     ) i
  1570.     LEFT JOIN
  1571.         @ST a
  1572.     ON
  1573.         a.Row = i.Row;
  1574.     DECLARE @ST_NAM_Stage_Name TABLE (
  1575.         ST_NAM_ST_ID int not null,
  1576.         Metadata_ST_NAM int not null,
  1577.         ST_NAM_ChangedAt datetime not null,
  1578.         ST_NAM_Stage_Name varchar(42) not null,
  1579.         ST_NAM_Version bigint not null,
  1580.         ST_NAM_StatementType char(1) not null,
  1581.         primary key(
  1582.             ST_NAM_Version,
  1583.             ST_NAM_ST_ID
  1584.         )
  1585.     );
  1586.     INSERT INTO @ST_NAM_Stage_Name
  1587.     SELECT
  1588.         i.ST_NAM_ST_ID,
  1589.         i.Metadata_ST_NAM,
  1590.         i.ST_NAM_ChangedAt,
  1591.         i.ST_NAM_Stage_Name,
  1592.         DENSE_RANK() OVER (PARTITION BY i.ST_NAM_ST_ID ORDER BY i.ST_NAM_ChangedAt),
  1593.         'X'
  1594.     FROM
  1595.         @inserted i
  1596.     WHERE
  1597.         i.ST_NAM_Stage_Name is not null;
  1598.     SELECT
  1599.         @maxVersion = max(ST_NAM_Version),
  1600.         @currentVersion = 0
  1601.     FROM
  1602.         @ST_NAM_Stage_Name;
  1603.     WHILE (@currentVersion < @maxVersion)
  1604.     BEGIN
  1605.         SET @currentVersion = @currentVersion + 1;
  1606.         UPDATE v
  1607.         SET
  1608.             v.ST_NAM_StatementType =
  1609.                 CASE
  1610.                     WHEN [NAM].ST_NAM_ST_ID is not null
  1611.                     THEN 'D' -- duplicate
  1612.                     WHEN [dbo].[rfST_NAM_Stage_Name](
  1613.                         v.ST_NAM_ST_ID,
  1614.                         v.ST_NAM_Stage_Name,
  1615.                         v.ST_NAM_ChangedAt
  1616.                     ) = 1
  1617.                     THEN 'R' -- restatement
  1618.                     ELSE 'N' -- new statement
  1619.                 END
  1620.         FROM
  1621.             @ST_NAM_Stage_Name v
  1622.         LEFT JOIN
  1623.             [dbo].[ST_NAM_Stage_Name] [NAM]
  1624.         ON
  1625.             [NAM].ST_NAM_ST_ID = v.ST_NAM_ST_ID
  1626.         AND
  1627.             [NAM].ST_NAM_ChangedAt = v.ST_NAM_ChangedAt
  1628.         AND
  1629.             [NAM].ST_NAM_Stage_Name = v.ST_NAM_Stage_Name
  1630.         WHERE
  1631.             v.ST_NAM_Version = @currentVersion;
  1632.         INSERT INTO [dbo].[ST_NAM_Stage_Name] (
  1633.             ST_NAM_ST_ID,
  1634.             Metadata_ST_NAM,
  1635.             ST_NAM_ChangedAt,
  1636.             ST_NAM_Stage_Name
  1637.         )
  1638.         SELECT
  1639.             ST_NAM_ST_ID,
  1640.             Metadata_ST_NAM,
  1641.             ST_NAM_ChangedAt,
  1642.             ST_NAM_Stage_Name
  1643.         FROM
  1644.             @ST_NAM_Stage_Name
  1645.         WHERE
  1646.             ST_NAM_Version = @currentVersion
  1647.         AND
  1648.             ST_NAM_StatementType in ('N');
  1649.     END
  1650.     INSERT INTO [dbo].[ST_LOC_Stage_Location] (
  1651.         ST_LOC_ST_ID,
  1652.         Metadata_ST_LOC,
  1653.         ST_LOC_Stage_Location
  1654.     )
  1655.     SELECT
  1656.         i.ST_LOC_ST_ID,
  1657.         i.Metadata_ST_LOC,
  1658.         i.ST_LOC_Stage_Location
  1659.     FROM
  1660.         @inserted i
  1661.     LEFT JOIN
  1662.         [dbo].[ST_LOC_Stage_Location] [LOC]
  1663.     ON
  1664.         [LOC].ST_LOC_ST_ID = i.ST_LOC_ST_ID
  1665.     WHERE
  1666.         [LOC].ST_LOC_ST_ID is null
  1667.     AND
  1668.         i.ST_LOC_Stage_Location is not null;
  1669. END
  1670. GO
  1671. -- UPDATE trigger -----------------------------------------------------------------------------------------------------
  1672. -- utST_Stage instead of UPDATE trigger on lST_Stage
  1673. -----------------------------------------------------------------------------------------------------------------------
  1674. CREATE TRIGGER [dbo].[utST_Stage] ON [dbo].[lST_Stage]
  1675. INSTEAD OF UPDATE
  1676. AS
  1677. BEGIN
  1678.     SET NOCOUNT ON;
  1679.     DECLARE @now datetime2(7) = sysdatetime();
  1680.     IF(UPDATE(ST_ID))
  1681.         RAISERROR('The identity column ST_ID is not updatable.', 16, 1);
  1682.     IF(UPDATE(ST_NAM_Stage_Name))
  1683.     INSERT INTO [dbo].[ST_NAM_Stage_Name] (
  1684.         ST_NAM_ST_ID,
  1685.         Metadata_ST_NAM,
  1686.         ST_NAM_ChangedAt,
  1687.         ST_NAM_Stage_Name
  1688.     )
  1689.     SELECT
  1690.         i.ST_NAM_ST_ID,
  1691.         CASE WHEN UPDATE(Metadata_ST_NAM) THEN i.Metadata_ST_NAM ELSE 0 END,
  1692.         CASE WHEN UPDATE(ST_NAM_ChangedAt) THEN i.ST_NAM_ChangedAt ELSE @now END,
  1693.         i.ST_NAM_Stage_Name
  1694.     FROM
  1695.         inserted i
  1696.     LEFT JOIN
  1697.         [dbo].[ST_NAM_Stage_Name] b
  1698.     ON
  1699.         b.ST_NAM_ST_ID = i.ST_NAM_ST_ID
  1700.     AND
  1701.         b.ST_NAM_Stage_Name = i.ST_NAM_Stage_Name
  1702.     AND
  1703.         b.ST_NAM_ChangedAt = i.ST_NAM_ChangedAt
  1704.     WHERE
  1705.         b.ST_NAM_ST_ID is null
  1706.     AND
  1707.         [dbo].[rfST_NAM_Stage_Name](
  1708.             i.ST_NAM_ST_ID,
  1709.             i.ST_NAM_Stage_Name,
  1710.             i.ST_NAM_ChangedAt
  1711.         ) = 0;
  1712. END
  1713. GO
  1714. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  1715. -- dtST_Stage instead of DELETE trigger on lST_Stage
  1716. -----------------------------------------------------------------------------------------------------------------------
  1717. CREATE TRIGGER [dbo].[dtST_Stage] ON [dbo].[lST_Stage]
  1718. INSTEAD OF DELETE
  1719. AS
  1720. BEGIN
  1721.     SET NOCOUNT ON;
  1722.     DELETE [NAM]
  1723.     FROM
  1724.         [dbo].[ST_NAM_Stage_Name] [NAM]
  1725.     JOIN
  1726.         deleted d
  1727.     ON
  1728.         d.ST_NAM_ST_ID = [NAM].ST_NAM_ST_ID
  1729.     AND
  1730.         d.ST_NAM_ChangedAt = [NAM].ST_NAM_ChangedAt;
  1731.     DELETE [LOC]
  1732.     FROM
  1733.         [dbo].[ST_LOC_Stage_Location] [LOC]
  1734.     JOIN
  1735.         deleted d
  1736.     ON
  1737.         d.ST_LOC_ST_ID = [LOC].ST_LOC_ST_ID
  1738.     DELETE [ST]
  1739.     FROM
  1740.         [dbo].[ST_Stage] [ST]
  1741.     LEFT JOIN
  1742.         [dbo].[ST_NAM_Stage_Name] [NAM]
  1743.     ON
  1744.         [NAM].ST_NAM_ST_ID = [ST].ST_ID
  1745.     LEFT JOIN
  1746.         [dbo].[ST_LOC_Stage_Location] [LOC]
  1747.     ON
  1748.         [LOC].ST_LOC_ST_ID = [ST].ST_ID
  1749.     WHERE
  1750.         [NAM].ST_NAM_ST_ID is null
  1751.     AND
  1752.         [LOC].ST_LOC_ST_ID is null;
  1753. END
  1754. GO
  1755. -- Insert trigger -----------------------------------------------------------------------------------------------------
  1756. -- itPE_Performance instead of INSERT trigger on lPE_Performance
  1757. -----------------------------------------------------------------------------------------------------------------------
  1758. CREATE TRIGGER [dbo].[itPE_Performance] ON [dbo].[lPE_Performance]
  1759. INSTEAD OF INSERT
  1760. AS
  1761. BEGIN
  1762.     SET NOCOUNT ON;
  1763.     DECLARE @now datetime2(7) = sysdatetime();
  1764.     DECLARE @maxVersion int;
  1765.     DECLARE @currentVersion int;
  1766.     DECLARE @PE TABLE (
  1767.         Row bigint IDENTITY(1,1) not null primary key,
  1768.         PE_ID int not null
  1769.     );
  1770.     INSERT INTO [dbo].[PE_Performance] (
  1771.         Metadata_PE
  1772.     )
  1773.     OUTPUT
  1774.         inserted.PE_ID
  1775.     INTO
  1776.         @PE
  1777.     SELECT
  1778.         Metadata_PE
  1779.     FROM
  1780.         inserted
  1781.     WHERE
  1782.         inserted.PE_ID is null;
  1783.     DECLARE @inserted TABLE (
  1784.         PE_ID int not null,
  1785.         Metadata_PE int not null,
  1786.         PE_DAT_PE_ID int null,
  1787.         Metadata_PE_DAT int null,
  1788.         PE_DAT_Performance_Date datetime null,
  1789.         PE_AUD_PE_ID int null,
  1790.         Metadata_PE_AUD int null,
  1791.         PE_AUD_Performance_Audience int null,
  1792.         PE_REV_PE_ID int null,
  1793.         Metadata_PE_REV int null,
  1794.         PE_REV_Performance_Revenue money null
  1795.     );
  1796.     INSERT INTO @inserted
  1797.     SELECT
  1798.         ISNULL(i.PE_ID, a.PE_ID),
  1799.         i.Metadata_PE,
  1800.         ISNULL(ISNULL(i.PE_DAT_PE_ID, i.PE_ID), a.PE_ID),
  1801.         ISNULL(i.Metadata_PE_DAT, i.Metadata_PE),
  1802.         i.PE_DAT_Performance_Date,
  1803.         ISNULL(ISNULL(i.PE_AUD_PE_ID, i.PE_ID), a.PE_ID),
  1804.         ISNULL(i.Metadata_PE_AUD, i.Metadata_PE),
  1805.         i.PE_AUD_Performance_Audience,
  1806.         ISNULL(ISNULL(i.PE_REV_PE_ID, i.PE_ID), a.PE_ID),
  1807.         ISNULL(i.Metadata_PE_REV, i.Metadata_PE),
  1808.         i.PE_REV_Performance_Revenue
  1809.     FROM (
  1810.         SELECT
  1811.             PE_ID,
  1812.             Metadata_PE,
  1813.             PE_DAT_PE_ID,
  1814.             Metadata_PE_DAT,
  1815.             PE_DAT_Performance_Date,
  1816.             PE_AUD_PE_ID,
  1817.             Metadata_PE_AUD,
  1818.             PE_AUD_Performance_Audience,
  1819.             PE_REV_PE_ID,
  1820.             Metadata_PE_REV,
  1821.             PE_REV_Performance_Revenue,
  1822.             ROW_NUMBER() OVER (PARTITION BY PE_ID ORDER BY PE_ID) AS Row
  1823.         FROM
  1824.             inserted
  1825.     ) i
  1826.     LEFT JOIN
  1827.         @PE a
  1828.     ON
  1829.         a.Row = i.Row;
  1830.     INSERT INTO [dbo].[PE_DAT_Performance_Date] (
  1831.         PE_DAT_PE_ID,
  1832.         Metadata_PE_DAT,
  1833.         PE_DAT_Performance_Date
  1834.     )
  1835.     SELECT
  1836.         i.PE_DAT_PE_ID,
  1837.         i.Metadata_PE_DAT,
  1838.         i.PE_DAT_Performance_Date
  1839.     FROM
  1840.         @inserted i
  1841.     LEFT JOIN
  1842.         [dbo].[PE_DAT_Performance_Date] [DAT]
  1843.     ON
  1844.         [DAT].PE_DAT_PE_ID = i.PE_DAT_PE_ID
  1845.     WHERE
  1846.         [DAT].PE_DAT_PE_ID is null
  1847.     AND
  1848.         i.PE_DAT_Performance_Date is not null;
  1849.     INSERT INTO [dbo].[PE_AUD_Performance_Audience] (
  1850.         PE_AUD_PE_ID,
  1851.         Metadata_PE_AUD,
  1852.         PE_AUD_Performance_Audience
  1853.     )
  1854.     SELECT
  1855.         i.PE_AUD_PE_ID,
  1856.         i.Metadata_PE_AUD,
  1857.         i.PE_AUD_Performance_Audience
  1858.     FROM
  1859.         @inserted i
  1860.     LEFT JOIN
  1861.         [dbo].[PE_AUD_Performance_Audience] [AUD]
  1862.     ON
  1863.         [AUD].PE_AUD_PE_ID = i.PE_AUD_PE_ID
  1864.     WHERE
  1865.         [AUD].PE_AUD_PE_ID is null
  1866.     AND
  1867.         i.PE_AUD_Performance_Audience is not null;
  1868.     INSERT INTO [dbo].[PE_REV_Performance_Revenue] (
  1869.         PE_REV_PE_ID,
  1870.         Metadata_PE_REV,
  1871.         PE_REV_Performance_Revenue
  1872.     )
  1873.     SELECT
  1874.         i.PE_REV_PE_ID,
  1875.         i.Metadata_PE_REV,
  1876.         i.PE_REV_Performance_Revenue
  1877.     FROM
  1878.         @inserted i
  1879.     LEFT JOIN
  1880.         [dbo].[PE_REV_Performance_Revenue] [REV]
  1881.     ON
  1882.         [REV].PE_REV_PE_ID = i.PE_REV_PE_ID
  1883.     WHERE
  1884.         [REV].PE_REV_PE_ID is null
  1885.     AND
  1886.         i.PE_REV_Performance_Revenue is not null;
  1887. END
  1888. GO
  1889. -- Insert trigger -----------------------------------------------------------------------------------------------------
  1890. -- itAC_Actor instead of INSERT trigger on lAC_Actor
  1891. -----------------------------------------------------------------------------------------------------------------------
  1892. CREATE TRIGGER [dbo].[itAC_Actor] ON [dbo].[lAC_Actor]
  1893. INSTEAD OF INSERT
  1894. AS
  1895. BEGIN
  1896.     SET NOCOUNT ON;
  1897.     DECLARE @now datetime2(7) = sysdatetime();
  1898.     DECLARE @maxVersion int;
  1899.     DECLARE @currentVersion int;
  1900.     DECLARE @AC TABLE (
  1901.         Row bigint IDENTITY(1,1) not null primary key,
  1902.         AC_ID int not null
  1903.     );
  1904.     INSERT INTO [dbo].[AC_Actor] (
  1905.         Metadata_AC
  1906.     )
  1907.     OUTPUT
  1908.         inserted.AC_ID
  1909.     INTO
  1910.         @AC
  1911.     SELECT
  1912.         Metadata_AC
  1913.     FROM
  1914.         inserted
  1915.     WHERE
  1916.         inserted.AC_ID is null;
  1917.     DECLARE @inserted TABLE (
  1918.         AC_ID int not null,
  1919.         Metadata_AC int not null,
  1920.         AC_NAM_AC_ID int null,
  1921.         Metadata_AC_NAM int null,
  1922.         AC_NAM_ChangedAt datetime null,
  1923.         AC_NAM_Actor_Name varchar(42) null,
  1924.         AC_GEN_AC_ID int null,
  1925.         Metadata_AC_GEN int null,
  1926.         AC_GEN_GEN_Gender varchar(42) null,
  1927.         AC_GEN_Metadata_GEN int null,
  1928.         AC_GEN_GEN_ID bit null,
  1929.         AC_PLV_AC_ID int null,
  1930.         Metadata_AC_PLV int null,
  1931.         AC_PLV_ChangedAt datetime null,
  1932.         AC_PLV_PLV_ProfessionalLevel varchar(42) null,
  1933.         AC_PLV_Metadata_PLV int null,
  1934.         AC_PLV_PLV_ID tinyint null
  1935.     );
  1936.     INSERT INTO @inserted
  1937.     SELECT
  1938.         ISNULL(i.AC_ID, a.AC_ID),
  1939.         i.Metadata_AC,
  1940.         ISNULL(ISNULL(i.AC_NAM_AC_ID, i.AC_ID), a.AC_ID),
  1941.         ISNULL(i.Metadata_AC_NAM, i.Metadata_AC),
  1942.         ISNULL(i.AC_NAM_ChangedAt, @now),
  1943.         i.AC_NAM_Actor_Name,
  1944.         ISNULL(ISNULL(i.AC_GEN_AC_ID, i.AC_ID), a.AC_ID),
  1945.         ISNULL(i.Metadata_AC_GEN, i.Metadata_AC),
  1946.         i.AC_GEN_GEN_Gender,
  1947.         ISNULL(i.AC_GEN_Metadata_GEN, i.Metadata_AC),
  1948.         i.AC_GEN_GEN_ID,
  1949.         ISNULL(ISNULL(i.AC_PLV_AC_ID, i.AC_ID), a.AC_ID),
  1950.         ISNULL(i.Metadata_AC_PLV, i.Metadata_AC),
  1951.         ISNULL(i.AC_PLV_ChangedAt, @now),
  1952.         i.AC_PLV_PLV_ProfessionalLevel,
  1953.         ISNULL(i.AC_PLV_Metadata_PLV, i.Metadata_AC),
  1954.         i.AC_PLV_PLV_ID
  1955.     FROM (
  1956.         SELECT
  1957.             AC_ID,
  1958.             Metadata_AC,
  1959.             AC_NAM_AC_ID,
  1960.             Metadata_AC_NAM,
  1961.             AC_NAM_ChangedAt,
  1962.             AC_NAM_Actor_Name,
  1963.             AC_GEN_AC_ID,
  1964.             Metadata_AC_GEN,
  1965.             AC_GEN_GEN_Gender,
  1966.             AC_GEN_Metadata_GEN,
  1967.             AC_GEN_GEN_ID,
  1968.             AC_PLV_AC_ID,
  1969.             Metadata_AC_PLV,
  1970.             AC_PLV_ChangedAt,
  1971.             AC_PLV_PLV_ProfessionalLevel,
  1972.             AC_PLV_Metadata_PLV,
  1973.             AC_PLV_PLV_ID,
  1974.             ROW_NUMBER() OVER (PARTITION BY AC_ID ORDER BY AC_ID) AS Row
  1975.         FROM
  1976.             inserted
  1977.     ) i
  1978.     LEFT JOIN
  1979.         @AC a
  1980.     ON
  1981.         a.Row = i.Row;
  1982.     DECLARE @AC_NAM_Actor_Name TABLE (
  1983.         AC_NAM_AC_ID int not null,
  1984.         Metadata_AC_NAM int not null,
  1985.         AC_NAM_ChangedAt datetime not null,
  1986.         AC_NAM_Actor_Name varchar(42) not null,
  1987.         AC_NAM_Version bigint not null,
  1988.         AC_NAM_StatementType char(1) not null,
  1989.         primary key(
  1990.             AC_NAM_Version,
  1991.             AC_NAM_AC_ID
  1992.         )
  1993.     );
  1994.     INSERT INTO @AC_NAM_Actor_Name
  1995.     SELECT
  1996.         i.AC_NAM_AC_ID,
  1997.         i.Metadata_AC_NAM,
  1998.         i.AC_NAM_ChangedAt,
  1999.         i.AC_NAM_Actor_Name,
  2000.         DENSE_RANK() OVER (PARTITION BY i.AC_NAM_AC_ID ORDER BY i.AC_NAM_ChangedAt),
  2001.         'X'
  2002.     FROM
  2003.         @inserted i
  2004.     WHERE
  2005.         i.AC_NAM_Actor_Name is not null;
  2006.     SELECT
  2007.         @maxVersion = max(AC_NAM_Version),
  2008.         @currentVersion = 0
  2009.     FROM
  2010.         @AC_NAM_Actor_Name;
  2011.     WHILE (@currentVersion < @maxVersion)
  2012.     BEGIN
  2013.         SET @currentVersion = @currentVersion + 1;
  2014.         UPDATE v
  2015.         SET
  2016.             v.AC_NAM_StatementType =
  2017.                 CASE
  2018.                     WHEN [NAM].AC_NAM_AC_ID is not null
  2019.                     THEN 'D' -- duplicate
  2020.                     WHEN [dbo].[rfAC_NAM_Actor_Name](
  2021.                         v.AC_NAM_AC_ID,
  2022.                         v.AC_NAM_Actor_Name,
  2023.                         v.AC_NAM_ChangedAt
  2024.                     ) = 1
  2025.                     THEN 'R' -- restatement
  2026.                     ELSE 'N' -- new statement
  2027.                 END
  2028.         FROM
  2029.             @AC_NAM_Actor_Name v
  2030.         LEFT JOIN
  2031.             [dbo].[AC_NAM_Actor_Name] [NAM]
  2032.         ON
  2033.             [NAM].AC_NAM_AC_ID = v.AC_NAM_AC_ID
  2034.         AND
  2035.             [NAM].AC_NAM_ChangedAt = v.AC_NAM_ChangedAt
  2036.         AND
  2037.             [NAM].AC_NAM_Actor_Name = v.AC_NAM_Actor_Name
  2038.         WHERE
  2039.             v.AC_NAM_Version = @currentVersion;
  2040.         INSERT INTO [dbo].[AC_NAM_Actor_Name] (
  2041.             AC_NAM_AC_ID,
  2042.             Metadata_AC_NAM,
  2043.             AC_NAM_ChangedAt,
  2044.             AC_NAM_Actor_Name
  2045.         )
  2046.         SELECT
  2047.             AC_NAM_AC_ID,
  2048.             Metadata_AC_NAM,
  2049.             AC_NAM_ChangedAt,
  2050.             AC_NAM_Actor_Name
  2051.         FROM
  2052.             @AC_NAM_Actor_Name
  2053.         WHERE
  2054.             AC_NAM_Version = @currentVersion
  2055.         AND
  2056.             AC_NAM_StatementType in ('N');
  2057.     END
  2058.     INSERT INTO [dbo].[AC_GEN_Actor_Gender] (
  2059.         AC_GEN_AC_ID,
  2060.         Metadata_AC_GEN,
  2061.         AC_GEN_GEN_ID
  2062.     )
  2063.     SELECT
  2064.         i.AC_GEN_AC_ID,
  2065.         i.Metadata_AC_GEN,
  2066.         ISNULL(i.AC_GEN_GEN_ID, [kGEN].GEN_ID)
  2067.     FROM
  2068.         @inserted i
  2069.     LEFT JOIN
  2070.         [dbo].[AC_GEN_Actor_Gender] [GEN]
  2071.     ON
  2072.         [GEN].AC_GEN_AC_ID = i.AC_GEN_AC_ID
  2073.     LEFT JOIN
  2074.         [dbo].[GEN_Gender] [kGEN]
  2075.     ON
  2076.         [kGEN].GEN_Gender = i.AC_GEN_GEN_Gender
  2077.     WHERE
  2078.         ISNULL(i.AC_GEN_GEN_ID, [kGEN].GEN_ID) is not null
  2079.     AND
  2080.         [GEN].AC_GEN_AC_ID is null
  2081.     AND
  2082.         ISNULL(i.AC_GEN_GEN_ID, [kGEN].GEN_ID) is not null;
  2083.     DECLARE @AC_PLV_Actor_ProfessionalLevel TABLE (
  2084.         AC_PLV_AC_ID int not null,
  2085.         Metadata_AC_PLV int not null,
  2086.         AC_PLV_ChangedAt datetime not null,
  2087.         AC_PLV_PLV_ID tinyint not null,
  2088.         AC_PLV_Version bigint not null,
  2089.         AC_PLV_StatementType char(1) not null,
  2090.         primary key(
  2091.             AC_PLV_Version,
  2092.             AC_PLV_AC_ID
  2093.         )
  2094.     );
  2095.     INSERT INTO @AC_PLV_Actor_ProfessionalLevel
  2096.     SELECT
  2097.         i.AC_PLV_AC_ID,
  2098.         i.Metadata_AC_PLV,
  2099.         i.AC_PLV_ChangedAt,
  2100.         ISNULL(i.AC_PLV_PLV_ID, [kPLV].PLV_ID),
  2101.         DENSE_RANK() OVER (PARTITION BY i.AC_PLV_AC_ID ORDER BY i.AC_PLV_ChangedAt),
  2102.         'X'
  2103.     FROM
  2104.         @inserted i
  2105.     LEFT JOIN
  2106.         [dbo].[PLV_ProfessionalLevel] [kPLV]
  2107.     ON
  2108.         [kPLV].PLV_ProfessionalLevel = i.AC_PLV_PLV_ProfessionalLevel
  2109.     WHERE
  2110.         ISNULL(i.AC_PLV_PLV_ID, [kPLV].PLV_ID) is not null;
  2111.     SELECT
  2112.         @maxVersion = max(AC_PLV_Version),
  2113.         @currentVersion = 0
  2114.     FROM
  2115.         @AC_PLV_Actor_ProfessionalLevel;
  2116.     WHILE (@currentVersion < @maxVersion)
  2117.     BEGIN
  2118.         SET @currentVersion = @currentVersion + 1;
  2119.         UPDATE v
  2120.         SET
  2121.             v.AC_PLV_StatementType =
  2122.                 CASE
  2123.                     WHEN [PLV].AC_PLV_AC_ID is not null
  2124.                     THEN 'D' -- duplicate
  2125.                     WHEN [dbo].[rfAC_PLV_Actor_ProfessionalLevel](
  2126.                         v.AC_PLV_AC_ID,
  2127.                         v.AC_PLV_PLV_ID,
  2128.                         v.AC_PLV_ChangedAt
  2129.                     ) = 1
  2130.                     THEN 'R' -- restatement
  2131.                     ELSE 'N' -- new statement
  2132.                 END
  2133.         FROM
  2134.             @AC_PLV_Actor_ProfessionalLevel v
  2135.         LEFT JOIN
  2136.             [dbo].[AC_PLV_Actor_ProfessionalLevel] [PLV]
  2137.         ON
  2138.             [PLV].AC_PLV_AC_ID = v.AC_PLV_AC_ID
  2139.         AND
  2140.             [PLV].AC_PLV_ChangedAt = v.AC_PLV_ChangedAt
  2141.         AND
  2142.             [PLV].AC_PLV_PLV_ID = v.AC_PLV_PLV_ID
  2143.         WHERE
  2144.             v.AC_PLV_Version = @currentVersion;
  2145.         INSERT INTO [dbo].[AC_PLV_Actor_ProfessionalLevel] (
  2146.             AC_PLV_AC_ID,
  2147.             Metadata_AC_PLV,
  2148.             AC_PLV_ChangedAt,
  2149.             AC_PLV_PLV_ID
  2150.         )
  2151.         SELECT
  2152.             AC_PLV_AC_ID,
  2153.             Metadata_AC_PLV,
  2154.             AC_PLV_ChangedAt,
  2155.             AC_PLV_PLV_ID
  2156.         FROM
  2157.             @AC_PLV_Actor_ProfessionalLevel
  2158.         WHERE
  2159.             AC_PLV_Version = @currentVersion
  2160.         AND
  2161.             AC_PLV_StatementType in ('N');
  2162.     END
  2163. END
  2164. GO
  2165. -- UPDATE trigger -----------------------------------------------------------------------------------------------------
  2166. -- utAC_Actor instead of UPDATE trigger on lAC_Actor
  2167. -----------------------------------------------------------------------------------------------------------------------
  2168. CREATE TRIGGER [dbo].[utAC_Actor] ON [dbo].[lAC_Actor]
  2169. INSTEAD OF UPDATE
  2170. AS
  2171. BEGIN
  2172.     SET NOCOUNT ON;
  2173.     DECLARE @now datetime2(7) = sysdatetime();
  2174.     IF(UPDATE(AC_ID))
  2175.         RAISERROR('The identity column AC_ID is not updatable.', 16, 1);
  2176.     IF(UPDATE(AC_NAM_Actor_Name))
  2177.     INSERT INTO [dbo].[AC_NAM_Actor_Name] (
  2178.         AC_NAM_AC_ID,
  2179.         Metadata_AC_NAM,
  2180.         AC_NAM_ChangedAt,
  2181.         AC_NAM_Actor_Name
  2182.     )
  2183.     SELECT
  2184.         i.AC_NAM_AC_ID,
  2185.         CASE WHEN UPDATE(Metadata_AC_NAM) THEN i.Metadata_AC_NAM ELSE 0 END,
  2186.         CASE WHEN UPDATE(AC_NAM_ChangedAt) THEN i.AC_NAM_ChangedAt ELSE @now END,
  2187.         i.AC_NAM_Actor_Name
  2188.     FROM
  2189.         inserted i
  2190.     LEFT JOIN
  2191.         [dbo].[AC_NAM_Actor_Name] b
  2192.     ON
  2193.         b.AC_NAM_AC_ID = i.AC_NAM_AC_ID
  2194.     AND
  2195.         b.AC_NAM_Actor_Name = i.AC_NAM_Actor_Name
  2196.     AND
  2197.         b.AC_NAM_ChangedAt = i.AC_NAM_ChangedAt
  2198.     WHERE
  2199.         b.AC_NAM_AC_ID is null
  2200.     AND
  2201.         [dbo].[rfAC_NAM_Actor_Name](
  2202.             i.AC_NAM_AC_ID,
  2203.             i.AC_NAM_Actor_Name,
  2204.             i.AC_NAM_ChangedAt
  2205.         ) = 0;
  2206.     IF(UPDATE(AC_PLV_PLV_ID) OR UPDATE(AC_PLV_PLV_ProfessionalLevel))
  2207.     INSERT INTO [dbo].[AC_PLV_Actor_ProfessionalLevel] (
  2208.         AC_PLV_AC_ID,
  2209.         Metadata_AC_PLV,
  2210.         AC_PLV_ChangedAt,
  2211.         AC_PLV_PLV_ID
  2212.     )
  2213.     SELECT
  2214.         i.AC_PLV_AC_ID,
  2215.         CASE WHEN UPDATE(Metadata_AC_PLV) THEN i.Metadata_AC_PLV ELSE 0 END,
  2216.         CASE WHEN UPDATE(AC_PLV_ChangedAt) THEN i.AC_PLV_ChangedAt ELSE @now END,
  2217.         CASE WHEN UPDATE(AC_PLV_PLV_ID) THEN i.AC_PLV_PLV_ID ELSE [kPLV].PLV_ID END
  2218.     FROM
  2219.         inserted i
  2220.     LEFT JOIN
  2221.         [dbo].[PLV_ProfessionalLevel] [kPLV]
  2222.     ON
  2223.         [kPLV].PLV_ProfessionalLevel = i.AC_PLV_PLV_ProfessionalLevel
  2224.     LEFT JOIN
  2225.         [dbo].[AC_PLV_Actor_ProfessionalLevel] b
  2226.     ON
  2227.         b.AC_PLV_AC_ID = i.AC_PLV_AC_ID
  2228.     AND
  2229.         b.AC_PLV_PLV_ID = CASE WHEN UPDATE(AC_PLV_PLV_ID) THEN i.AC_PLV_PLV_ID ELSE [kPLV].PLV_ID END
  2230.     AND
  2231.         b.AC_PLV_ChangedAt = i.AC_PLV_ChangedAt
  2232.     WHERE
  2233.         b.AC_PLV_AC_ID is null
  2234.     AND
  2235.         [dbo].[rfAC_PLV_Actor_ProfessionalLevel](
  2236.             i.AC_PLV_AC_ID,
  2237.             i.AC_PLV_PLV_ID,
  2238.             i.AC_PLV_ChangedAt
  2239.         ) = 0;
  2240. END
  2241. GO
  2242. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  2243. -- dtAC_Actor instead of DELETE trigger on lAC_Actor
  2244. -----------------------------------------------------------------------------------------------------------------------
  2245. CREATE TRIGGER [dbo].[dtAC_Actor] ON [dbo].[lAC_Actor]
  2246. INSTEAD OF DELETE
  2247. AS
  2248. BEGIN
  2249.     SET NOCOUNT ON;
  2250.     DELETE [NAM]
  2251.     FROM
  2252.         [dbo].[AC_NAM_Actor_Name] [NAM]
  2253.     JOIN
  2254.         deleted d
  2255.     ON
  2256.         d.AC_NAM_AC_ID = [NAM].AC_NAM_AC_ID
  2257.     AND
  2258.         d.AC_NAM_ChangedAt = [NAM].AC_NAM_ChangedAt;
  2259.     DELETE [GEN]
  2260.     FROM
  2261.         [dbo].[AC_GEN_Actor_Gender] [GEN]
  2262.     JOIN
  2263.         deleted d
  2264.     ON
  2265.         d.AC_GEN_AC_ID = [GEN].AC_GEN_AC_ID
  2266.     DELETE [PLV]
  2267.     FROM
  2268.         [dbo].[AC_PLV_Actor_ProfessionalLevel] [PLV]
  2269.     JOIN
  2270.         deleted d
  2271.     ON
  2272.         d.AC_PLV_AC_ID = [PLV].AC_PLV_AC_ID
  2273.     AND
  2274.         d.AC_PLV_ChangedAt = [PLV].AC_PLV_ChangedAt;
  2275.     DELETE [AC]
  2276.     FROM
  2277.         [dbo].[AC_Actor] [AC]
  2278.     LEFT JOIN
  2279.         [dbo].[AC_NAM_Actor_Name] [NAM]
  2280.     ON
  2281.         [NAM].AC_NAM_AC_ID = [AC].AC_ID
  2282.     LEFT JOIN
  2283.         [dbo].[AC_GEN_Actor_Gender] [GEN]
  2284.     ON
  2285.         [GEN].AC_GEN_AC_ID = [AC].AC_ID
  2286.     LEFT JOIN
  2287.         [dbo].[AC_PLV_Actor_ProfessionalLevel] [PLV]
  2288.     ON
  2289.         [PLV].AC_PLV_AC_ID = [AC].AC_ID
  2290.     WHERE
  2291.         [NAM].AC_NAM_AC_ID is null
  2292.     AND
  2293.         [GEN].AC_GEN_AC_ID is null
  2294.     AND
  2295.         [PLV].AC_PLV_AC_ID is null;
  2296. END
  2297. GO
  2298. -- TIE RESTATEMENT CONSTRAINTS ----------------------------------------------------------------------------------------
  2299. --
  2300. -- Ties may be prevented from storing restatements.
  2301. -- A restatement is when the same (non-key) values occurs for two adjacent points
  2302. -- in changing time.
  2303. --
  2304. -- returns 1 for one or two equal surrounding values, 0 for different surrounding values
  2305. --
  2306. -- Restatement Finder Function and Constraint -------------------------------------------------------------------------
  2307. -- rfAC_exclusive_AC_with restatement finder
  2308. --
  2309. -- @AC_ID_exclusive non-key value
  2310. -- @AC_ID_with non-key value
  2311. -- @changed the point in time from which this value shall represent a change
  2312. --
  2313. -- rcAC_exclusive_AC_with restatement constraint
  2314. -----------------------------------------------------------------------------------------------------------------------
  2315. IF Object_ID('rfAC_exclusive_AC_with', 'FN') IS NULL
  2316. BEGIN
  2317.     EXEC('
  2318.    CREATE FUNCTION [dbo].[rfAC_exclusive_AC_with] (
  2319.        @AC_ID_exclusive int,
  2320.        @AC_ID_with int,
  2321.        @changed datetime
  2322.    )
  2323.    RETURNS tinyint AS
  2324.    BEGIN RETURN (
  2325.        SELECT
  2326.            COUNT(*)
  2327.        FROM (
  2328.            SELECT TOP 1
  2329.                pre.AC_ID_exclusive,
  2330.                pre.AC_ID_with
  2331.            FROM
  2332.                [dbo].[AC_exclusive_AC_with] pre
  2333.            WHERE
  2334.            (
  2335.                    pre.AC_ID_exclusive = @AC_ID_exclusive
  2336.                OR
  2337.                    pre.AC_ID_with = @AC_ID_with
  2338.            )
  2339.            AND
  2340.                pre.AC_exclusive_AC_with_ChangedAt < @changed
  2341.            ORDER BY
  2342.                pre.AC_exclusive_AC_with_ChangedAt DESC
  2343.            UNION
  2344.            SELECT TOP 1
  2345.                fol.AC_ID_exclusive,
  2346.                fol.AC_ID_with
  2347.            FROM
  2348.                [dbo].[AC_exclusive_AC_with] fol
  2349.            WHERE
  2350.            (
  2351.                    fol.AC_ID_exclusive = @AC_ID_exclusive
  2352.                OR
  2353.                    fol.AC_ID_with = @AC_ID_with
  2354.            )
  2355.            AND
  2356.                fol.AC_exclusive_AC_with_ChangedAt > @changed
  2357.            ORDER BY
  2358.                fol.AC_exclusive_AC_with_ChangedAt ASC
  2359.        ) s
  2360.        WHERE
  2361.            s.AC_ID_exclusive = @AC_ID_exclusive
  2362.        AND
  2363.            s.AC_ID_with = @AC_ID_with
  2364.    );
  2365.    END
  2366.    ');
  2367.     ALTER TABLE [dbo].[AC_exclusive_AC_with]
  2368.     ADD CONSTRAINT [rcAC_exclusive_AC_with] CHECK (
  2369.         [dbo].[rfAC_exclusive_AC_with] (
  2370.             AC_ID_exclusive,
  2371.             AC_ID_with,
  2372.             AC_exclusive_AC_with_ChangedAt
  2373.         ) = 0
  2374.     );
  2375. END
  2376. GO
  2377. -- Restatement Finder Function and Constraint -------------------------------------------------------------------------
  2378. -- rfAC_part_PR_in_RAT_got restatement finder
  2379. --
  2380. -- @AC_ID_part primary key component
  2381. -- @PR_ID_in primary key component
  2382. -- @RAT_ID_got non-key value
  2383. -- @changed the point in time from which this value shall represent a change
  2384. --
  2385. -- rcAC_part_PR_in_RAT_got restatement constraint
  2386. -----------------------------------------------------------------------------------------------------------------------
  2387. IF Object_ID('rfAC_part_PR_in_RAT_got', 'FN') IS NULL
  2388. BEGIN
  2389.     EXEC('
  2390.    CREATE FUNCTION [dbo].[rfAC_part_PR_in_RAT_got] (
  2391.        @AC_ID_part int,
  2392.        @PR_ID_in int,
  2393.        @RAT_ID_got tinyint,
  2394.        @changed datetime
  2395.    )
  2396.    RETURNS tinyint AS
  2397.    BEGIN RETURN (
  2398.        SELECT
  2399.            COUNT(*)
  2400.        FROM (
  2401.            SELECT TOP 1
  2402.                pre.RAT_ID_got
  2403.            FROM
  2404.                [dbo].[AC_part_PR_in_RAT_got] pre
  2405.            WHERE
  2406.                pre.AC_ID_part = @AC_ID_part
  2407.            AND
  2408.                pre.PR_ID_in = @PR_ID_in
  2409.            AND
  2410.                pre.AC_part_PR_in_RAT_got_ChangedAt < @changed
  2411.            ORDER BY
  2412.                pre.AC_part_PR_in_RAT_got_ChangedAt DESC
  2413.            UNION
  2414.            SELECT TOP 1
  2415.                fol.RAT_ID_got
  2416.            FROM
  2417.                [dbo].[AC_part_PR_in_RAT_got] fol
  2418.            WHERE
  2419.                fol.AC_ID_part = @AC_ID_part
  2420.            AND
  2421.                fol.PR_ID_in = @PR_ID_in
  2422.            AND
  2423.                fol.AC_part_PR_in_RAT_got_ChangedAt > @changed
  2424.            ORDER BY
  2425.                fol.AC_part_PR_in_RAT_got_ChangedAt ASC
  2426.        ) s
  2427.        WHERE
  2428.            s.RAT_ID_got = @RAT_ID_got
  2429.    );
  2430.    END
  2431.    ');
  2432.     ALTER TABLE [dbo].[AC_part_PR_in_RAT_got]
  2433.     ADD CONSTRAINT [rcAC_part_PR_in_RAT_got] CHECK (
  2434.         [dbo].[rfAC_part_PR_in_RAT_got] (
  2435.             AC_ID_part,
  2436.             PR_ID_in,
  2437.             RAT_ID_got,
  2438.             AC_part_PR_in_RAT_got_ChangedAt
  2439.         ) = 0
  2440.     );
  2441. END
  2442. GO
  2443. -- TIE TEMPORAL PERSPECTIVES ------------------------------------------------------------------------------------------
  2444. --
  2445. -- These table valued functions simplify temporal querying by providing a temporal
  2446. -- perspective of each tie. There are four types of perspectives: latest,
  2447. -- point-in-time, difference, and now.
  2448. --
  2449. -- The latest perspective shows the latest available information for each tie.
  2450. -- The now perspective shows the information as it is right now.
  2451. -- The point-in-time perspective lets you travel through the information to the given timepoint.
  2452. --
  2453. -- @changingTimepoint the point in changing time to travel to
  2454. --
  2455. -- The difference perspective shows changes between the two given timepoints.
  2456. --
  2457. -- @intervalStart the start of the interval for finding changes
  2458. -- @intervalEnd the end of the interval for finding changes
  2459. --
  2460. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2461. IF Object_ID('dAC_parent_AC_child_PAT_having', 'IF') IS NOT NULL
  2462. DROP FUNCTION [dbo].[dAC_parent_AC_child_PAT_having];
  2463. IF Object_ID('nAC_parent_AC_child_PAT_having', 'V') IS NOT NULL
  2464. DROP VIEW [dbo].[nAC_parent_AC_child_PAT_having];
  2465. IF Object_ID('pAC_parent_AC_child_PAT_having', 'IF') IS NOT NULL
  2466. DROP FUNCTION [dbo].[pAC_parent_AC_child_PAT_having];
  2467. IF Object_ID('lAC_parent_AC_child_PAT_having', 'V') IS NOT NULL
  2468. DROP VIEW [dbo].[lAC_parent_AC_child_PAT_having];
  2469. GO
  2470. -- Latest perspective -------------------------------------------------------------------------------------------------
  2471. -- lAC_parent_AC_child_PAT_having viewed by the latest available information (may include future versions)
  2472. -----------------------------------------------------------------------------------------------------------------------
  2473. CREATE VIEW [dbo].[lAC_parent_AC_child_PAT_having] WITH SCHEMABINDING AS
  2474. SELECT
  2475.     tie.Metadata_AC_parent_AC_child_PAT_having,
  2476.     tie.AC_ID_parent,
  2477.     tie.AC_ID_child,
  2478.     [PAT_having].PAT_ParentalType AS having_PAT_ParentalType,
  2479.     [PAT_having].Metadata_PAT AS having_Metadata_PAT,
  2480.     tie.PAT_ID_having
  2481. FROM
  2482.     [dbo].[AC_parent_AC_child_PAT_having] tie
  2483. LEFT JOIN
  2484.     [dbo].[PAT_ParentalType] [PAT_having]
  2485. ON
  2486.     [PAT_having].PAT_ID = tie.PAT_ID_having;
  2487. GO
  2488. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2489. -- pAC_parent_AC_child_PAT_having viewed by the latest available information (may include future versions)
  2490. -----------------------------------------------------------------------------------------------------------------------
  2491. CREATE FUNCTION [dbo].[pAC_parent_AC_child_PAT_having] (
  2492.     @changingTimepoint datetime2(7)
  2493. )
  2494. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2495. SELECT
  2496.     tie.Metadata_AC_parent_AC_child_PAT_having,
  2497.     tie.AC_ID_parent,
  2498.     tie.AC_ID_child,
  2499.     [PAT_having].PAT_ParentalType AS having_PAT_ParentalType,
  2500.     [PAT_having].Metadata_PAT AS having_Metadata_PAT,
  2501.     tie.PAT_ID_having
  2502. FROM
  2503.     [dbo].[AC_parent_AC_child_PAT_having] tie
  2504. LEFT JOIN
  2505.     [dbo].[PAT_ParentalType] [PAT_having]
  2506. ON
  2507.     [PAT_having].PAT_ID = tie.PAT_ID_having;
  2508. GO
  2509. -- Now perspective ----------------------------------------------------------------------------------------------------
  2510. -- nAC_parent_AC_child_PAT_having viewed as it currently is (cannot include future versions)
  2511. -----------------------------------------------------------------------------------------------------------------------
  2512. CREATE VIEW [dbo].[nAC_parent_AC_child_PAT_having]
  2513. AS
  2514. SELECT
  2515.     *
  2516. FROM
  2517.     [dbo].[pAC_parent_AC_child_PAT_having](sysdatetime());
  2518. GO
  2519. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2520. IF Object_ID('dST_atLocation_PR_isPlaying', 'IF') IS NOT NULL
  2521. DROP FUNCTION [dbo].[dST_atLocation_PR_isPlaying];
  2522. IF Object_ID('nST_atLocation_PR_isPlaying', 'V') IS NOT NULL
  2523. DROP VIEW [dbo].[nST_atLocation_PR_isPlaying];
  2524. IF Object_ID('pST_atLocation_PR_isPlaying', 'IF') IS NOT NULL
  2525. DROP FUNCTION [dbo].[pST_atLocation_PR_isPlaying];
  2526. IF Object_ID('lST_atLocation_PR_isPlaying', 'V') IS NOT NULL
  2527. DROP VIEW [dbo].[lST_atLocation_PR_isPlaying];
  2528. GO
  2529. -- Latest perspective -------------------------------------------------------------------------------------------------
  2530. -- lST_atLocation_PR_isPlaying viewed by the latest available information (may include future versions)
  2531. -----------------------------------------------------------------------------------------------------------------------
  2532. CREATE VIEW [dbo].[lST_atLocation_PR_isPlaying] WITH SCHEMABINDING AS
  2533. SELECT
  2534.     tie.Metadata_ST_atLocation_PR_isPlaying,
  2535.     tie.ST_atLocation_PR_isPlaying_ChangedAt,
  2536.     tie.ST_ID_atLocation,
  2537.     tie.PR_ID_isPlaying
  2538. FROM
  2539.     [dbo].[ST_atLocation_PR_isPlaying] tie
  2540. WHERE
  2541.     tie.ST_atLocation_PR_isPlaying_ChangedAt = (
  2542.         SELECT
  2543.             max(sub.ST_atLocation_PR_isPlaying_ChangedAt)
  2544.         FROM
  2545.             [dbo].[ST_atLocation_PR_isPlaying] sub
  2546.         WHERE
  2547.             sub.ST_ID_atLocation = tie.ST_ID_atLocation
  2548.         AND
  2549.             sub.PR_ID_isPlaying = tie.PR_ID_isPlaying
  2550.    );
  2551. GO
  2552. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2553. -- pST_atLocation_PR_isPlaying viewed by the latest available information (may include future versions)
  2554. -----------------------------------------------------------------------------------------------------------------------
  2555. CREATE FUNCTION [dbo].[pST_atLocation_PR_isPlaying] (
  2556.     @changingTimepoint datetime2(7)
  2557. )
  2558. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2559. SELECT
  2560.     tie.Metadata_ST_atLocation_PR_isPlaying,
  2561.     tie.ST_atLocation_PR_isPlaying_ChangedAt,
  2562.     tie.ST_ID_atLocation,
  2563.     tie.PR_ID_isPlaying
  2564. FROM
  2565.     [dbo].[ST_atLocation_PR_isPlaying] tie
  2566. WHERE
  2567.     tie.ST_atLocation_PR_isPlaying_ChangedAt = (
  2568.         SELECT
  2569.             max(sub.ST_atLocation_PR_isPlaying_ChangedAt)
  2570.         FROM
  2571.             [dbo].[ST_atLocation_PR_isPlaying] sub
  2572.         WHERE
  2573.             sub.ST_ID_atLocation = tie.ST_ID_atLocation
  2574.         AND
  2575.             sub.PR_ID_isPlaying = tie.PR_ID_isPlaying
  2576.         AND
  2577.             tie.ST_atLocation_PR_isPlaying_ChangedAt <= @changingTimepoint
  2578.    );
  2579. GO
  2580. -- Now perspective ----------------------------------------------------------------------------------------------------
  2581. -- nST_atLocation_PR_isPlaying viewed as it currently is (cannot include future versions)
  2582. -----------------------------------------------------------------------------------------------------------------------
  2583. CREATE VIEW [dbo].[nST_atLocation_PR_isPlaying]
  2584. AS
  2585. SELECT
  2586.     *
  2587. FROM
  2588.     [dbo].[pST_atLocation_PR_isPlaying](sysdatetime());
  2589. GO
  2590. -- Difference perspective ---------------------------------------------------------------------------------------------
  2591. -- dST_atLocation_PR_isPlaying showing all differences between the given timepoints
  2592. -----------------------------------------------------------------------------------------------------------------------
  2593. CREATE FUNCTION [dbo].[dST_atLocation_PR_isPlaying] (
  2594.     @intervalStart datetime2(7),
  2595.     @intervalEnd datetime2(7)
  2596. )
  2597. RETURNS TABLE AS RETURN
  2598. SELECT
  2599.     tie.Metadata_ST_atLocation_PR_isPlaying,
  2600.     tie.ST_atLocation_PR_isPlaying_ChangedAt,
  2601.     tie.ST_ID_atLocation,
  2602.     tie.PR_ID_isPlaying
  2603. FROM
  2604.     [dbo].[ST_atLocation_PR_isPlaying] tie
  2605. WHERE
  2606.     tie.ST_atLocation_PR_isPlaying_ChangedAt BETWEEN @intervalStart AND @intervalEnd;
  2607. GO
  2608. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2609. IF Object_ID('dPE_at_PR_wasPlayed', 'IF') IS NOT NULL
  2610. DROP FUNCTION [dbo].[dPE_at_PR_wasPlayed];
  2611. IF Object_ID('nPE_at_PR_wasPlayed', 'V') IS NOT NULL
  2612. DROP VIEW [dbo].[nPE_at_PR_wasPlayed];
  2613. IF Object_ID('pPE_at_PR_wasPlayed', 'IF') IS NOT NULL
  2614. DROP FUNCTION [dbo].[pPE_at_PR_wasPlayed];
  2615. IF Object_ID('lPE_at_PR_wasPlayed', 'V') IS NOT NULL
  2616. DROP VIEW [dbo].[lPE_at_PR_wasPlayed];
  2617. GO
  2618. -- Latest perspective -------------------------------------------------------------------------------------------------
  2619. -- lPE_at_PR_wasPlayed viewed by the latest available information (may include future versions)
  2620. -----------------------------------------------------------------------------------------------------------------------
  2621. CREATE VIEW [dbo].[lPE_at_PR_wasPlayed] WITH SCHEMABINDING AS
  2622. SELECT
  2623.     tie.Metadata_PE_at_PR_wasPlayed,
  2624.     tie.PE_ID_at,
  2625.     tie.PR_ID_wasPlayed
  2626. FROM
  2627.     [dbo].[PE_at_PR_wasPlayed] tie;
  2628. GO
  2629. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2630. -- pPE_at_PR_wasPlayed viewed by the latest available information (may include future versions)
  2631. -----------------------------------------------------------------------------------------------------------------------
  2632. CREATE FUNCTION [dbo].[pPE_at_PR_wasPlayed] (
  2633.     @changingTimepoint datetime2(7)
  2634. )
  2635. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2636. SELECT
  2637.     tie.Metadata_PE_at_PR_wasPlayed,
  2638.     tie.PE_ID_at,
  2639.     tie.PR_ID_wasPlayed
  2640. FROM
  2641.     [dbo].[PE_at_PR_wasPlayed] tie;
  2642. GO
  2643. -- Now perspective ----------------------------------------------------------------------------------------------------
  2644. -- nPE_at_PR_wasPlayed viewed as it currently is (cannot include future versions)
  2645. -----------------------------------------------------------------------------------------------------------------------
  2646. CREATE VIEW [dbo].[nPE_at_PR_wasPlayed]
  2647. AS
  2648. SELECT
  2649.     *
  2650. FROM
  2651.     [dbo].[pPE_at_PR_wasPlayed](sysdatetime());
  2652. GO
  2653. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2654. IF Object_ID('dPE_wasHeld_ST_atLocation', 'IF') IS NOT NULL
  2655. DROP FUNCTION [dbo].[dPE_wasHeld_ST_atLocation];
  2656. IF Object_ID('nPE_wasHeld_ST_atLocation', 'V') IS NOT NULL
  2657. DROP VIEW [dbo].[nPE_wasHeld_ST_atLocation];
  2658. IF Object_ID('pPE_wasHeld_ST_atLocation', 'IF') IS NOT NULL
  2659. DROP FUNCTION [dbo].[pPE_wasHeld_ST_atLocation];
  2660. IF Object_ID('lPE_wasHeld_ST_atLocation', 'V') IS NOT NULL
  2661. DROP VIEW [dbo].[lPE_wasHeld_ST_atLocation];
  2662. GO
  2663. -- Latest perspective -------------------------------------------------------------------------------------------------
  2664. -- lPE_wasHeld_ST_atLocation viewed by the latest available information (may include future versions)
  2665. -----------------------------------------------------------------------------------------------------------------------
  2666. CREATE VIEW [dbo].[lPE_wasHeld_ST_atLocation] WITH SCHEMABINDING AS
  2667. SELECT
  2668.     tie.Metadata_PE_wasHeld_ST_atLocation,
  2669.     tie.PE_ID_wasHeld,
  2670.     tie.ST_ID_atLocation
  2671. FROM
  2672.     [dbo].[PE_wasHeld_ST_atLocation] tie;
  2673. GO
  2674. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2675. -- pPE_wasHeld_ST_atLocation viewed by the latest available information (may include future versions)
  2676. -----------------------------------------------------------------------------------------------------------------------
  2677. CREATE FUNCTION [dbo].[pPE_wasHeld_ST_atLocation] (
  2678.     @changingTimepoint datetime2(7)
  2679. )
  2680. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2681. SELECT
  2682.     tie.Metadata_PE_wasHeld_ST_atLocation,
  2683.     tie.PE_ID_wasHeld,
  2684.     tie.ST_ID_atLocation
  2685. FROM
  2686.     [dbo].[PE_wasHeld_ST_atLocation] tie;
  2687. GO
  2688. -- Now perspective ----------------------------------------------------------------------------------------------------
  2689. -- nPE_wasHeld_ST_atLocation viewed as it currently is (cannot include future versions)
  2690. -----------------------------------------------------------------------------------------------------------------------
  2691. CREATE VIEW [dbo].[nPE_wasHeld_ST_atLocation]
  2692. AS
  2693. SELECT
  2694.     *
  2695. FROM
  2696.     [dbo].[pPE_wasHeld_ST_atLocation](sysdatetime());
  2697. GO
  2698. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2699. IF Object_ID('dAC_exclusive_AC_with', 'IF') IS NOT NULL
  2700. DROP FUNCTION [dbo].[dAC_exclusive_AC_with];
  2701. IF Object_ID('nAC_exclusive_AC_with', 'V') IS NOT NULL
  2702. DROP VIEW [dbo].[nAC_exclusive_AC_with];
  2703. IF Object_ID('pAC_exclusive_AC_with', 'IF') IS NOT NULL
  2704. DROP FUNCTION [dbo].[pAC_exclusive_AC_with];
  2705. IF Object_ID('lAC_exclusive_AC_with', 'V') IS NOT NULL
  2706. DROP VIEW [dbo].[lAC_exclusive_AC_with];
  2707. GO
  2708. -- Latest perspective -------------------------------------------------------------------------------------------------
  2709. -- lAC_exclusive_AC_with viewed by the latest available information (may include future versions)
  2710. -----------------------------------------------------------------------------------------------------------------------
  2711. CREATE VIEW [dbo].[lAC_exclusive_AC_with] WITH SCHEMABINDING AS
  2712. SELECT
  2713.     tie.Metadata_AC_exclusive_AC_with,
  2714.     tie.AC_exclusive_AC_with_ChangedAt,
  2715.     tie.AC_ID_exclusive,
  2716.     tie.AC_ID_with
  2717. FROM
  2718.     [dbo].[AC_exclusive_AC_with] tie
  2719. WHERE
  2720.     tie.AC_exclusive_AC_with_ChangedAt = (
  2721.         SELECT
  2722.             max(sub.AC_exclusive_AC_with_ChangedAt)
  2723.         FROM
  2724.             [dbo].[AC_exclusive_AC_with] sub
  2725.         WHERE
  2726.             sub.AC_ID_exclusive = tie.AC_ID_exclusive
  2727.         OR
  2728.             sub.AC_ID_with = tie.AC_ID_with
  2729.    );
  2730. GO
  2731. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2732. -- pAC_exclusive_AC_with viewed by the latest available information (may include future versions)
  2733. -----------------------------------------------------------------------------------------------------------------------
  2734. CREATE FUNCTION [dbo].[pAC_exclusive_AC_with] (
  2735.     @changingTimepoint datetime2(7)
  2736. )
  2737. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2738. SELECT
  2739.     tie.Metadata_AC_exclusive_AC_with,
  2740.     tie.AC_exclusive_AC_with_ChangedAt,
  2741.     tie.AC_ID_exclusive,
  2742.     tie.AC_ID_with
  2743. FROM
  2744.     [dbo].[AC_exclusive_AC_with] tie
  2745. WHERE
  2746.     tie.AC_exclusive_AC_with_ChangedAt = (
  2747.         SELECT
  2748.             max(sub.AC_exclusive_AC_with_ChangedAt)
  2749.         FROM
  2750.             [dbo].[AC_exclusive_AC_with] sub
  2751.         WHERE
  2752.         (
  2753.                 sub.AC_ID_exclusive = tie.AC_ID_exclusive
  2754.             OR
  2755.                 sub.AC_ID_with = tie.AC_ID_with
  2756.         )
  2757.         AND
  2758.             tie.AC_exclusive_AC_with_ChangedAt <= @changingTimepoint
  2759.    );
  2760. GO
  2761. -- Now perspective ----------------------------------------------------------------------------------------------------
  2762. -- nAC_exclusive_AC_with viewed as it currently is (cannot include future versions)
  2763. -----------------------------------------------------------------------------------------------------------------------
  2764. CREATE VIEW [dbo].[nAC_exclusive_AC_with]
  2765. AS
  2766. SELECT
  2767.     *
  2768. FROM
  2769.     [dbo].[pAC_exclusive_AC_with](sysdatetime());
  2770. GO
  2771. -- Difference perspective ---------------------------------------------------------------------------------------------
  2772. -- dAC_exclusive_AC_with showing all differences between the given timepoints
  2773. -----------------------------------------------------------------------------------------------------------------------
  2774. CREATE FUNCTION [dbo].[dAC_exclusive_AC_with] (
  2775.     @intervalStart datetime2(7),
  2776.     @intervalEnd datetime2(7)
  2777. )
  2778. RETURNS TABLE AS RETURN
  2779. SELECT
  2780.     tie.Metadata_AC_exclusive_AC_with,
  2781.     tie.AC_exclusive_AC_with_ChangedAt,
  2782.     tie.AC_ID_exclusive,
  2783.     tie.AC_ID_with
  2784. FROM
  2785.     [dbo].[AC_exclusive_AC_with] tie
  2786. WHERE
  2787.     tie.AC_exclusive_AC_with_ChangedAt BETWEEN @intervalStart AND @intervalEnd;
  2788. GO
  2789. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2790. IF Object_ID('dPE_subset_EV_of', 'IF') IS NOT NULL
  2791. DROP FUNCTION [dbo].[dPE_subset_EV_of];
  2792. IF Object_ID('nPE_subset_EV_of', 'V') IS NOT NULL
  2793. DROP VIEW [dbo].[nPE_subset_EV_of];
  2794. IF Object_ID('pPE_subset_EV_of', 'IF') IS NOT NULL
  2795. DROP FUNCTION [dbo].[pPE_subset_EV_of];
  2796. IF Object_ID('lPE_subset_EV_of', 'V') IS NOT NULL
  2797. DROP VIEW [dbo].[lPE_subset_EV_of];
  2798. GO
  2799. -- Latest perspective -------------------------------------------------------------------------------------------------
  2800. -- lPE_subset_EV_of viewed by the latest available information (may include future versions)
  2801. -----------------------------------------------------------------------------------------------------------------------
  2802. CREATE VIEW [dbo].[lPE_subset_EV_of] WITH SCHEMABINDING AS
  2803. SELECT
  2804.     tie.Metadata_PE_subset_EV_of,
  2805.     tie.PE_ID_subset,
  2806.     tie.EV_ID_of
  2807. FROM
  2808.     [dbo].[PE_subset_EV_of] tie;
  2809. GO
  2810. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2811. -- pPE_subset_EV_of viewed by the latest available information (may include future versions)
  2812. -----------------------------------------------------------------------------------------------------------------------
  2813. CREATE FUNCTION [dbo].[pPE_subset_EV_of] (
  2814.     @changingTimepoint datetime2(7)
  2815. )
  2816. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2817. SELECT
  2818.     tie.Metadata_PE_subset_EV_of,
  2819.     tie.PE_ID_subset,
  2820.     tie.EV_ID_of
  2821. FROM
  2822.     [dbo].[PE_subset_EV_of] tie;
  2823. GO
  2824. -- Now perspective ----------------------------------------------------------------------------------------------------
  2825. -- nPE_subset_EV_of viewed as it currently is (cannot include future versions)
  2826. -----------------------------------------------------------------------------------------------------------------------
  2827. CREATE VIEW [dbo].[nPE_subset_EV_of]
  2828. AS
  2829. SELECT
  2830.     *
  2831. FROM
  2832.     [dbo].[pPE_subset_EV_of](sysdatetime());
  2833. GO
  2834. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2835. IF Object_ID('dPE_in_AC_wasCast', 'IF') IS NOT NULL
  2836. DROP FUNCTION [dbo].[dPE_in_AC_wasCast];
  2837. IF Object_ID('nPE_in_AC_wasCast', 'V') IS NOT NULL
  2838. DROP VIEW [dbo].[nPE_in_AC_wasCast];
  2839. IF Object_ID('pPE_in_AC_wasCast', 'IF') IS NOT NULL
  2840. DROP FUNCTION [dbo].[pPE_in_AC_wasCast];
  2841. IF Object_ID('lPE_in_AC_wasCast', 'V') IS NOT NULL
  2842. DROP VIEW [dbo].[lPE_in_AC_wasCast];
  2843. GO
  2844. -- Latest perspective -------------------------------------------------------------------------------------------------
  2845. -- lPE_in_AC_wasCast viewed by the latest available information (may include future versions)
  2846. -----------------------------------------------------------------------------------------------------------------------
  2847. CREATE VIEW [dbo].[lPE_in_AC_wasCast] WITH SCHEMABINDING AS
  2848. SELECT
  2849.     tie.Metadata_PE_in_AC_wasCast,
  2850.     tie.PE_ID_in,
  2851.     tie.AC_ID_wasCast
  2852. FROM
  2853.     [dbo].[PE_in_AC_wasCast] tie;
  2854. GO
  2855. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2856. -- pPE_in_AC_wasCast viewed by the latest available information (may include future versions)
  2857. -----------------------------------------------------------------------------------------------------------------------
  2858. CREATE FUNCTION [dbo].[pPE_in_AC_wasCast] (
  2859.     @changingTimepoint datetime2(7)
  2860. )
  2861. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2862. SELECT
  2863.     tie.Metadata_PE_in_AC_wasCast,
  2864.     tie.PE_ID_in,
  2865.     tie.AC_ID_wasCast
  2866. FROM
  2867.     [dbo].[PE_in_AC_wasCast] tie;
  2868. GO
  2869. -- Now perspective ----------------------------------------------------------------------------------------------------
  2870. -- nPE_in_AC_wasCast viewed as it currently is (cannot include future versions)
  2871. -----------------------------------------------------------------------------------------------------------------------
  2872. CREATE VIEW [dbo].[nPE_in_AC_wasCast]
  2873. AS
  2874. SELECT
  2875.     *
  2876. FROM
  2877.     [dbo].[pPE_in_AC_wasCast](sysdatetime());
  2878. GO
  2879. -- Drop perspectives --------------------------------------------------------------------------------------------------
  2880. IF Object_ID('dAC_part_PR_in_RAT_got', 'IF') IS NOT NULL
  2881. DROP FUNCTION [dbo].[dAC_part_PR_in_RAT_got];
  2882. IF Object_ID('nAC_part_PR_in_RAT_got', 'V') IS NOT NULL
  2883. DROP VIEW [dbo].[nAC_part_PR_in_RAT_got];
  2884. IF Object_ID('pAC_part_PR_in_RAT_got', 'IF') IS NOT NULL
  2885. DROP FUNCTION [dbo].[pAC_part_PR_in_RAT_got];
  2886. IF Object_ID('lAC_part_PR_in_RAT_got', 'V') IS NOT NULL
  2887. DROP VIEW [dbo].[lAC_part_PR_in_RAT_got];
  2888. GO
  2889. -- Latest perspective -------------------------------------------------------------------------------------------------
  2890. -- lAC_part_PR_in_RAT_got viewed by the latest available information (may include future versions)
  2891. -----------------------------------------------------------------------------------------------------------------------
  2892. CREATE VIEW [dbo].[lAC_part_PR_in_RAT_got] WITH SCHEMABINDING AS
  2893. SELECT
  2894.     tie.Metadata_AC_part_PR_in_RAT_got,
  2895.     tie.AC_part_PR_in_RAT_got_ChangedAt,
  2896.     tie.AC_ID_part,
  2897.     tie.PR_ID_in,
  2898.     [RAT_got].RAT_Rating AS got_RAT_Rating,
  2899.     [RAT_got].Metadata_RAT AS got_Metadata_RAT,
  2900.     tie.RAT_ID_got
  2901. FROM
  2902.     [dbo].[AC_part_PR_in_RAT_got] tie
  2903. LEFT JOIN
  2904.     [dbo].[RAT_Rating] [RAT_got]
  2905. ON
  2906.     [RAT_got].RAT_ID = tie.RAT_ID_got
  2907. WHERE
  2908.     tie.AC_part_PR_in_RAT_got_ChangedAt = (
  2909.         SELECT
  2910.             max(sub.AC_part_PR_in_RAT_got_ChangedAt)
  2911.         FROM
  2912.             [dbo].[AC_part_PR_in_RAT_got] sub
  2913.         WHERE
  2914.             sub.AC_ID_part = tie.AC_ID_part
  2915.         AND
  2916.             sub.PR_ID_in = tie.PR_ID_in
  2917.    );
  2918. GO
  2919. -- Point-in-time perspective ------------------------------------------------------------------------------------------
  2920. -- pAC_part_PR_in_RAT_got viewed by the latest available information (may include future versions)
  2921. -----------------------------------------------------------------------------------------------------------------------
  2922. CREATE FUNCTION [dbo].[pAC_part_PR_in_RAT_got] (
  2923.     @changingTimepoint datetime2(7)
  2924. )
  2925. RETURNS TABLE WITH SCHEMABINDING AS RETURN
  2926. SELECT
  2927.     tie.Metadata_AC_part_PR_in_RAT_got,
  2928.     tie.AC_part_PR_in_RAT_got_ChangedAt,
  2929.     tie.AC_ID_part,
  2930.     tie.PR_ID_in,
  2931.     [RAT_got].RAT_Rating AS got_RAT_Rating,
  2932.     [RAT_got].Metadata_RAT AS got_Metadata_RAT,
  2933.     tie.RAT_ID_got
  2934. FROM
  2935.     [dbo].[AC_part_PR_in_RAT_got] tie
  2936. LEFT JOIN
  2937.     [dbo].[RAT_Rating] [RAT_got]
  2938. ON
  2939.     [RAT_got].RAT_ID = tie.RAT_ID_got
  2940. WHERE
  2941.     tie.AC_part_PR_in_RAT_got_ChangedAt = (
  2942.         SELECT
  2943.             max(sub.AC_part_PR_in_RAT_got_ChangedAt)
  2944.         FROM
  2945.             [dbo].[AC_part_PR_in_RAT_got] sub
  2946.         WHERE
  2947.             sub.AC_ID_part = tie.AC_ID_part
  2948.         AND
  2949.             sub.PR_ID_in = tie.PR_ID_in
  2950.         AND
  2951.             tie.AC_part_PR_in_RAT_got_ChangedAt <= @changingTimepoint
  2952.    );
  2953. GO
  2954. -- Now perspective ----------------------------------------------------------------------------------------------------
  2955. -- nAC_part_PR_in_RAT_got viewed as it currently is (cannot include future versions)
  2956. -----------------------------------------------------------------------------------------------------------------------
  2957. CREATE VIEW [dbo].[nAC_part_PR_in_RAT_got]
  2958. AS
  2959. SELECT
  2960.     *
  2961. FROM
  2962.     [dbo].[pAC_part_PR_in_RAT_got](sysdatetime());
  2963. GO
  2964. -- Difference perspective ---------------------------------------------------------------------------------------------
  2965. -- dAC_part_PR_in_RAT_got showing all differences between the given timepoints
  2966. -----------------------------------------------------------------------------------------------------------------------
  2967. CREATE FUNCTION [dbo].[dAC_part_PR_in_RAT_got] (
  2968.     @intervalStart datetime2(7),
  2969.     @intervalEnd datetime2(7)
  2970. )
  2971. RETURNS TABLE AS RETURN
  2972. SELECT
  2973.     tie.Metadata_AC_part_PR_in_RAT_got,
  2974.     tie.AC_part_PR_in_RAT_got_ChangedAt,
  2975.     tie.AC_ID_part,
  2976.     tie.PR_ID_in,
  2977.     [RAT_got].RAT_Rating AS got_RAT_Rating,
  2978.     [RAT_got].Metadata_RAT AS got_Metadata_RAT,
  2979.     tie.RAT_ID_got
  2980. FROM
  2981.     [dbo].[AC_part_PR_in_RAT_got] tie
  2982. LEFT JOIN
  2983.     [dbo].[RAT_Rating] [RAT_got]
  2984. ON
  2985.     [RAT_got].RAT_ID = tie.RAT_ID_got
  2986. WHERE
  2987.     tie.AC_part_PR_in_RAT_got_ChangedAt BETWEEN @intervalStart AND @intervalEnd;
  2988. GO
  2989. -- TIE TRIGGERS -------------------------------------------------------------------------------------------------------
  2990. --
  2991. -- The following triggers on the latest view make it behave like a table.
  2992. -- There are three different 'instead of' triggers: insert, update, and delete.
  2993. -- They will ensure that such operations are propagated to the underlying tables
  2994. -- in a consistent way. Default values are used for some columns if not provided
  2995. -- by the corresponding SQL statements.
  2996. --
  2997. -- For idempotent ties, only changes that represent values different from
  2998. -- the previous or following value are stored. Others are silently ignored in
  2999. -- order to avoid unnecessary temporal duplicates.
  3000. --
  3001. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3002. -- itAC_parent_AC_child_PAT_having instead of INSERT trigger on lAC_parent_AC_child_PAT_having
  3003. -----------------------------------------------------------------------------------------------------------------------
  3004. CREATE TRIGGER [dbo].[itAC_parent_AC_child_PAT_having] ON [dbo].[lAC_parent_AC_child_PAT_having]
  3005. INSTEAD OF INSERT
  3006. AS
  3007. BEGIN
  3008.     SET NOCOUNT ON;
  3009.     DECLARE @now datetime2(7) = sysdatetime();
  3010.     DECLARE @maxVersion int;
  3011.     DECLARE @currentVersion int;
  3012.     DECLARE @inserted TABLE (
  3013.         Metadata_AC_parent_AC_child_PAT_having int not null,
  3014.         AC_ID_parent int not null,
  3015.         AC_ID_child int not null,
  3016.         PAT_ID_having tinyint not null,
  3017.         primary key (
  3018.             AC_ID_parent,
  3019.             AC_ID_child,
  3020.             PAT_ID_having
  3021.         )
  3022.     );
  3023.     INSERT INTO @inserted
  3024.     SELECT
  3025.         ISNULL(i.Metadata_AC_parent_AC_child_PAT_having, 0),
  3026.         i.AC_ID_parent,
  3027.         i.AC_ID_child,
  3028.         ISNULL(i.PAT_ID_having, [PAT_having].PAT_ID)
  3029.     FROM
  3030.         inserted i
  3031.     LEFT JOIN
  3032.         [dbo].[PAT_ParentalType] [PAT_having]
  3033.     ON
  3034.         [PAT_having].PAT_ParentalType = i.having_PAT_ParentalType
  3035.     WHERE
  3036.         i.AC_ID_parent is not null
  3037.     AND
  3038.         i.AC_ID_child is not null
  3039.     AND
  3040.         i.PAT_ID_having is not null;
  3041.     INSERT INTO [dbo].[AC_parent_AC_child_PAT_having] (
  3042.         Metadata_AC_parent_AC_child_PAT_having,
  3043.         AC_ID_parent,
  3044.         AC_ID_child,
  3045.         PAT_ID_having
  3046.     )
  3047.     SELECT
  3048.         i.Metadata_AC_parent_AC_child_PAT_having,
  3049.         i.AC_ID_parent,
  3050.         i.AC_ID_child,
  3051.         i.PAT_ID_having
  3052.     FROM
  3053.         @inserted i
  3054.     LEFT JOIN
  3055.         [dbo].[AC_parent_AC_child_PAT_having] tie
  3056.     ON
  3057.         tie.AC_ID_parent = i.AC_ID_parent
  3058.     AND
  3059.         tie.AC_ID_child = i.AC_ID_child
  3060.     AND
  3061.         tie.PAT_ID_having = i.PAT_ID_having
  3062.     WHERE
  3063.         tie.PAT_ID_having is null;
  3064. END
  3065. GO
  3066. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3067. -- dtAC_parent_AC_child_PAT_having instead of DELETE trigger on lAC_parent_AC_child_PAT_having
  3068. -----------------------------------------------------------------------------------------------------------------------
  3069. CREATE TRIGGER [dbo].[dtAC_parent_AC_child_PAT_having] ON [dbo].[lAC_parent_AC_child_PAT_having]
  3070. INSTEAD OF DELETE
  3071. AS
  3072. BEGIN
  3073.     SET NOCOUNT ON;
  3074.     DELETE tie
  3075.     FROM
  3076.         [dbo].[AC_parent_AC_child_PAT_having] tie
  3077.     JOIN
  3078.         deleted d
  3079.     ON
  3080.         d.AC_ID_parent = tie.AC_ID_parent
  3081.     AND
  3082.         d.AC_ID_child = tie.AC_ID_child
  3083.     AND
  3084.         d.PAT_ID_having = tie.PAT_ID_having;
  3085. END
  3086. GO
  3087. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3088. -- itST_atLocation_PR_isPlaying instead of INSERT trigger on lST_atLocation_PR_isPlaying
  3089. -----------------------------------------------------------------------------------------------------------------------
  3090. CREATE TRIGGER [dbo].[itST_atLocation_PR_isPlaying] ON [dbo].[lST_atLocation_PR_isPlaying]
  3091. INSTEAD OF INSERT
  3092. AS
  3093. BEGIN
  3094.     SET NOCOUNT ON;
  3095.     DECLARE @now datetime2(7) = sysdatetime();
  3096.     DECLARE @maxVersion int;
  3097.     DECLARE @currentVersion int;
  3098.     DECLARE @inserted TABLE (
  3099.         Metadata_ST_atLocation_PR_isPlaying int not null,
  3100.         ST_atLocation_PR_isPlaying_ChangedAt datetime not null,
  3101.         ST_atLocation_PR_isPlaying_Version bigint not null,
  3102.         ST_atLocation_PR_isPlaying_StatementType char(1) not null,
  3103.         ST_ID_atLocation int not null,
  3104.         PR_ID_isPlaying int not null,
  3105.         primary key (
  3106.             ST_atLocation_PR_isPlaying_Version,
  3107.             ST_ID_atLocation,
  3108.             PR_ID_isPlaying
  3109.         )
  3110.     );
  3111.     INSERT INTO @inserted
  3112.     SELECT
  3113.         ISNULL(i.Metadata_ST_atLocation_PR_isPlaying, 0),
  3114.         ISNULL(i.ST_atLocation_PR_isPlaying_ChangedAt, @now),
  3115.         DENSE_RANK() OVER (
  3116.             PARTITION BY
  3117.                 ST_ID_atLocation,
  3118.                 PR_ID_isPlaying
  3119.             ORDER BY
  3120.                 ISNULL(i.ST_atLocation_PR_isPlaying_ChangedAt, @now)
  3121.         ),
  3122.         'X',
  3123.         i.ST_ID_atLocation,
  3124.         i.PR_ID_isPlaying
  3125.     FROM
  3126.         inserted i
  3127.     WHERE
  3128.         i.ST_ID_atLocation is not null
  3129.     AND
  3130.         i.PR_ID_isPlaying is not null;
  3131.     INSERT INTO [dbo].[ST_atLocation_PR_isPlaying] (
  3132.         Metadata_ST_atLocation_PR_isPlaying,
  3133.         ST_ID_atLocation,
  3134.         PR_ID_isPlaying
  3135.     )
  3136.     SELECT
  3137.         i.Metadata_ST_atLocation_PR_isPlaying,
  3138.         i.ST_ID_atLocation,
  3139.         i.PR_ID_isPlaying
  3140.     FROM
  3141.         @inserted i
  3142.     LEFT JOIN
  3143.         [dbo].[ST_atLocation_PR_isPlaying] tie
  3144.     ON
  3145.         tie.ST_ID_atLocation = i.ST_ID_atLocation
  3146.     AND
  3147.         tie.PR_ID_isPlaying = i.PR_ID_isPlaying
  3148.     WHERE
  3149.         tie.PR_ID_isPlaying is null;
  3150. END
  3151. GO
  3152. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3153. -- dtST_atLocation_PR_isPlaying instead of DELETE trigger on lST_atLocation_PR_isPlaying
  3154. -----------------------------------------------------------------------------------------------------------------------
  3155. CREATE TRIGGER [dbo].[dtST_atLocation_PR_isPlaying] ON [dbo].[lST_atLocation_PR_isPlaying]
  3156. INSTEAD OF DELETE
  3157. AS
  3158. BEGIN
  3159.     SET NOCOUNT ON;
  3160.     DELETE tie
  3161.     FROM
  3162.         [dbo].[ST_atLocation_PR_isPlaying] tie
  3163.     JOIN
  3164.         deleted d
  3165.     ON
  3166.         d.ST_atLocation_PR_isPlaying_ChangedAt = tie.ST_atLocation_PR_isPlaying_ChangedAt
  3167.     AND
  3168.         d.ST_ID_atLocation = tie.ST_ID_atLocation
  3169.     AND
  3170.         d.PR_ID_isPlaying = tie.PR_ID_isPlaying;
  3171. END
  3172. GO
  3173. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3174. -- itPE_at_PR_wasPlayed instead of INSERT trigger on lPE_at_PR_wasPlayed
  3175. -----------------------------------------------------------------------------------------------------------------------
  3176. CREATE TRIGGER [dbo].[itPE_at_PR_wasPlayed] ON [dbo].[lPE_at_PR_wasPlayed]
  3177. INSTEAD OF INSERT
  3178. AS
  3179. BEGIN
  3180.     SET NOCOUNT ON;
  3181.     DECLARE @now datetime2(7) = sysdatetime();
  3182.     DECLARE @maxVersion int;
  3183.     DECLARE @currentVersion int;
  3184.     DECLARE @inserted TABLE (
  3185.         Metadata_PE_at_PR_wasPlayed int not null,
  3186.         PE_ID_at int not null,
  3187.         PR_ID_wasPlayed int not null,
  3188.         primary key (
  3189.             PE_ID_at
  3190.         )
  3191.     );
  3192.     INSERT INTO @inserted
  3193.     SELECT
  3194.         ISNULL(i.Metadata_PE_at_PR_wasPlayed, 0),
  3195.         i.PE_ID_at,
  3196.         i.PR_ID_wasPlayed
  3197.     FROM
  3198.         inserted i
  3199.     WHERE
  3200.         i.PE_ID_at is not null;
  3201.     INSERT INTO [dbo].[PE_at_PR_wasPlayed] (
  3202.         Metadata_PE_at_PR_wasPlayed,
  3203.         PE_ID_at,
  3204.         PR_ID_wasPlayed
  3205.     )
  3206.     SELECT
  3207.         i.Metadata_PE_at_PR_wasPlayed,
  3208.         i.PE_ID_at,
  3209.         i.PR_ID_wasPlayed
  3210.     FROM
  3211.         @inserted i
  3212.     LEFT JOIN
  3213.         [dbo].[PE_at_PR_wasPlayed] tie
  3214.     ON
  3215.         tie.PE_ID_at = i.PE_ID_at
  3216.     WHERE
  3217.         tie.PE_ID_at is null;
  3218. END
  3219. GO
  3220. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3221. -- dtPE_at_PR_wasPlayed instead of DELETE trigger on lPE_at_PR_wasPlayed
  3222. -----------------------------------------------------------------------------------------------------------------------
  3223. CREATE TRIGGER [dbo].[dtPE_at_PR_wasPlayed] ON [dbo].[lPE_at_PR_wasPlayed]
  3224. INSTEAD OF DELETE
  3225. AS
  3226. BEGIN
  3227.     SET NOCOUNT ON;
  3228.     DELETE tie
  3229.     FROM
  3230.         [dbo].[PE_at_PR_wasPlayed] tie
  3231.     JOIN
  3232.         deleted d
  3233.     ON
  3234.         d.PE_ID_at = tie.PE_ID_at;
  3235. END
  3236. GO
  3237. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3238. -- itPE_wasHeld_ST_atLocation instead of INSERT trigger on lPE_wasHeld_ST_atLocation
  3239. -----------------------------------------------------------------------------------------------------------------------
  3240. CREATE TRIGGER [dbo].[itPE_wasHeld_ST_atLocation] ON [dbo].[lPE_wasHeld_ST_atLocation]
  3241. INSTEAD OF INSERT
  3242. AS
  3243. BEGIN
  3244.     SET NOCOUNT ON;
  3245.     DECLARE @now datetime2(7) = sysdatetime();
  3246.     DECLARE @maxVersion int;
  3247.     DECLARE @currentVersion int;
  3248.     DECLARE @inserted TABLE (
  3249.         Metadata_PE_wasHeld_ST_atLocation int not null,
  3250.         PE_ID_wasHeld int not null,
  3251.         ST_ID_atLocation int not null,
  3252.         primary key (
  3253.             PE_ID_wasHeld
  3254.         )
  3255.     );
  3256.     INSERT INTO @inserted
  3257.     SELECT
  3258.         ISNULL(i.Metadata_PE_wasHeld_ST_atLocation, 0),
  3259.         i.PE_ID_wasHeld,
  3260.         i.ST_ID_atLocation
  3261.     FROM
  3262.         inserted i
  3263.     WHERE
  3264.         i.PE_ID_wasHeld is not null;
  3265.     INSERT INTO [dbo].[PE_wasHeld_ST_atLocation] (
  3266.         Metadata_PE_wasHeld_ST_atLocation,
  3267.         PE_ID_wasHeld,
  3268.         ST_ID_atLocation
  3269.     )
  3270.     SELECT
  3271.         i.Metadata_PE_wasHeld_ST_atLocation,
  3272.         i.PE_ID_wasHeld,
  3273.         i.ST_ID_atLocation
  3274.     FROM
  3275.         @inserted i
  3276.     LEFT JOIN
  3277.         [dbo].[PE_wasHeld_ST_atLocation] tie
  3278.     ON
  3279.         tie.PE_ID_wasHeld = i.PE_ID_wasHeld
  3280.     WHERE
  3281.         tie.PE_ID_wasHeld is null;
  3282. END
  3283. GO
  3284. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3285. -- dtPE_wasHeld_ST_atLocation instead of DELETE trigger on lPE_wasHeld_ST_atLocation
  3286. -----------------------------------------------------------------------------------------------------------------------
  3287. CREATE TRIGGER [dbo].[dtPE_wasHeld_ST_atLocation] ON [dbo].[lPE_wasHeld_ST_atLocation]
  3288. INSTEAD OF DELETE
  3289. AS
  3290. BEGIN
  3291.     SET NOCOUNT ON;
  3292.     DELETE tie
  3293.     FROM
  3294.         [dbo].[PE_wasHeld_ST_atLocation] tie
  3295.     JOIN
  3296.         deleted d
  3297.     ON
  3298.         d.PE_ID_wasHeld = tie.PE_ID_wasHeld;
  3299. END
  3300. GO
  3301. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3302. -- itAC_exclusive_AC_with instead of INSERT trigger on lAC_exclusive_AC_with
  3303. -----------------------------------------------------------------------------------------------------------------------
  3304. CREATE TRIGGER [dbo].[itAC_exclusive_AC_with] ON [dbo].[lAC_exclusive_AC_with]
  3305. INSTEAD OF INSERT
  3306. AS
  3307. BEGIN
  3308.     SET NOCOUNT ON;
  3309.     DECLARE @now datetime2(7) = sysdatetime();
  3310.     DECLARE @maxVersion int;
  3311.     DECLARE @currentVersion int;
  3312.     DECLARE @inserted TABLE (
  3313.         Metadata_AC_exclusive_AC_with int not null,
  3314.         AC_exclusive_AC_with_ChangedAt datetime not null,
  3315.         AC_exclusive_AC_with_Version bigint not null,
  3316.         AC_exclusive_AC_with_StatementType char(1) not null,
  3317.         AC_ID_exclusive int not null,
  3318.         AC_ID_with int not null,
  3319.         primary key (
  3320.             AC_exclusive_AC_with_Version,
  3321.             AC_ID_exclusive,
  3322.             AC_ID_with
  3323.         )
  3324.     );
  3325.     INSERT INTO @inserted
  3326.     SELECT
  3327.         ISNULL(i.Metadata_AC_exclusive_AC_with, 0),
  3328.         ISNULL(i.AC_exclusive_AC_with_ChangedAt, @now),
  3329.         DENSE_RANK() OVER (
  3330.             PARTITION BY
  3331.                 AC_ID_exclusive,
  3332.                 AC_ID_with
  3333.             ORDER BY
  3334.                 ISNULL(i.AC_exclusive_AC_with_ChangedAt, @now)
  3335.         ),
  3336.         'X',
  3337.         i.AC_ID_exclusive,
  3338.         i.AC_ID_with
  3339.     FROM
  3340.         inserted i
  3341.     WHERE
  3342.         i.AC_ID_exclusive is not null
  3343.     AND
  3344.         i.AC_ID_with is not null;
  3345.     SELECT
  3346.         @maxVersion = max(AC_exclusive_AC_with_Version),
  3347.         @currentVersion = 0
  3348.     FROM
  3349.         @inserted;
  3350.     WHILE (@currentVersion < @maxVersion)
  3351.     BEGIN
  3352.         SET @currentVersion = @currentVersion + 1;
  3353.         UPDATE v
  3354.         SET
  3355.             v.AC_exclusive_AC_with_StatementType =
  3356.                 CASE
  3357.                     WHEN tie.AC_exclusive_AC_with_ChangedAt is not null
  3358.                     THEN 'D' -- duplicate
  3359.                     WHEN [dbo].[rfAC_exclusive_AC_with] (
  3360.                         v.AC_ID_exclusive,
  3361.                         v.AC_ID_with,
  3362.                         v.AC_exclusive_AC_with_ChangedAt
  3363.                     ) > 0
  3364.                     THEN 'R' -- restatement
  3365.                     ELSE 'N' -- new statement
  3366.                 END
  3367.         FROM
  3368.             @inserted v
  3369.         LEFT JOIN
  3370.             [dbo].[AC_exclusive_AC_with] tie
  3371.         ON
  3372.             tie.AC_exclusive_AC_with_ChangedAt = v.AC_exclusive_AC_with_ChangedAt
  3373.         AND
  3374.             tie.AC_ID_exclusive = v.AC_ID_exclusive
  3375.         AND
  3376.             tie.AC_ID_with = v.AC_ID_with
  3377.         WHERE
  3378.             v.AC_exclusive_AC_with_Version = @currentVersion;
  3379.         INSERT INTO [dbo].[AC_exclusive_AC_with] (
  3380.             Metadata_AC_exclusive_AC_with,
  3381.             AC_ID_exclusive,
  3382.             AC_ID_with
  3383.         )
  3384.         SELECT
  3385.             Metadata_AC_exclusive_AC_with,
  3386.             AC_ID_exclusive,
  3387.             AC_ID_with
  3388.         FROM
  3389.             @inserted
  3390.         WHERE
  3391.             AC_exclusive_AC_with_Version = @currentVersion
  3392.         AND
  3393.             AC_exclusive_AC_with_StatementType in ('N');
  3394.     END
  3395. END
  3396. GO
  3397. -- UPDATE trigger -----------------------------------------------------------------------------------------------------
  3398. -- utAC_exclusive_AC_with instead of UPDATE trigger on lAC_exclusive_AC_with
  3399. -----------------------------------------------------------------------------------------------------------------------
  3400. CREATE TRIGGER [dbo].[utAC_exclusive_AC_with] ON [dbo].[lAC_exclusive_AC_with]
  3401. INSTEAD OF UPDATE
  3402. AS
  3403. BEGIN
  3404.     SET NOCOUNT ON;
  3405.     DECLARE @now datetime2(7) = sysdatetime();
  3406.     INSERT INTO [dbo].[AC_exclusive_AC_with] (
  3407.         Metadata_AC_exclusive_AC_with,
  3408.         AC_ID_exclusive,
  3409.         AC_ID_with,
  3410.         AC_exclusive_AC_with_ChangedAt
  3411.     )
  3412.     SELECT
  3413.         i.Metadata_AC_exclusive_AC_with,
  3414.         i.AC_ID_exclusive,
  3415.         i.AC_ID_with,
  3416.         CASE WHEN UPDATE(AC_exclusive_AC_with_ChangedAt) THEN i.AC_exclusive_AC_with_ChangedAt ELSE @now END
  3417.     FROM
  3418.         inserted i
  3419.     LEFT JOIN
  3420.         [dbo].[AC_exclusive_AC_with] tie
  3421.     ON
  3422.         tie.AC_ID_exclusive = i.AC_ID_exclusive
  3423.     OR
  3424.         tie.AC_ID_with = i.AC_ID_with
  3425.     WHERE
  3426.         tie.AC_ID_with is null
  3427.     AND
  3428.         [dbo].[rfAC_exclusive_AC_with](
  3429.             i.AC_ID_exclusive,
  3430.             i.AC_ID_with,
  3431.             i.AC_exclusive_AC_with_ChangedAt
  3432.         ) = 0;
  3433. END
  3434. GO
  3435. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3436. -- dtAC_exclusive_AC_with instead of DELETE trigger on lAC_exclusive_AC_with
  3437. -----------------------------------------------------------------------------------------------------------------------
  3438. CREATE TRIGGER [dbo].[dtAC_exclusive_AC_with] ON [dbo].[lAC_exclusive_AC_with]
  3439. INSTEAD OF DELETE
  3440. AS
  3441. BEGIN
  3442.     SET NOCOUNT ON;
  3443.     DELETE tie
  3444.     FROM
  3445.         [dbo].[AC_exclusive_AC_with] tie
  3446.     JOIN
  3447.         deleted d
  3448.     ON
  3449.         d.AC_exclusive_AC_with_ChangedAt = tie.AC_exclusive_AC_with_ChangedAt
  3450.     AND
  3451.        (
  3452.             d.AC_ID_exclusive = tie.AC_ID_exclusive
  3453.         OR
  3454.             d.AC_ID_with = tie.AC_ID_with
  3455.        );
  3456. END
  3457. GO
  3458. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3459. -- itPE_subset_EV_of instead of INSERT trigger on lPE_subset_EV_of
  3460. -----------------------------------------------------------------------------------------------------------------------
  3461. CREATE TRIGGER [dbo].[itPE_subset_EV_of] ON [dbo].[lPE_subset_EV_of]
  3462. INSTEAD OF INSERT
  3463. AS
  3464. BEGIN
  3465.     SET NOCOUNT ON;
  3466.     DECLARE @now datetime2(7) = sysdatetime();
  3467.     DECLARE @maxVersion int;
  3468.     DECLARE @currentVersion int;
  3469.     DECLARE @inserted TABLE (
  3470.         Metadata_PE_subset_EV_of int not null,
  3471.         PE_ID_subset int not null,
  3472.         EV_ID_of int not null,
  3473.         primary key (
  3474.             PE_ID_subset,
  3475.             EV_ID_of
  3476.         )
  3477.     );
  3478.     INSERT INTO @inserted
  3479.     SELECT
  3480.         ISNULL(i.Metadata_PE_subset_EV_of, 0),
  3481.         i.PE_ID_subset,
  3482.         i.EV_ID_of
  3483.     FROM
  3484.         inserted i
  3485.     WHERE
  3486.         i.PE_ID_subset is not null
  3487.     AND
  3488.         i.EV_ID_of is not null;
  3489.     INSERT INTO [dbo].[PE_subset_EV_of] (
  3490.         Metadata_PE_subset_EV_of,
  3491.         PE_ID_subset,
  3492.         EV_ID_of
  3493.     )
  3494.     SELECT
  3495.         i.Metadata_PE_subset_EV_of,
  3496.         i.PE_ID_subset,
  3497.         i.EV_ID_of
  3498.     FROM
  3499.         @inserted i
  3500.     LEFT JOIN
  3501.         [dbo].[PE_subset_EV_of] tie
  3502.     ON
  3503.         tie.PE_ID_subset = i.PE_ID_subset
  3504.     OR
  3505.         tie.EV_ID_of = i.EV_ID_of
  3506.     WHERE
  3507.         tie.EV_ID_of is null;
  3508. END
  3509. GO
  3510. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3511. -- dtPE_subset_EV_of instead of DELETE trigger on lPE_subset_EV_of
  3512. -----------------------------------------------------------------------------------------------------------------------
  3513. CREATE TRIGGER [dbo].[dtPE_subset_EV_of] ON [dbo].[lPE_subset_EV_of]
  3514. INSTEAD OF DELETE
  3515. AS
  3516. BEGIN
  3517.     SET NOCOUNT ON;
  3518.     DELETE tie
  3519.     FROM
  3520.         [dbo].[PE_subset_EV_of] tie
  3521.     JOIN
  3522.         deleted d
  3523.     ON
  3524.        (
  3525.             d.PE_ID_subset = tie.PE_ID_subset
  3526.         OR
  3527.             d.EV_ID_of = tie.EV_ID_of
  3528.        );
  3529. END
  3530. GO
  3531. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3532. -- itPE_in_AC_wasCast instead of INSERT trigger on lPE_in_AC_wasCast
  3533. -----------------------------------------------------------------------------------------------------------------------
  3534. CREATE TRIGGER [dbo].[itPE_in_AC_wasCast] ON [dbo].[lPE_in_AC_wasCast]
  3535. INSTEAD OF INSERT
  3536. AS
  3537. BEGIN
  3538.     SET NOCOUNT ON;
  3539.     DECLARE @now datetime2(7) = sysdatetime();
  3540.     DECLARE @maxVersion int;
  3541.     DECLARE @currentVersion int;
  3542.     DECLARE @inserted TABLE (
  3543.         Metadata_PE_in_AC_wasCast int not null,
  3544.         PE_ID_in int not null,
  3545.         AC_ID_wasCast int not null,
  3546.         primary key (
  3547.             PE_ID_in,
  3548.             AC_ID_wasCast
  3549.         )
  3550.     );
  3551.     INSERT INTO @inserted
  3552.     SELECT
  3553.         ISNULL(i.Metadata_PE_in_AC_wasCast, 0),
  3554.         i.PE_ID_in,
  3555.         i.AC_ID_wasCast
  3556.     FROM
  3557.         inserted i
  3558.     WHERE
  3559.         i.PE_ID_in is not null
  3560.     AND
  3561.         i.AC_ID_wasCast is not null;
  3562.     INSERT INTO [dbo].[PE_in_AC_wasCast] (
  3563.         Metadata_PE_in_AC_wasCast,
  3564.         PE_ID_in,
  3565.         AC_ID_wasCast
  3566.     )
  3567.     SELECT
  3568.         i.Metadata_PE_in_AC_wasCast,
  3569.         i.PE_ID_in,
  3570.         i.AC_ID_wasCast
  3571.     FROM
  3572.         @inserted i
  3573.     LEFT JOIN
  3574.         [dbo].[PE_in_AC_wasCast] tie
  3575.     ON
  3576.         tie.PE_ID_in = i.PE_ID_in
  3577.     AND
  3578.         tie.AC_ID_wasCast = i.AC_ID_wasCast
  3579.     WHERE
  3580.         tie.AC_ID_wasCast is null;
  3581. END
  3582. GO
  3583. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3584. -- dtPE_in_AC_wasCast instead of DELETE trigger on lPE_in_AC_wasCast
  3585. -----------------------------------------------------------------------------------------------------------------------
  3586. CREATE TRIGGER [dbo].[dtPE_in_AC_wasCast] ON [dbo].[lPE_in_AC_wasCast]
  3587. INSTEAD OF DELETE
  3588. AS
  3589. BEGIN
  3590.     SET NOCOUNT ON;
  3591.     DELETE tie
  3592.     FROM
  3593.         [dbo].[PE_in_AC_wasCast] tie
  3594.     JOIN
  3595.         deleted d
  3596.     ON
  3597.         d.PE_ID_in = tie.PE_ID_in
  3598.     AND
  3599.         d.AC_ID_wasCast = tie.AC_ID_wasCast;
  3600. END
  3601. GO
  3602. -- Insert trigger -----------------------------------------------------------------------------------------------------
  3603. -- itAC_part_PR_in_RAT_got instead of INSERT trigger on lAC_part_PR_in_RAT_got
  3604. -----------------------------------------------------------------------------------------------------------------------
  3605. CREATE TRIGGER [dbo].[itAC_part_PR_in_RAT_got] ON [dbo].[lAC_part_PR_in_RAT_got]
  3606. INSTEAD OF INSERT
  3607. AS
  3608. BEGIN
  3609.     SET NOCOUNT ON;
  3610.     DECLARE @now datetime2(7) = sysdatetime();
  3611.     DECLARE @maxVersion int;
  3612.     DECLARE @currentVersion int;
  3613.     DECLARE @inserted TABLE (
  3614.         Metadata_AC_part_PR_in_RAT_got int not null,
  3615.         AC_part_PR_in_RAT_got_ChangedAt datetime not null,
  3616.         AC_part_PR_in_RAT_got_Version bigint not null,
  3617.         AC_part_PR_in_RAT_got_StatementType char(1) not null,
  3618.         AC_ID_part int not null,
  3619.         PR_ID_in int not null,
  3620.         RAT_ID_got tinyint not null,
  3621.         primary key (
  3622.             AC_part_PR_in_RAT_got_Version,
  3623.             AC_ID_part,
  3624.             PR_ID_in
  3625.         )
  3626.     );
  3627.     INSERT INTO @inserted
  3628.     SELECT
  3629.         ISNULL(i.Metadata_AC_part_PR_in_RAT_got, 0),
  3630.         ISNULL(i.AC_part_PR_in_RAT_got_ChangedAt, @now),
  3631.         DENSE_RANK() OVER (
  3632.             PARTITION BY
  3633.                 AC_ID_part,
  3634.                 PR_ID_in
  3635.             ORDER BY
  3636.                 ISNULL(i.AC_part_PR_in_RAT_got_ChangedAt, @now)
  3637.         ),
  3638.         'X',
  3639.         i.AC_ID_part,
  3640.         i.PR_ID_in,
  3641.         ISNULL(i.RAT_ID_got, [RAT_got].RAT_ID)
  3642.     FROM
  3643.         inserted i
  3644.     LEFT JOIN
  3645.         [dbo].[RAT_Rating] [RAT_got]
  3646.     ON
  3647.         [RAT_got].RAT_Rating = i.got_RAT_Rating
  3648.     WHERE
  3649.         i.AC_ID_part is not null
  3650.     AND
  3651.         i.PR_ID_in is not null;
  3652.     SELECT
  3653.         @maxVersion = max(AC_part_PR_in_RAT_got_Version),
  3654.         @currentVersion = 0
  3655.     FROM
  3656.         @inserted;
  3657.     WHILE (@currentVersion < @maxVersion)
  3658.     BEGIN
  3659.         SET @currentVersion = @currentVersion + 1;
  3660.         UPDATE v
  3661.         SET
  3662.             v.AC_part_PR_in_RAT_got_StatementType =
  3663.                 CASE
  3664.                     WHEN tie.AC_part_PR_in_RAT_got_ChangedAt is not null
  3665.                     THEN 'D' -- duplicate
  3666.                     WHEN [dbo].[rfAC_part_PR_in_RAT_got] (
  3667.                         v.AC_ID_part,
  3668.                         v.PR_ID_in,
  3669.                         v.RAT_ID_got,
  3670.                         v.AC_part_PR_in_RAT_got_ChangedAt
  3671.                     ) > 0
  3672.                     THEN 'R' -- restatement
  3673.                     ELSE 'N' -- new statement
  3674.                 END
  3675.         FROM
  3676.             @inserted v
  3677.         LEFT JOIN
  3678.             [dbo].[AC_part_PR_in_RAT_got] tie
  3679.         ON
  3680.             tie.AC_part_PR_in_RAT_got_ChangedAt = v.AC_part_PR_in_RAT_got_ChangedAt
  3681.         AND
  3682.             tie.AC_ID_part = v.AC_ID_part
  3683.         AND
  3684.             tie.PR_ID_in = v.PR_ID_in
  3685.         AND
  3686.             tie.RAT_ID_got = v.RAT_ID_got
  3687.         WHERE
  3688.             v.AC_part_PR_in_RAT_got_Version = @currentVersion;
  3689.         INSERT INTO [dbo].[AC_part_PR_in_RAT_got] (
  3690.             Metadata_AC_part_PR_in_RAT_got,
  3691.             AC_ID_part,
  3692.             PR_ID_in,
  3693.             RAT_ID_got
  3694.         )
  3695.         SELECT
  3696.             Metadata_AC_part_PR_in_RAT_got,
  3697.             AC_ID_part,
  3698.             PR_ID_in,
  3699.             RAT_ID_got
  3700.         FROM
  3701.             @inserted
  3702.         WHERE
  3703.             AC_part_PR_in_RAT_got_Version = @currentVersion
  3704.         AND
  3705.             AC_part_PR_in_RAT_got_StatementType in ('N');
  3706.     END
  3707. END
  3708. GO
  3709. -- UPDATE trigger -----------------------------------------------------------------------------------------------------
  3710. -- utAC_part_PR_in_RAT_got instead of UPDATE trigger on lAC_part_PR_in_RAT_got
  3711. -----------------------------------------------------------------------------------------------------------------------
  3712. CREATE TRIGGER [dbo].[utAC_part_PR_in_RAT_got] ON [dbo].[lAC_part_PR_in_RAT_got]
  3713. INSTEAD OF UPDATE
  3714. AS
  3715. BEGIN
  3716.     SET NOCOUNT ON;
  3717.     DECLARE @now datetime2(7) = sysdatetime();
  3718.     IF(UPDATE(AC_ID_part))
  3719.         RAISERROR('The identity column AC_ID_part is not updatable.', 16, 1);
  3720.     IF(UPDATE(PR_ID_in))
  3721.         RAISERROR('The identity column PR_ID_in is not updatable.', 16, 1);
  3722.     INSERT INTO [dbo].[AC_part_PR_in_RAT_got] (
  3723.         Metadata_AC_part_PR_in_RAT_got,
  3724.         AC_ID_part,
  3725.         PR_ID_in,
  3726.         RAT_ID_got,
  3727.         AC_part_PR_in_RAT_got_ChangedAt
  3728.     )
  3729.     SELECT
  3730.         i.Metadata_AC_part_PR_in_RAT_got,
  3731.         i.AC_ID_part,
  3732.         i.PR_ID_in,
  3733.         i.RAT_ID_got,
  3734.         CASE WHEN UPDATE(AC_part_PR_in_RAT_got_ChangedAt) THEN i.AC_part_PR_in_RAT_got_ChangedAt ELSE @now END
  3735.     FROM
  3736.         inserted i
  3737.     LEFT JOIN
  3738.         [dbo].[AC_part_PR_in_RAT_got] tie
  3739.     ON
  3740.         tie.AC_ID_part = i.AC_ID_part
  3741.     AND
  3742.         tie.PR_ID_in = i.PR_ID_in
  3743.     WHERE
  3744.         tie.PR_ID_in is null
  3745.     AND
  3746.         [dbo].[rfAC_part_PR_in_RAT_got](
  3747.             i.AC_ID_part,
  3748.             i.PR_ID_in,
  3749.             i.RAT_ID_got,
  3750.             i.AC_part_PR_in_RAT_got_ChangedAt
  3751.         ) = 0;
  3752. END
  3753. GO
  3754. -- DELETE trigger -----------------------------------------------------------------------------------------------------
  3755. -- dtAC_part_PR_in_RAT_got instead of DELETE trigger on lAC_part_PR_in_RAT_got
  3756. -----------------------------------------------------------------------------------------------------------------------
  3757. CREATE TRIGGER [dbo].[dtAC_part_PR_in_RAT_got] ON [dbo].[lAC_part_PR_in_RAT_got]
  3758. INSTEAD OF DELETE
  3759. AS
  3760. BEGIN
  3761.     SET NOCOUNT ON;
  3762.     DELETE tie
  3763.     FROM
  3764.         [dbo].[AC_part_PR_in_RAT_got] tie
  3765.     JOIN
  3766.         deleted d
  3767.     ON
  3768.         d.AC_part_PR_in_RAT_got_ChangedAt = tie.AC_part_PR_in_RAT_got_ChangedAt
  3769.     AND
  3770.         d.AC_ID_part = tie.AC_ID_part
  3771.     AND
  3772.         d.PR_ID_in = tie.PR_ID_in;
  3773. END
  3774. GO
  3775. -- SCHEMA EVOLUTION ---------------------------------------------------------------------------------------------------
  3776. --
  3777. -- The following tables, views, and functions are used to track schema changes
  3778. -- over time, as well as providing every XML that has been 'executed' against
  3779. -- the database.
  3780. --
  3781. -- Schema table -------------------------------------------------------------------------------------------------------
  3782. -- The schema table holds every xml that has been executed against the database
  3783. -----------------------------------------------------------------------------------------------------------------------
  3784. IF Object_ID('_Schema', 'U') IS NULL
  3785.    CREATE TABLE [dbo].[_Schema] (
  3786.       [version] int identity(1, 1) not null primary key,
  3787.       [activation] datetime2(7) not null,
  3788.       [schema] xml not null
  3789.    );
  3790. GO
  3791. -- Insert the XML schema (as of now)
  3792. INSERT INTO [dbo].[_Schema] (
  3793.    [activation],
  3794.    [schema]
  3795. )
  3796. SELECT
  3797.    current_timestamp,
  3798.    N'<schema><metadata format="0.97" temporalization="uni"/><knot mnemonic="PAT" descriptor="ParentalType" identity="tinyint" dataRange="varchar(42)"><metadata capsule="dbo" generator="false"/><layout x="616.06" y="383.39" fixed="false"/></knot><knot mnemonic="PLV" descriptor="ProfessionalLevel" identity="tinyint" dataRange="varchar(42)"><metadata capsule="dbo" generator="false"/><layout x="547.64" y="327.96" fixed="false"/></knot><knot mnemonic="RAT" descriptor="Rating" identity="tinyint" dataRange="varchar(42)"><metadata capsule="dbo" generator="false"/><layout x="867.37" y="247.09" fixed="false"/></knot><knot mnemonic="GEN" descriptor="Gender" identity="bit" dataRange="varchar(42)"><metadata capsule="dbo" generator="false"/><layout x="580.04" y="194.93" fixed="false"/></knot><anchor mnemonic="EV" descriptor="Event" identity="int"><metadata capsule="dbo" generator="true"/><layout x="1112.63" y="240.76" fixed="false"/></anchor><anchor mnemonic="PR" descriptor="Program" identity="int"><metadata capsule="dbo" generator="true"/><attribute mnemonic="NAM" descriptor="Name" dataRange="varchar(42)"><metadata capsule="dbo"/><layout x="993.36" y="419.62" fixed="false"/></attribute><layout x="999.17" y="337.14" fixed="false"/></anchor><anchor mnemonic="ST" descriptor="Stage" identity="int"><metadata capsule="dbo" generator="true"/><attribute mnemonic="NAM" descriptor="Name" timeRange="datetime" dataRange="varchar(42)"><metadata capsule="dbo" restatable="false" idempotent="true"/><layout x="1249.50" y="364.75" fixed="false"/></attribute><attribute mnemonic="LOC" descriptor="Location" dataRange="varchar(42)"><metadata capsule="dbo"/><layout x="1292.59" y="308.91" fixed="false"/></attribute><layout x="1200.71" y="316.61" fixed="false"/></anchor><anchor mnemonic="PE" descriptor="Performance" identity="int"><metadata capsule="dbo" generator="true"/><attribute mnemonic="DAT" descriptor="Date" dataRange="datetime"><metadata capsule="dbo"/><layout x="1087.90" y="16.92" fixed="false"/></attribute><attribute mnemonic="AUD" descriptor="Audience" dataRange="int"><metadata capsule="dbo"/><layout x="1055.59" y="36.48" fixed="false"/></attribute><attribute mnemonic="REV" descriptor="Revenue" dataRange="money"><metadata capsule="dbo"/><layout x="1136.77" y="59.82" fixed="false"/></attribute><layout x="1078.79" y="111.16" fixed="false"/></anchor><anchor mnemonic="AC" descriptor="Actor" identity="int"><metadata capsule="dbo" generator="true"/><attribute mnemonic="NAM" descriptor="Name" timeRange="datetime" dataRange="varchar(42)"><metadata capsule="dbo" restatable="false" idempotent="true"/><layout x="771.78" y="342.28" fixed="false"/></attribute><attribute mnemonic="GEN" descriptor="Gender" knotRange="GEN"><metadata capsule="dbo"/><layout x="642.32" y="227.10" fixed="false"/></attribute><attribute mnemonic="PLV" descriptor="ProfessionalLevel" timeRange="datetime" knotRange="PLV"><metadata capsule="dbo" restatable="false" idempotent="true"/><layout x="634.20" y="311.32" fixed="false"/></attribute><layout x="738.14" y="263.82" fixed="false"/></anchor><tie><anchorRole role="parent" type="AC" identifier="true"/><anchorRole role="child" type="AC" identifier="true"/><knotRole role="having" type="PAT" identifier="true"/><metadata capsule="dbo"/><layout x="679.70" y="337.17" fixed="false"/></tie><tie timeRange="datetime"><anchorRole role="atLocation" type="ST" identifier="true"/><anchorRole role="isPlaying" type="PR" identifier="true"/><metadata capsule="dbo" restatable="false" idempotent="true"/><layout x="1093.51" y="335.53" fixed="false"/></tie><tie><anchorRole role="at" type="PE" identifier="true"/><anchorRole role="wasPlayed" type="PR" identifier="false"/><metadata capsule="dbo"/><layout x="1034.95" y="219.71" fixed="false"/></tie><tie><anchorRole role="wasHeld" type="PE" identifier="true"/><anchorRole role="atLocation" type="ST" identifier="false"/><metadata capsule="dbo"/><layout x="1173.89" y="210.84" fixed="false"/></tie><tie timeRange="datetime"><anchorRole role="exclusive" type="AC" identifier="false"/><anchorRole role="with" type="AC" identifier="false"/><metadata capsule="dbo" restatable="false" idempotent="true"/><layout x="720.47" y="193.69" fixed="false"/></tie><tie><anchorRole role="subset" type="PE" identifier="false"/><anchorRole role="of" type="EV" identifier="false"/><metadata capsule="dbo"/><layout x="1073.97" y="201.16" fixed="false"/></tie><tie><anchorRole role="in" type="PE" identifier="true"/><anchorRole role="wasCast" type="AC" identifier="true"/><metadata capsule="dbo"/><layout x="896.09" y="183.39" fixed="false"/></tie><tie timeRange="datetime"><anchorRole role="part" type="AC" identifier="true"/><anchorRole role="in" type="PR" identifier="true"/><knotRole role="got" type="RAT" identifier="false"/><metadata capsule="dbo" restatable="false" idempotent="true"/><layout x="864.02" y="322.19" fixed="false"/></tie></schema>';
  3799. GO
  3800. -- Anchor view --------------------------------------------------------------------------------------------------------
  3801. -- The anchor view shows information about all the anchors in a schema
  3802. -----------------------------------------------------------------------------------------------------------------------
  3803. IF Object_ID('_Anchor', 'V') IS NOT NULL
  3804. DROP VIEW [dbo].[_Anchor]
  3805. GO
  3806. CREATE VIEW [dbo].[_Anchor]
  3807. AS
  3808. SELECT
  3809.    S.version,
  3810.    S.activation,
  3811.    Nodeset.anchor.value('concat(@mnemonic, "_", @descriptor)', 'nvarchar(max)') as [name],
  3812.    Nodeset.anchor.value('metadata[1]/@capsule', 'nvarchar(max)') as [capsule],
  3813.    Nodeset.anchor.value('@mnemonic', 'nvarchar(max)') as [mnemonic],
  3814.    Nodeset.anchor.value('@descriptor', 'nvarchar(max)') as [descriptor],
  3815.    Nodeset.anchor.value('@identity', 'nvarchar(max)') as [identity],
  3816.    Nodeset.anchor.value('metadata[1]/@generator', 'nvarchar(max)') as [generator],
  3817.    Nodeset.anchor.value('count(attribute)', 'int') as [numberOfAttributes]
  3818. FROM
  3819.    [dbo].[_Schema] S
  3820. CROSS APPLY
  3821.    S.[schema].nodes('/schema/anchor') as Nodeset(anchor);
  3822. GO
  3823. -- Knot view ----------------------------------------------------------------------------------------------------------
  3824. -- The knot view shows information about all the knots in a schema
  3825. -----------------------------------------------------------------------------------------------------------------------
  3826. IF Object_ID('_Knot', 'V') IS NOT NULL
  3827. DROP VIEW [dbo].[_Knot]
  3828. GO
  3829. CREATE VIEW [dbo].[_Knot]
  3830. AS
  3831. SELECT
  3832.    S.version,
  3833.    S.activation,
  3834.    Nodeset.knot.value('concat(@mnemonic, "_", @descriptor)', 'nvarchar(max)') as [name],
  3835.    Nodeset.knot.value('metadata[1]/@capsule', 'nvarchar(max)') as [capsule],
  3836.    Nodeset.knot.value('@mnemonic', 'nvarchar(max)') as [mnemonic],
  3837.    Nodeset.knot.value('@descriptor', 'nvarchar(max)') as [descriptor],
  3838.    Nodeset.knot.value('@identity', 'nvarchar(max)') as [identity],
  3839.    Nodeset.knot.value('metadata[1]/@generator', 'nvarchar(max)') as [generator],
  3840.    Nodeset.knot.value('@dataRange', 'nvarchar(max)') as [dataRange]
  3841. FROM
  3842.    [dbo].[_Schema] S
  3843. CROSS APPLY
  3844.    S.[schema].nodes('/schema/knot') as Nodeset(knot);
  3845. GO
  3846. -- Attribute view -----------------------------------------------------------------------------------------------------
  3847. -- The attribute view shows information about all the attributes in a schema
  3848. -----------------------------------------------------------------------------------------------------------------------
  3849. IF Object_ID('_Attribute', 'V') IS NOT NULL
  3850. DROP VIEW [dbo].[_Attribute]
  3851. GO
  3852. CREATE VIEW [dbo].[_Attribute]
  3853. AS
  3854. SELECT
  3855.    S.version,
  3856.    S.activation,
  3857.    ParentNodeset.anchor.value('concat(@mnemonic, "_")', 'nvarchar(max)') +
  3858.    Nodeset.attribute.value('concat(@mnemonic, "_")', 'nvarchar(max)') +
  3859.    ParentNodeset.anchor.value('concat(@descriptor, "_")', 'nvarchar(max)') +
  3860.    Nodeset.attribute.value('@descriptor', 'nvarchar(max)') as [name],
  3861.    Nodeset.attribute.value('metadata[1]/@capsule', 'nvarchar(max)') as [capsule],
  3862.    Nodeset.attribute.value('@mnemonic', 'nvarchar(max)') as [mnemonic],
  3863.    Nodeset.attribute.value('@descriptor', 'nvarchar(max)') as [descriptor],
  3864.    ParentNodeset.anchor.value('@mnemonic', 'nvarchar(max)') as [anchorMnemonic],
  3865.    ParentNodeset.anchor.value('@descriptor', 'nvarchar(max)') as [anchorDescriptor],
  3866.    ParentNodeset.anchor.value('@identity', 'nvarchar(max)') as [anchorIdentity],
  3867.    Nodeset.attribute.value('@dataRange', 'nvarchar(max)') as [dataRange],
  3868.    Nodeset.attribute.value('@knotRange', 'nvarchar(max)') as [knotRange],
  3869.    Nodeset.attribute.value('@timeRange', 'nvarchar(max)') as [timeRange]
  3870. FROM
  3871.    [dbo].[_Schema] S
  3872. CROSS APPLY
  3873.    S.[schema].nodes('/schema/anchor') as ParentNodeset(anchor)
  3874. OUTER APPLY
  3875.    ParentNodeset.anchor.nodes('attribute') as Nodeset(attribute);
  3876. GO
  3877. -- Tie view -----------------------------------------------------------------------------------------------------------
  3878. -- The tie view shows information about all the ties in a schema
  3879. -----------------------------------------------------------------------------------------------------------------------
  3880. IF Object_ID('_Tie', 'V') IS NOT NULL
  3881. DROP VIEW [dbo].[_Tie]
  3882. GO
  3883. CREATE VIEW [dbo].[_Tie]
  3884. AS
  3885. SELECT
  3886.    S.version,
  3887.    S.activation,
  3888.    REPLACE(Nodeset.tie.query('
  3889.      for $role in *[local-name() = "anchorRole" or local-name() = "knotRole"]
  3890.      return concat($role/@type, "_", $role/@role)
  3891.   ').value('.', 'nvarchar(max)'), ' ', '_') as [name],
  3892.    Nodeset.tie.value('metadata[1]/@capsule', 'nvarchar(max)') as [capsule],
  3893.    Nodeset.tie.value('count(anchorRole) + count(knotRole)', 'int') as [numberOfRoles],
  3894.    Nodeset.tie.query('
  3895.      for $role in *[local-name() = "anchorRole" or local-name() = "knotRole"]
  3896.      return string($role/@role)
  3897.   ').value('.', 'nvarchar(max)') as [roles],
  3898.    Nodeset.tie.value('count(anchorRole)', 'int') as [numberOfAnchors],
  3899.    Nodeset.tie.query('
  3900.      for $role in anchorRole
  3901.      return string($role/@type)
  3902.   ').value('.', 'nvarchar(max)') as [anchors],
  3903.    Nodeset.tie.value('count(knotRole)', 'int') as [numberOfKnots],
  3904.    Nodeset.tie.query('
  3905.      for $role in knotRole
  3906.      return string($role/@type)
  3907.   ').value('.', 'nvarchar(max)') as [knots],
  3908.    Nodeset.tie.value('count(*[local-name() = "anchorRole" or local-name() = "knotRole"][@identifier = "true"])', 'int') as [numberOfIdentifiers],
  3909.    Nodeset.tie.query('
  3910.      for $role in *[local-name() = "anchorRole" or local-name() = "knotRole"][@identifier = "true"]
  3911.      return string($role/@type)
  3912.   ').value('.', 'nvarchar(max)') as [identifiers],
  3913.    Nodeset.tie.value('@timeRange', 'nvarchar(max)') as [timeRange]
  3914. FROM
  3915.    [dbo].[_Schema] S
  3916. CROSS APPLY
  3917.    S.[schema].nodes('/schema/tie') as Nodeset(tie);
  3918. GO
  3919. -- Evolution function -------------------------------------------------------------------------------------------------
  3920. -- The evolution function shows what the schema looked like at the given
  3921. -- point in time with additional information about missing or added
  3922. -- modeling components since that time.
  3923. --
  3924. -- @timepoint The point in time to which you would like to travel.
  3925. -----------------------------------------------------------------------------------------------------------------------
  3926. IF Object_ID('_Evolution', 'IF') IS NOT NULL
  3927. DROP FUNCTION [dbo].[_Evolution];
  3928. GO
  3929. CREATE FUNCTION [dbo].[_Evolution] (
  3930.     @timepoint AS DATETIME2(7)
  3931. )
  3932. RETURNS TABLE
  3933. RETURN
  3934. SELECT
  3935.    V.[version],
  3936.    ISNULL(S.[name], T.[name]) AS [name],
  3937.    ISNULL(V.[activation], T.[create_date]) AS [activation],
  3938.    CASE
  3939.       WHEN S.[name] is null THEN
  3940.          CASE
  3941.             WHEN T.[create_date] > (
  3942.                SELECT
  3943.                   ISNULL(MAX([activation]), @timepoint)
  3944.                FROM
  3945.                   [dbo].[_Schema]
  3946.                WHERE
  3947.                   [activation] <= @timepoint
  3948.             ) THEN 'Future'
  3949.             ELSE 'Past'
  3950.          END
  3951.       WHEN T.[name] is null THEN 'Missing'
  3952.       ELSE 'Present'
  3953.    END AS Existence
  3954. FROM (
  3955.    SELECT
  3956.       MAX([version]) as [version],
  3957.       MAX([activation]) as [activation]
  3958.    FROM
  3959.       [dbo].[_Schema]
  3960.    WHERE
  3961.       [activation] <= @timepoint
  3962. ) V
  3963. JOIN (
  3964.    SELECT
  3965.       [name],
  3966.       [version]
  3967.    FROM
  3968.       [dbo].[_Anchor] a
  3969.    UNION ALL
  3970.    SELECT
  3971.       [name],
  3972.       [version]
  3973.    FROM
  3974.       [dbo].[_Knot] k
  3975.    UNION ALL
  3976.    SELECT
  3977.       [name],
  3978.       [version]
  3979.    FROM
  3980.       [dbo].[_Attribute] b
  3981.    UNION ALL
  3982.    SELECT
  3983.       [name],
  3984.       [version]
  3985.    FROM
  3986.       [dbo].[_Tie] t
  3987. ) S
  3988. ON
  3989.    S.[version] = V.[version]
  3990. FULL OUTER JOIN (
  3991.    SELECT
  3992.       [name],
  3993.       [create_date]
  3994.    FROM
  3995.       sys.tables
  3996.    WHERE
  3997.       [type] like '%U%'
  3998.    AND
  3999.       LEFT([name], 1) <> '_'
  4000. ) T
  4001. ON
  4002.    S.[name] = T.[name];
  4003. GO
  4004. -- Drop Script Generator ----------------------------------------------------------------------------------------------
  4005. -- generates a drop script, that must be run separately, dropping everything in an Anchor Modeled database
  4006. -----------------------------------------------------------------------------------------------------------------------
  4007. IF Object_ID('_GenerateDropScript', 'P') IS NOT NULL
  4008. DROP PROCEDURE [dbo].[_GenerateDropScript];
  4009. GO
  4010. CREATE PROCEDURE [dbo]._GenerateDropScript (
  4011.    @exclusionPattern varchar(42) = '[_]%' -- exclude Metadata by default
  4012. )
  4013. AS
  4014. BEGIN
  4015.    WITH objects AS (
  4016.       SELECT
  4017.          'DROP ' + ft.[type] + ' ' + fn.[name] + '; -- ' + fn.[description] as [statement],
  4018.          row_number() OVER (
  4019.             ORDER BY
  4020.                -- order based on type
  4021.                CASE ft.[type]
  4022.                   WHEN 'PROCEDURE' THEN 1
  4023.                   WHEN 'FUNCTION' THEN 2
  4024.                   WHEN 'VIEW' THEN 3
  4025.                   WHEN 'TABLE' THEN 4
  4026.                   ELSE 5
  4027.                END ASC,
  4028.                -- order within type
  4029.                CASE dc.[description]
  4030.                   WHEN 'key generator' THEN 1
  4031.                   WHEN 'latest perspective' THEN 2
  4032.                   WHEN 'current perspective' THEN 3
  4033.                   WHEN 'difference perspective' THEN 4
  4034.                   WHEN 'point-in-time perspective' THEN 5
  4035.                   WHEN 'time traveler' THEN 6
  4036.                   WHEN 'rewinder' THEN 7
  4037.                   WHEN 'assembled view' THEN 8
  4038.                   WHEN 'annex table' THEN 9
  4039.                   WHEN 'posit table' THEN 10
  4040.                   WHEN 'table' THEN 11
  4041.                   WHEN 'restatement finder' THEN 12
  4042.                   ELSE 13
  4043.                END,
  4044.                -- order within description
  4045.                CASE ft.[type]
  4046.                   WHEN 'TABLE' THEN
  4047.                      CASE cl.[class]
  4048.                         WHEN 'Attribute' THEN 1
  4049.                         WHEN 'Attribute Annex' THEN 2
  4050.                         WHEN 'Attribute Posit' THEN 3
  4051.                         WHEN 'Tie' THEN 4
  4052.                         WHEN 'Anchor' THEN 5
  4053.                         WHEN 'Knot' THEN 6
  4054.                         ELSE 7
  4055.                      END
  4056.                   ELSE
  4057.                      CASE cl.[class]
  4058.                         WHEN 'Anchor' THEN 1
  4059.                         WHEN 'Attribute' THEN 2
  4060.                         WHEN 'Attribute Annex' THEN 3
  4061.                         WHEN 'Attribute Posit' THEN 4
  4062.                         WHEN 'Tie' THEN 5
  4063.                         WHEN 'Knot' THEN 6
  4064.                         ELSE 7
  4065.                      END
  4066.                END,
  4067.                -- finally alphabetically
  4068.                o.[name] ASC
  4069.          ) AS [ordinal]
  4070.       FROM
  4071.          sys.objects o
  4072.       JOIN
  4073.          sys.schemas s
  4074.       ON
  4075.          s.[schema_id] = o.[schema_id]
  4076.       CROSS APPLY (
  4077.          SELECT
  4078.             CASE
  4079.                WHEN o.[name] LIKE '[_]%'
  4080.                COLLATE Latin1_General_BIN THEN 'Metadata'
  4081.                WHEN o.[name] LIKE '%[A-Z][A-Z][_][a-z]%[A-Z][A-Z][_][a-z]%'
  4082.                COLLATE Latin1_General_BIN THEN 'Tie'
  4083.                WHEN o.[name] LIKE '%[A-Z][A-Z][_][A-Z][A-Z][A-Z][_][A-Z]%[_]%'
  4084.                COLLATE Latin1_General_BIN THEN 'Attribute'
  4085.                WHEN o.[name] LIKE '%[A-Z][A-Z][A-Z][_][A-Z]%'
  4086.                COLLATE Latin1_General_BIN THEN 'Knot'
  4087.                WHEN o.[name] LIKE '%[A-Z][A-Z][_][A-Z]%'
  4088.                COLLATE Latin1_General_BIN THEN 'Anchor'
  4089.                ELSE 'Other'
  4090.             END
  4091.       ) cl ([class])
  4092.       CROSS APPLY (
  4093.          SELECT
  4094.             CASE o.[type]
  4095.                WHEN 'P' THEN 'PROCEDURE'
  4096.                WHEN 'IF' THEN 'FUNCTION'
  4097.                WHEN 'FN' THEN 'FUNCTION'
  4098.                WHEN 'V' THEN 'VIEW'
  4099.                WHEN 'U' THEN 'TABLE'
  4100.             END
  4101.       ) ft ([type])
  4102.       CROSS APPLY (
  4103.          SELECT
  4104.             CASE
  4105.                WHEN ft.[type] = 'PROCEDURE' AND cl.[class] = 'Anchor' AND o.[name] LIKE 'k%'
  4106.                COLLATE Latin1_General_BIN THEN 'key generator'
  4107.                WHEN ft.[type] = 'FUNCTION' AND o.[name] LIKE 't%'
  4108.                COLLATE Latin1_General_BIN THEN 'time traveler'
  4109.                WHEN ft.[type] = 'FUNCTION' AND o.[name] LIKE 'rf%'
  4110.                COLLATE Latin1_General_BIN THEN 'restatement finder'
  4111.                WHEN ft.[type] = 'FUNCTION' AND o.[name] LIKE 'r%'
  4112.                COLLATE Latin1_General_BIN THEN 'rewinder'
  4113.                WHEN ft.[type] = 'VIEW' AND o.[name] LIKE 'l%'
  4114.                COLLATE Latin1_General_BIN THEN 'latest perspective'
  4115.                WHEN ft.[type] = 'FUNCTION' AND o.[name] LIKE 'p%'
  4116.                COLLATE Latin1_General_BIN THEN 'point-in-time perspective'
  4117.                WHEN ft.[type] = 'VIEW' AND o.[name] LIKE 'n%'
  4118.                COLLATE Latin1_General_BIN THEN 'current perspective'
  4119.                WHEN ft.[type] = 'FUNCTION' AND o.[name] LIKE 'd%'
  4120.                COLLATE Latin1_General_BIN THEN 'difference perspective'
  4121.                WHEN ft.[type] = 'VIEW' AND cl.[class] = 'Attribute'
  4122.                COLLATE Latin1_General_BIN THEN 'assembled view'
  4123.                WHEN ft.[type] = 'TABLE' AND o.[name] LIKE '%Annex'
  4124.                COLLATE Latin1_General_BIN THEN 'annex table'
  4125.                WHEN ft.[type] = 'TABLE' AND o.[name] LIKE '%Posit'
  4126.                COLLATE Latin1_General_BIN THEN 'posit table'
  4127.                WHEN ft.[type] = 'TABLE'
  4128.                COLLATE Latin1_General_BIN THEN 'table'
  4129.                ELSE 'other'
  4130.             END
  4131.       ) dc ([description])
  4132.       CROSS APPLY (
  4133.          SELECT
  4134.             s.[name] + '.' + o.[name],
  4135.             cl.[class] + ' ' + dc.[description]
  4136.       ) fn ([name], [description])
  4137.       WHERE
  4138.          o.[type] IN ('P', 'IF', 'FN', 'V', 'U')
  4139.       AND
  4140.          o.[name] NOT LIKE ISNULL(@exclusionPattern, '')
  4141.    )
  4142.    SELECT
  4143.       [statement] + CHAR(13) as [text()]
  4144.    FROM
  4145.       objects
  4146.    ORDER BY
  4147.       [ordinal]
  4148.    FOR XML PATH('');
  4149. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement