1

This is the stored procedure I'm using to get 5 digits from an xml:

    CREATE PROCEDURE [dbo].[SP_KINGPRICE_InsertJournalFromPost]
    (
        @ResponseID bigint,
        @TransactionDetailID bigint
    )
    AS
    BEGIN
    DECLARE @info as varchar(max) = '', @Reference as varchar(max) = ''
    SET @info = (SELECT SUBSTRING(Response, CHARINDEX('<GlJournal>',Response) + 11,5) 
                 FROM SysproIntegration..ifmTransactionDetailResponse 
                 WHERE TransactionDetailResponseID = @ResponseID)
    SET @Reference = (SELECT DISTINCT Reference 
                      FROM GenJournalDetail 
                      WHERE Journal = CAST(@info as DECIMAL(5,0)))
    INSERT INTO ZJournalRecords
    (JournalNumber,Reference)

    VALUES (@info,@Reference)
    END

The XML has a tag like this:

    <GLJournal>12345</GLJournal>

If the XML document has only one of these tags, I have no worries. The SP works fine. The problem comes in when there are two nested <GLJournal> tags. The xml would then look something like:

    <GLJournal>
        <SomeTag/>
        <SomeTag2/>
        <GLJournal>12345</GLJournal>
    </GLJournal>

How can I get the 5 digit value of the nested tag? (It will always be 5 digits) I have thought of using a try catch, but that doesn't seem like an elegant solution.

EDIT:
Also, part of the problem is, I don't know when there will be one GlJournal tags, or two.

2 Answers 2

2

I decided to count the number of occurrences of the tag, and based on that, substring a certain position.

DECLARE @info as varchar(max) = '', @Reference as varchar(max) = '', @inputString as varchar(max), @searchString as varchar(max), @numberOfTags as int

SET @searchString = '<GlJournal>'
SET @inputString = (SELECT Response FROM SysproIntegration..ifmTransactionDetailResponse WHERE TransactionDetailResponseID = @ResponseID)
SET @numberOfTags = (LEN(@inputString) - 
                 LEN(REPLACE(@inputString, @searchString, ''))) /
                 LEN(@searchString)

IF @numberOfTags = 1 
BEGIN
SET @info = (SELECT SUBSTRING(Response, CHARINDEX('<GlJournal>',Response) + 11,5) FROM SysproIntegration..ifmTransactionDetailResponse WHERE TransactionDetailResponseID = @ResponseID)
SET @Reference = (SELECT DISTINCT Reference FROM GenJournalDetail WHERE Journal = CAST(@info as DECIMAL(5,0)))
END
ELSE
BEGIN
SET @info = (SELECT SUBSTRING(Response, CHARINDEX('<GlJournal>',Response) + 69,5) FROM SysproIntegration..ifmTransactionDetailResponse WHERE TransactionDetailResponseID = @ResponseID)
SET @Reference = (SELECT DISTINCT Reference FROM GenJournalDetail WHERE Journal = CAST(@info as DECIMAL(5,0)))
END
Sign up to request clarification or add additional context in comments.

Comments

0

Here's an int test you might adapt:



declare @xml xml, @tags smallint

SET @tags = 1

-- Load @xml...
--SELECT @xml = Response 
--FROM SysproIntegration..ifmTransactionDetailResponse 
--WHERE TransactionDetailResponseID = @ResponseID

-- ... or for test...
IF @tags = 1 BEGIN
    -- Test 1 GLJournal tag
    SET @xml = '12346'
END ELSE IF @tags = 2 BEGIN
    -- Test 2 GLJournal tags
    SET @xml = '
        
        
        12345
    '
END ELSE BEGIN
    -- Test no GLJournal tags 
    SET @xml = ''
END    

DECLARE @i int; SET @i = -1

-- Try one tag    
SELECT @i = ParamValues.ID.query('GLJournal').value('.','int')
FROM @xml.nodes('/') as ParamValues(ID)

IF @i = 0   
    -- Try two tags 
    SELECT @i = ParamValues.ID.query('GLJournal').value('.','int')
    FROM @xml.nodes('/GLJournal') as ParamValues(ID)     

SELECT @i    

-- INSERT INTO ZJournalRecords... other stuff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.