0

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:

Instead Of

Newly Added Row

EDITED:

HSHD data before trigger run:

enter image description here

HSHD data after trigger run:

enter image description here

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.

Community
  • 1
  • 1
Luiey
  • 843
  • 2
  • 23
  • 50
  • can you show example of what you query will achieve(expected ouput) and what is not showing.it will be helpfull,if you can provide a repro – TheGameiswar Dec 28 '16 at 04:42
  • @TheGameiswar I have add before trigger output and expected trigger output. Currently the trigger skip as after it finish convert 1 hex, it already has some hex left but it will get the last one only and convert to ASCII. All of this HEX is in a second with different milliseconds of each data – Luiey Dec 28 '16 at 07:07
  • Thanks marc_s for make it better question format :) – Luiey Dec 28 '16 at 08:44

0 Answers0