I have send data from downstream to cloud services and it will insert to Cloud SQL Services.
But, currently the data is send up to 12 data per seconds for some hours. The data contains DeviceID
, Timestamp
, HexData
etc. and will be save in tbl_highspeeddata
. Trigger has been created to convert the value of Hex data to ASCII string on this table.
The converting is work good if the data come in with some interval. As for now, there is only less hex data converted as I'm currently stuck on how to make it select range of new row inserted from last get until latest and do converting one-by-one before update back to the column.
After it finish, it will take from the the first new data after previous last processed data until the latest inserted record and repeat converting.
I have tried INSTEAD OF
trigger
SELECT TOP n [primaryKey], *
FROM table
ORDER BY [primaryKey] DESC
SELECT SCOPE_IDENTITY();
and all failed to convert and update every inserted row.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[HSHD_Data_Conversion]
ON [dbo].[HSHD]
AFTER INSERT
AS
BEGIN
/****** HSHD stand for High Speed Hex Data ******/
---------convert HEX to ASCII
DECLARE @ID INT
DECLARE @HEX VARCHAR(MAX)
SET @ID = (SELECT SCOPE_IDENTITY()) --(SELECT TOP 1 ID from HSHD ORDER BY DateCreated DESC)
SET @HEX = (select CONVERT(varbinary(max), Data, 2) from HSHD WHERE ID = @ID)
UPDATE HSHD
SET Data = @HEX
WHERE ID = @ID
---------split command, query and result
DECLARE @Delimiter VARCHAR(8000)
DECLARE @Item VARCHAR(8000)
DECLARE @ItemList VARCHAR(8000)
DECLARE @DelimIndex INT
DECLARE @RowID INT
DECLARE @counter INT
DECLARE @isQuery VARCHAR(1)
SET @RowID = (SELECT SCOPE_IDENTITY()) --(SELECT TOP 1 ID FROM HSHD ORDER BY ID DESC)
SET @ItemList = (SELECT Data FROM HSHD WHERE ID = @RowID)
SET @counter = 0
SET @Delimiter = ':'
SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
WHILE (@DelimIndex != 0)
BEGIN
SET @counter = @counter + 1
SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
--ignore the first element
IF @counter != 1
BEGIN
SET @isQuery = CHARINDEX('?',@Item,0)
IF len(@Item) != 0
BEGIN
IF @isQuery != 0
BEGIN
INSERT INTO HSHD_Data(HSHD,Query) VALUES (@RowID,':' + @Item)
END
ELSE INSERT INTO HSHD_Data(HSHD,Command) VALUES (@RowID, ':' + @Item)
END
END
-- Set @ItemList = @ItemList minus one less item
SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
SET @counter = @counter + 1
END -- End WHILE
IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
BEGIN
SET @Item = @ItemList
SET @isQuery = CHARINDEX('?',@Item,0)
IF len(@Item) != 0
BEGIN
IF @isQuery != 0
BEGIN
INSERT INTO HSHD_Data(HSHD,Query) VALUES (@RowID,':' + @Item)
END
ELSE INSERT INTO HSHD_Data(HSHD,Command) VALUES (@RowID, ':' + @Item)
END
END
ELSE
BEGIN
SET @isQuery = CHARINDEX('?',@ItemList,0)
IF @isQuery != 0
INSERT INTO HSHD_Data(HSHD,Query) VALUES (@RowID, @ItemList)
ELSE
INSERT INTO HSHD_Data(HSHD,Result) VALUES (@RowID, @ItemList)
END
END
My reference:
EDITED:
HSHD data before trigger run:
HSHD data after trigger run:
So, after trigger finish running and convert these 2 data, trigger will start grab for data after :SYSTem:ERRor! #STRing
until the last and converted hex and update back ASCII value to the HEX data.