1

Please assist me, I am getting null value for all columns in select statement. I have tried so many small changes, but still not getting null values .

DECLARE @XML AS XML,@hDoc AS INT, @SQL NVARCHAR (MAX)
SET @XML = N' <Customer>
       <CustomerID>4</CustomerID>
       <Citycode>BBY</Citycode>
       <TitleCode>1</TitleCode>
       <AccountTypeCode>SV</AccountTypeCode>
       <AccRiskCode>A</AccRiskCode>
       <BankBranchCode>BAU001</BankBranchCode>
   </Customer>'

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT 
    CustomerID, Citycode, TitleCode, AccountTypeCode, AccRiskCode, BankBranchCode
FROM OPENXML(@hDoc, 'Customer')
WITH 
(
CustomerID int  '@CustomerID',
Citycode [varchar](10) '@Citycode',
TitleCode int '@TitleCode',
AccountTypeCode varchar(4) '@AccountTypeCode',
AccRiskCode varchar(4) '@AccRiskCode',
BankBranchCode varchar(10) '@BankBranchCode'
)   

EXEC sp_xml_removedocument @hDoc
GO
1
  • You are mapping the columns with attributes but not elements, just remove all the @ prefixing the mapped names (@CustomerID should be just CustomerID), then it works OK. Commented Apr 24, 2016 at 7:10

1 Answer 1

1

Try this - use the built-in, native XQuery support instead of the deprecated OPENXML :

SELECT
    ID = XC.value('CustomerID[1]', 'int'),
    CityCode = XC.value('Citycode[1]', 'varchar(20)'),
    TitleCode = XC.value('TitleCode[1]', 'int'),
    AccountTypeCode = XC.value('AccountTypeCode[1]', 'varchar(20)'),
    AccRiskCode = XC.value('AccRiskCode[1]', 'varchar(20)'),
    BankBranchCode = XC.value('BankBranchCode[1]', 'varchar(20)')
FROM
    @XML.nodes('/Customer') AS XT(XC)

This returns this output:

enter image description here

Sign up to request clarification or add additional context in comments.

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.