I am trying to set up a trigger which will run after records are updated by a Applications. I have never built a trigger before nor have had much experience with SQL.
What this script is supposed to do is after a record has been updated, it uses the data in the HPLC_Params table to insert some of that data into two other tables. Those tables being ctc_MdeHeader and ctc_MdeData.
USE [ctc_custom]
GO
/****** Object: Trigger [dbo].[trg_InsertLabData] Script Date: 3/4/2019 8:28:33 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Cam Evenson
-- Create date: Feb 28, 2019
-- Description: Trigger passes records into the MDE tables
-- =============================================
ALTER TRIGGER [dbo].[trg_InsertLabData]
ON [ctc_custom].[dbo].[HPLC_Params]
AFTER UPDATE
AS
BEGIN
DECLARE
@MDEtype VARCHAR(50),
@HeaderID int,
@SampleName VARCHAR(10),
@source nvarchar(30),
@tagid nvarchar(30),
@InsertTime datetime,
@StringValue nvarchar(30),
@Quality int=192,
@Computer nvarchar(50)='',
@UserName nvarchar(50)='',
@DbUTagId int,
@DataType int,
@UseStartTime bit,
@headerSourceId int,
@Time nvarchar(25),
@RecordKey nvarchar(80),
@TestID as int,
@MdeHeaderId int,
@UTagId int,
@TagLETTER nvarchar(5),
@CompoundName nvarchar(30),
@LabTagName nvarchar(30),
@RecordCount int
SET @MDEtype = (SELECT Top 1[Destination] from [HPLC_Params])
SET @SampleName = (SELECT Top 1 [SampleName] FROM [HPLC_Params])
Set @HeaderID = (SELECT Top 1 [utagid] FROM [ctc_config].[dbo].[ctc_processtags] Where [name] like @MDEtype)
SET @InsertTime = (SELECT [Analysis Date] FROM [dbo].[HPLC_Params])
SET @TestID = (SELECT [TestID] FROM [dbo].[HPLC_Params])
-- Create the recordkey based on the time stamp
Set @time=CONVERT(nvarchar(25),@InsertTime,114)
Set @RecordKey= CONVERT(nvarchar(25),@InsertTime,12)+substring(@time,1,2)+substring(@time,4,2)+substring(@time,7,2)+'00' ---substring(@time,10,3)
SELECT
@DbUTagId = t1.utagid,
@DataType = t1.datatype,
@UseStartTime = hs.SelectByStartTime,
@headerSourceId = hs.Id
FROM
[ctc_config]..ctc_v_processtags t1 (NOEXPAND)
INNER JOIN
[ctc_config]..ctc_dssource t2
ON
t1.dssourceid = t2.dssourceid
INNER JOIN
[ctc_config]..ctc_mde_tagheadersource ths
INNER JOIN
[ctc_config]..ctc_mde_headersource hs
ON
ths.HeaderSourceId = hs.Id
on
t1.utagid = ths.ProcessUTagId
WHERE t1.name = @tagId AND t2.name = @source
If @DbUTagId is null or @headersourceid is null Return
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Configure which MDE the data is assigned too
If UPDATE([Validation])
If @MDEtype = 'Evaps'
-- Shift values to Evaps MDE
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'Evaporators')
If @MDEType = 'P-311'
-- Shift values to Fermenter MDE Yeast Spot 1 & checks if there are more then one sample
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'YeastProp')
Set @MdeHeaderId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_header] WHERE [ProductLabel] = @SampleName)
IF @MDEtype = 'A'
-- Processes Fermenter Letter A Values
Set @TagLETTER = 'A'
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'FermA_MDEG')
Set @MdeHeaderId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_header] WHERE [ProductLabel] = @SampleName)
IF @MDEtype = 'B'
-- Processes Fermenter Letter B Values
Set @TagLETTER = 'B'
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'FermB_MDEG')
Set @MdeHeaderId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_header] WHERE [ProductLabel] = @SampleName)
IF @MDEtype = 'C'
-- Processes Fermenter Letter C Values
Set @TagLETTER = 'C'
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'FermC_MDEG')
Set @MdeHeaderId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_header] WHERE [ProductLabel] = @SampleName)
IF @MDEtype = 'D'
-- Processes Fermenter Letter D Values
Set @TagLETTER = 'D'
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'FermD_MDEG')
Set @MdeHeaderId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_header] WHERE [ProductLabel] = @SampleName)
IF @MDEtype = 'YS'
-- Processes Yeast values
Set @headerSourceId = (SELECT [Id] FROM [ctc_config].[dbo].[ctc_mde_headersource] WHERE [Name] = 'Liquefaction')
--Create a new header for this timestamp
DECLARE @RC int
DECLARE @InsertResult int
DECLARE @RowVer timestamp
DECLARE @ProductLabel nvarchar(50)
DECLARE @StartTime datetime
DECLARE @EndTime datetime
DECLARE @GradeNameId int
DECLARE @ManuallyModified bit=1
DECLARE @AllMdCellsHaveValues bit=0
DECLARE @UpdateWhenExists bit
DECLARE @ModificationType nvarchar(20)
IF @UseStartTime = 1
Set @StartTime=@InsertTime
Else
Set @EndTime=@InsertTime
EXECUTE @RC = [ctc_config]..[ctc_sp_MdeHeaderInsert]
@MdeHeaderId OUTPUT
,@InsertResult OUTPUT
,@RowVer OUTPUT
,@HeaderSourceId
,@RecordKey
,@ProductLabel
,@StartTime
,@EndTime
,@GradeNameId
,@ManuallyModified
,@AllMdCellsHaveValues
,@UpdateWhenExists
,@Computer
,@UserName
,@ModificationType
If @MdeHeaderId is null Return
WHILE @TestID is not null
BEGIN
-- We now have our new headerId, we can now stuff the new value
-- Loop through the Data values and insert them into MDE Data
DECLARE @Id int
DECLARE @Comment nvarchar(1024)='HPLC_Write'
DECLARE @NumericValue float
DECLARE @EntrySource tinyint=2
DECLARE @ReadOnly bit=0
DECLARE @OverrideReadOnly bit
DECLARE @DateTimeValue datetime
If @DataType is Null or @DataType<>8
Begin
-- Set the Numeric Value of the Lab Data here
SET @RecordCount = (SELECT count(*) as 'Records' FROM [ctc_config].[dbo].[ctc_mde_value]
WHERE [MdeHeaderId] = @MdeHeaderId GROUP BY [MdeHeaderId] HAVING count(*)>1 )
SET @CompoundName = (SELECT b.CompoundName FROM [ctc_custom].[dbo].[HPLC_Data] a join [ctc_custom].[dbo].[HPLC_Compound] b
on a.CompoundID = b.CompoundID WHERE a.TestID = @TestID)
SET @LabTagName = UPPER(@TagLETTER + '_' + @CompoundName + '_S' + @RecordCount)
SET @UTagId = (SELECT [UTAGID] FROM [ctc_config].[dbo].[ctc_processtags] WHERE [Name] = @LabTagName)
SET @NumericValue = (SELECT [Amount] FROM [ctc_custom].[dbo].[HPLC_Data] a Join [dbo].[HPLC_Compound] b on a.[CompoundID] = b.[CompoundID] WHERE a.[TestID] = @TestID
AND b.CompoundName = @CompoundName)
Set @NumericValue=Cast(@StringValue as Float)
Set @StringValue=Null
End
-- TODO: Set parameter values here.
-- Loop through the Lab Data
EXECUTE @RC = [ctc_config]..[ctc_sp_MdeValueInsert]
@Id OUTPUT
,@InsertResult OUTPUT
,@RowVer OUTPUT
,@mdeheaderid
,@DBUTagId
,@NumericValue
,@StringValue
,@Quality
,@Comment
,@Computer
,@userName
,@EntrySource
,@ReadOnly
,@OverrideReadOnly
,@UpdateWhenExists
,@DateTimeValue
END
END
ENDI am looking for the update of the field Validation from Null to have a value. Would like to get this working, as right now when the trigger is added to the Params table the validation software does not update the values in the Params table.