text: ﻿/*
  Deployment script for Observations
  
  This code was generated by a tool.
  Changes to this file may cause incorrect behavior and will be lost if
  the code is regenerated.
  */
  
  GO
  SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
  
  SET NUMERIC_ROUNDABORT OFF;
  
  
  GO
  :setvar DatabaseName "Observations"
  :setvar DefaultFilePrefix "Observations"
  :setvar DefaultDataPath "C:\Program Files\Microsoft SQL Server\MSSQL13.SAEON\MSSQL\DATA\"
  :setvar DefaultLogPath "C:\Program Files\Microsoft SQL Server\MSSQL13.SAEON\MSSQL\DATA\"
  
  GO
  :on error exit
  GO
  /*
  Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
  To re-enable the script after enabling SQLCMD mode, execute the following:
  SET NOEXEC OFF; 
  */
  :setvar __IsSqlCmdEnabled "True"
  GO
  IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
      BEGIN
          PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
          SET NOEXEC ON;
      END
  
  
  
  GO
  USE [$(DatabaseName)];
  
  GO
  PRINT N'Altering [dbo].[TR_AuditLog_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_AuditLog_Update] ON [dbo].[AuditLog]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          AuditLog src
  		inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.3 20160421 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_DataLog_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_DataLog_Update] ON [dbo].[DataLog]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          DataLog src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160708 TimPN
  GO
  PRINT N'Altering [dbo].[TR_DataSchema_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_DataSchema_Update] ON [dbo].[DataSchema]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          DataSchema src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160715 TimPN
  GO
  PRINT N'Altering [dbo].[TR_DataSource_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_DataSource_Update] ON [dbo].[DataSource]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          DataSource src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.3 20160421 TimPN
  GO
  PRINT N'Altering [dbo].[TR_DataSourceRole_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_DataSourceRole_Update] ON [dbo].[DataSourceRole]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          DataSourceRole src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160715 TimPN
  GO
  PRINT N'Altering [dbo].[TR_DataSourceTransformation_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_DataSourceTransformation_Update] ON [dbo].[DataSourceTransformation]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          DataSourceTransformation src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160715 TimPN
  GO
  PRINT N'Altering [dbo].[TR_DataSourceType_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_DataSourceType_Update] ON [dbo].[DataSourceType]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          DataSourceType src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160715 TimPN
  GO
  PRINT N'Altering [dbo].[TR_ImportBatch_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_ImportBatch_Update] ON [dbo].[ImportBatch]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          ImportBatch src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160715 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Instrument_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Instrument_Update] ON [dbo].[Instrument]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Instrument src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.4 20160508 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Instrument_DataSource_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Instrument_DataSource_Update] ON [dbo].[Instrument_DataSource]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Instrument_DataSource src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.9 20160727 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Instrument_Sensor_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Instrument_Sensor_Update] ON [dbo].[Instrument_Sensor]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Instrument_Sensor src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --> Added 2.0.5 20160530 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Observation_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Observation_Update] ON [dbo].[Observation]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Observation src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Offering_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Offering_Update] ON [dbo].[Offering]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Offering src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Organisation_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Organisation_Update] ON [dbo].[Organisation]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Organisation src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --> Changed 2.0.15 20161102 TimPN
  --< Added 2.0.4 20160508 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Organisation_Instrument_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Organisation_Instrument_Update] ON [dbo].[Organisation_Instrument]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Organisation_Instrument src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160530 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Organisation_Site_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Organisation_Site_Update] ON [dbo].[Organisation_Site]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Organisation_Site src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160530 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Organisation_Station_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Organisation_Station_Update] ON [dbo].[Organisation_Station]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Organisation_Station src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160530 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_OrganisationRole_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_OrganisationRole_Update] ON [dbo].[OrganisationRole]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          OrganisationRole src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Phenomenon_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Phenomenon_Update] ON [dbo].[Phenomenon]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Phenomenon src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_PhenomenonOffering_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_PhenomenonOffering_Update] ON [dbo].[PhenomenonOffering]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          PhenomenonOffering src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_PhenomenonUOM_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_PhenomenonUOM_Update] ON [dbo].[PhenomenonUOM]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          PhenomenonUOM src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Programme_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Programme_Update] ON [dbo].[Programme]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Programme src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160511 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Project_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Project_Update] ON [dbo].[Project]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Project src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160511 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Project_Station_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Project_Station_Update] ON [dbo].[Project_Station]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Project_Station src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160527 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_SchemaColumn_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_SchemaColumn_Update] ON [dbo].[SchemaColumn]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          SchemaColumn src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.11 20160908 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_SchemaColumnType_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_SchemaColumnType_Update] ON [dbo].[SchemaColumnType]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          SchemaColumnType src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.11 20160908 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Sensor_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Sensor_Update] ON [dbo].[Sensor]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Sensor src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Site_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Site_Update] ON [dbo].[Site]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update 
          src 
      set 
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Site src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.3 20160421 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Station_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Station_Update] ON [dbo].[Station]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Station src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.3 20160421 TimPN
  GO
  PRINT N'Altering [dbo].[TR_Station_Instrument_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Station_Instrument_Update] ON [dbo].[Station_Instrument]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Station_Instrument src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.5 20160512 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_Status_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_Status_Update] ON [dbo].[Status]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          Status src
          inner join inserted ins 
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_StatusReason_Update]...';
  
  
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER OFF;
  
  
  GO
  ALTER TRIGGER [dbo].[TR_StatusReason_Update] ON [dbo].[StatusReason]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          StatusReason src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --> Added 2.0.9 20160823 TimPN
  GO
  SET ANSI_NULLS, QUOTED_IDENTIFIER ON;
  
  
  GO
  PRINT N'Altering [dbo].[TR_TransformationType_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_TransformationType_Update] ON [dbo].[TransformationType]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          TransformationType src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[TR_UnitOfMeasure_Update]...';
  
  
  GO
  ALTER TRIGGER [dbo].[TR_UnitOfMeasure_Update] ON [dbo].[UnitOfMeasure]
  FOR UPDATE
  AS
  BEGIN
      SET NoCount ON
      Update
          src
      set
  --> Changed 2.0.19 20161205 TimPN
  --		AddedAt = del.AddedAt,
          AddedAt = Coalesce(del.AddedAt, ins.AddedAt, GetDate ()),
  --< Changed 2.0.19 20161205 TimPN
          UpdatedAt = GETDATE()
      from
          UnitOfMeasure src
          inner join inserted ins
              on (ins.ID = src.ID)
  		inner join deleted del
  			on (del.ID = src.ID)
  END
  --< Changed 2.0.15 20161102 TimPN
  --< Added 2.0.8 20160718 TimPN
  GO
  PRINT N'Altering [dbo].[vDataLog]...';
  
  
  GO
  --> Changed 2.0.3 20160503 TimPN
  --Renamed SensorProcedure to Sensor
  --< Changed 2.0.3 20160503 TimPN
  ALTER VIEW [dbo].[vDataLog]
  AS
  
  SELECT 
  d.ID, 
  d.ImportDate,
  
  --> Added 2.0.17 20161128 TimPN
  Site.Name SiteName,
  Station.Name StationName,
  Instrument.Name InstrumentName,
  --< Added 2.0.17 20161128 TimPN
  --> Removed 2.0.17 20161128 TimPN
  --org.Name Organisation,
  --ps.[Description] ProjectSite,
  --st.Name StationName,
  --< Removed 2.0.17 20161128 TimPN
  d.SensorID,
  Sensor.Name SensorName,
  CASE 
      WHEN d.SensorID is null then 1
      ELSE 0
  END SensorInvalid,
  
  d.ValueDate,
  d.InvalidDateValue, 
  CASE 
      WHEN ValueDate is null then 1
      ELSE 0
  END DateValueInvalid,
  
  d.InvalidTimeValue, 
  CASE 
      WHEN InvalidTimeValue is not null then 1
      ELSE 0
  END TimeValueInvalid,
  
  CASE 
      WHEN InvalidDateValue is null AND InvalidTimeValue IS NULL Then ValueDate
      WHEN ValueTime is not null then ValueTime 
  END ValueTime,
  
  
  d.RawValue,
  d.ValueText,
  CASE
      WHEN d.RawValue is null then 1
      ELSE 0
  END RawValueInvalid,	
  
  d.DataValue,
  d.TransformValueText, 
  CASE
      WHEN d.DataValue is null then 1
      ELSE 0
  END DataValueInvalid,
  
  d.PhenomenonOfferingID, 
  CASE
      WHEN d.PhenomenonOfferingID is null then 1
      ELSE 0
  END OfferingInvalid,
  
  --> Changed 2.0.3 20160421 TimPN
  --d.PhenonmenonUOMID, 
  d.PhenomenonUOMID, 
  --< Changed 2.0.3 20160421 TimPN
  CASE
  --> Changed 2.0.3 20160421 TimPN
  --    WHEN d.PhenonmenonUOMID is null then 1
      WHEN d.PhenomenonUOMID is null then 1
  --< Changed 2.0.3 20160421 TimPN
      ELSE 0
  END UOMInvalid,
  
  p.Name PhenomenonName,
  o.Name OfferingName,
  uom.Unit,
  
  d.DataSourceTransformationID,
  tt.Name Transformation,
  d.StatusID,
  s.Name [Status],
  d.ImportBatchID,
  d.RawFieldValue,
  d.Comment
  
  FROM DataLog d
  --> Removed 2.0.17 20161128 TimPN
  --LEFT JOIN Sensor sp
  --    on d.SensorID = sp.ID
  --LEFT JOIN Station st
  --    on sp.StationID = st.ID
  --LEFT JOIN ProjectSite ps
  --    on st.ProjectSiteID = ps.ID
  --LEFT JOIN Organisation org
  --    on ps.OrganisationID = org.ID
  --< Removed 2.0.17 20161128 TimPN
  --> Added 2.0.17 20161128 TimPN
    inner join Sensor 
      on (d.SensorID = Sensor.ID) 
    inner join Instrument_Sensor
      on (Instrument_Sensor.SensorID = Sensor.ID) and
  	   ((Instrument_Sensor.StartDate is null) or (d.ValueDate >= Instrument_Sensor.StartDate)) and
  	   ((Instrument_Sensor.EndDate is null) or (d.ValueDate <= Instrument_Sensor.EndDate))
    inner join Instrument
      on (Instrument_Sensor.InstrumentID = Instrument.ID) and
  	   ((Instrument.StartDate is null) or (d.ValueDate >= Instrument.StartDate )) and
  	   ((Instrument.EndDate is null) or (d.ValueDate <= Instrument.EndDate))
    inner join Station_Instrument
      on (Station_Instrument.InstrumentID = Instrument.ID) and
  	   ((Station_Instrument.StartDate is null) or (d.ValueDate >= Station_Instrument.StartDate)) and
  	   ((Station_Instrument.EndDate is null) or (d.ValueDate <= Station_Instrument.EndDate))
    inner join Station 
      on (Station_Instrument.StationID = Station.ID) and
  	   ((Station.StartDate is null) or (Cast(d.ValueDate as Date) >= Cast(Station.StartDate as Date))) and
  	   ((Station.EndDate is null) or (Cast(d.ValueDate as Date) <= Cast(Station.EndDate as Date)))
    inner join Site
      on (Station.SiteID = Site.ID) and
  	   ((Site.StartDate is null) or  (Cast(d.ValueDate as Date) >= Cast(Site.StartDate as Date))) and
  	   ((Site.EndDate is null) or  (Cast(d.ValueDate as Date) <= Cast(Site.EndDate as Date)))
  --< Added 2.0.17 20161128 TimPN
  LEFT JOIN PhenomenonOffering po
   ON d.PhenomenonOfferingID = po.ID
  LEFT JOIN Phenomenon p
      on po.PhenomenonID = p.ID
  LEFT JOIN Offering o
      on po.OfferingID = o.ID
  LEFT JOIN PhenomenonUOM pu
  --> Changed 2.0.3 20160421 TimPN
  --    on d.PhenonmenonUOMID = pu.ID
      on d.PhenomenonUOMID = pu.ID
  --< Changed 2.0.3 20160421 TimPN
  LEFT JOIN UnitOfMeasure uom
      on pu.UnitOfMeasureID = uom.ID
  LEFT JOIN DataSourceTransformation ds
      on d.DataSourceTransformationID = ds.ID
  LEFT JOIN TransformationType tt
      on ds.TransformationTypeID = tt.ID
  INNER JOIN [Status] s
      on d.StatusID = s.ID
  GO
  PRINT N'Altering [dbo].[vInventory]...';
  
  
  GO
  --> Changed 2.0.3 20160503 TimPN
  --Renamed SensorProcedure to Sensor
  --< Changed 2.0.3 20160503 TimPN
  --> Removed 2.0.17 20161128 TimPN
  --CREATE VIEW [dbo].[vInventory]
  --AS
  --Select 
  -- ps.Name Site,
  -- s.Name Station,
  -- sp.Name Sensor,
  -- p.Name Phenomenon,
  -- d.StartDate,
  -- d.EndDate
  --FROM Station s with (nolock)
  -- INNER Join ProjectSite ps with (nolock)
  -- on  ps.ID=  s.ProjectSiteID
  --INNER Join Sensor sp with (nolock)
  -- on s.ID = sp.StationID
  --INNER Join Phenomenon p with (nolock)
  -- on  sp.PhenomenonID = p.ID 
  
  --INNER JOIN 
  --(
  -- SELECT SensorID,MIN(ValueDate) StartDate,MAX(ValueDate) EndDate
  --  FROM Observation with (nolock)
  -- Group By SensorID
  --)d
  --ON sp.ID = d.SensorID
  --< Removed 2.0.17 20161128 TimPN
  --> Added 2.0.17 20161128 TimPN
  ALTER VIEW [dbo].[vInventory]
  AS
  Select 
    Site.Name Site,
    Station.Name Station,
    Instrument.Name Instrument,
    Sensor.Name Sensor,
    p.Name Phenomenon,
    d.StartDate,
    d.EndDate
  from
    Sensor 
    INNER JOIN 
    (
       SELECT SensorID,MIN(ValueDate) StartDate,MAX(ValueDate) EndDate
       FROM Observation
       Group By SensorID
    ) d
      ON (Sensor.ID = d.SensorID)
    inner join Instrument_Sensor
      on (Instrument_Sensor.SensorID = Sensor.ID) and
  	   ((Instrument_Sensor.StartDate is null) or (d.StartDate >= Instrument_Sensor.StartDate)) and
  	   ((Instrument_Sensor.EndDate is null) or (d.EndDate <= Instrument_Sensor.EndDate))
    inner join Instrument
      on (Instrument_Sensor.InstrumentID = Instrument.ID) and
  	   ((Instrument.StartDate is null) or (d.StartDate >= Instrument.StartDate )) and
  	   ((Instrument.EndDate is null) or (d.EndDate <= Instrument.EndDate))
    inner join Station_Instrument
      on (Station_Instrument.InstrumentID = Instrument.ID) and
  	   ((Station_Instrument.StartDate is null) or (d.StartDate >= Station_Instrument.StartDate)) and
  	   ((Station_Instrument.EndDate is null) or (d.EndDate <= Station_Instrument.EndDate))
    inner join Station 
      on (Station_Instrument.StationID = Station.ID) and
  	   ((Station.StartDate is null) or (Cast(d.StartDate as Date) >= Cast(Station.StartDate as Date))) and
  	   ((Station.EndDate is null) or (Cast(d.EndDate as Date) <= Cast(Station.EndDate as Date)))
    inner join Site
      on (Station.SiteID = Site.ID) and
  	   ((Site.StartDate is null) or  (Cast(d.StartDate as Date) >= Cast(Site.StartDate as Date))) and
  	   ((Site.EndDate is null) or  (Cast(d.EndDate as Date) <= Cast(Site.EndDate as Date)))
    inner join Phenomenon p 
     on (Sensor.PhenomenonID = p.ID )
  --< Added 2.0.17 20161128 TimPN
  GO
  PRINT N'Altering [dbo].[vObservation]...';
  
  
  GO
  --> Changed 2.0.3 20160503 TimPN
  --Renamed SensorProcedure to Sensor
  --< Changed 2.0.3 20160503 TimPN
  ALTER VIEW [dbo].[vObservation]
  AS
  --> Changed 2.0.16 20161107 TimPN
  --> Changed 2.0.3 20160421 TimPN
  --SELECT     o.ID, o.SensorID, o.PhenonmenonOfferingID, o.PhenonmenonUOMID, o.UserId, o.RawValue, o.DataValue, o.ImportBatchID, o.ValueDate, 
  --SELECT     o.ID, o.SensorID, o.PhenomenonOfferingID, o.PhenomenonUOMID, o.UserId, o.RawValue, o.DataValue, o.ImportBatchID, o.ValueDate, 
  --< Changed 2.0.3 20160421 TimPN
  --                      sp.Code AS spCode, sp.Description AS spDesc, sp.Name AS spName, sp.Url AS spURL, sp.DataSchemaID, sp.DataSourceID, sp.PhenomenonID, sp.StationID, 
  --                      ph.Name AS phName, st.Name AS stName, ds.Name AS dsName, ISNULL(dschema.Name,dschema1.Name) AS dschemaName, offer.Name AS offName, offer.ID AS offID, ps.Name AS psName, 
  --                      ps.ID AS psID, org.Name AS orgName, org.ID AS orgID, uom.Unit AS uomUnit, uom.UnitSymbol AS uomSymbol, users.UserName,
  --                      o.Comment
  --FROM         dbo.Observation AS o INNER JOIN
  --                      dbo.Sensor AS sp ON sp.ID = o.SensorID INNER JOIN
  --                      dbo.Phenomenon AS ph ON ph.ID = sp.PhenomenonID INNER JOIN
  --> Changed 2.0.3 20160421 TimPN
  --                      dbo.PhenomenonOffering AS phOff ON phOff.ID = o.PhenonmenonOfferingID INNER JOIN  
  --                      dbo.PhenomenonOffering AS phOff ON phOff.ID = o.PhenomenonOfferingID INNER JOIN  
  --< Changed 2.0.3 20160421 TimPN
  --                      dbo.Offering AS offer ON offer.ID = phOff.OfferingID INNER JOIN
  --> Changed 2.0.3 20160421 TimPN
  --                      dbo.PhenomenonUOM AS puom ON puom.ID = o.PhenonmenonUOMID INNER JOIN
  --                      dbo.PhenomenonUOM AS puom ON puom.ID = o.PhenomenonUOMID INNER JOIN
  --< Changed 2.0.3 20160421 TimPN
  --                      dbo.Station AS st ON st.ID = sp.StationID INNER JOIN
  --                      dbo.DataSource AS ds ON ds.ID = sp.DataSourceID LEFT JOIN
  --                      dbo.DataSchema AS dschema1 ON dschema1.ID = ds.DataSchemaID LEFT JOIN
  --                      dbo.DataSchema AS dschema ON dschema.ID = sp.DataSchemaID INNER JOIN                   
  --                      dbo.ProjectSite AS ps ON ps.ID = st.ProjectSiteID INNER JOIN
  --                      dbo.Organisation AS org ON org.ID = ps.OrganisationID INNER JOIN
  --                      dbo.UnitOfMeasure AS uom ON uom.ID = puom.UnitOfMeasureID INNER JOIN
  --                      dbo.aspnet_Users AS users ON users.UserId = o.UserId 
  SELECT 
    o.ID, o.SensorID, o.PhenomenonOfferingID, o.PhenomenonUOMID, o.RawValue, o.DataValue, o.ImportBatchID, o.ValueDate, o.Comment, 
    o.UserId, aspnet_Users.UserName,
    Status.Name StatusName,
    StatusReason.Name StatusReasonName,
    Offering.ID OfferingID,
    Offering.Name OfferingName,
    UnitOfMeasure.ID UnitOfMeasureID,
    UnitOfMeasure.Unit UnitOfMeasureUnit,
    UnitOfMeasure.UnitSymbol UnitOfMeasureSymbol,
    Sensor.Name SensorName,
    Phenomenon.ID PhenomenonID,
    Phenomenon.Name PhenomenonName,
    DataSource.ID DataSourceID,
    DataSource.Name DataSourceName,
    IsNull(SensorSchema.Name, DataSourceSchema.Name) DataSchemaName,
    Instrument.ID InstrumentID,
    Instrument.Name InstrumentName,
    Station.ID StationID,
    Station.Name StationName,
    Site.ID SiteID,
    Site.Name SiteName,
    Organisation.ID OrganisationID,
    Organisation.Name OrganisationName
  FROM
    Observation o
    left join Status
      on (o.StatusID = Status.ID)
    left join StatusReason
      on (o.StatusReasonID = StatusReason.ID)
    inner join PhenomenonOffering
      on (o.PhenomenonOfferingID = PhenomenonOffering.ID)
    inner join Offering
      on (PhenomenonOffering.OfferingID = Offering.ID)
    inner join PhenomenonUOM
      on (o.PhenomenonUOMID = PhenomenonUOM.ID)
    inner join UnitOfMeasure
      on (PhenomenonUOM.UnitOfMeasureID = UnitOfMeasure.ID)
    inner join Sensor 
      on (o.SensorID = Sensor.ID)
    inner join Phenomenon
      on (Sensor.PhenomenonID = Phenomenon.ID)
    left join DataSchema SensorSchema
      on (Sensor.DataSchemaID = SensorSchema.ID)
    inner join DataSource
      on (Sensor.DataSourceID = DataSource.ID)
    left join DataSchema DataSourceSchema
      on (DataSource.DataSchemaID = DataSourceSchema.ID)
    inner join Instrument_Sensor
      on (Instrument_Sensor.SensorID = Sensor.ID) and
  	   ((Instrument_Sensor.StartDate is null) or (o.ValueDate >= Instrument_Sensor.StartDate)) and
  	   ((Instrument_Sensor.EndDate is null) or (o.ValueDate <= Instrument_Sensor.EndDate))
    inner join Instrument
      on (Instrument_Sensor.InstrumentID = Instrument.ID) and
  	   ((Instrument.StartDate is null) or (o.ValueDate >= Instrument.StartDate )) and
  	   ((Instrument.EndDate is null) or (o.ValueDate <= Instrument.EndDate))
    inner join Station_Instrument
      on (Station_Instrument.InstrumentID = Instrument.ID) and
  	   ((Station_Instrument.StartDate is null) or (o.ValueDate >= Station_Instrument.StartDate)) and
  	   ((Station_Instrument.EndDate is null) or (o.ValueDate <= Station_Instrument.EndDate))
    inner join Station 
      on (Station_Instrument.StationID = Station.ID) and
  	   ((Station.StartDate is null) or (Cast(o.ValueDate as Date) >= Cast(Station.StartDate as Date))) and
  	   ((Station.EndDate is null) or (Cast(o.ValueDate as Date) <= Cast(Station.EndDate as Date)))
    inner join Site
      on (Station.SiteID = Site.ID) and
  	   ((Site.StartDate is null) or  (Cast(o.ValueDate as Date) >= Cast(Site.StartDate as Date))) and
  	   ((Site.EndDate is null) or  (Cast(o.ValueDate as Date) <= Cast(Site.EndDate as Date)))
    inner join Organisation_Site
      on (Organisation_Site.SiteID = Site.ID) and
  	   ((Organisation_Site.StartDate is null) or (Cast(o.ValueDate as Date) >= Cast(Organisation_Site.StartDate as Date))) and
  	   ((Organisation_Site.EndDate is null) or (Cast(o.ValueDate as Date) <= Cast(Organisation_Site.EndDate as Date)))
    inner join Organisation
      on (Organisation_Site.OrganisationID = Organisation.ID)
    inner join aspnet_Users
      on (o.UserId = aspnet_Users.UserId)
  --< Changed 2.0.16 20161107 TimPN
  GO
  PRINT N'Refreshing [dbo].[vObservationRoles]...';
  
  
  GO
  EXECUTE sp_refreshsqlmodule N'[dbo].[vObservationRoles]';
  
  
  GO
  PRINT N'Update complete.';
  
  
  GO
  