how to write a proc to upload the xml files into above tables

  • Hi

    There are two tables in my database HotelList_Temp, AsiaHotelChildrenAges_Temp

    I need to write a proc to upload the xml files into above tables .xmlfile is in my pc

    xml to table column mapping are given below

    <HOTEL>

    <SEQNO>1</SEQNO>

    <GRADE>5</GRADE> --StarRating

    <PRODUCT>H</PRODUCT>

    <CITYAREA>CKOWM</CITYAREA> --CityLocation

    <CITY>HKG</CITY> --ResortCode

    <CITYAREANAME>Mong Kok </CITYAREANAME> --ResortName

    <SERVICE>H5LPL</SERVICE> --HotelCode

    <REMARK>Only for Europe, Middle East, US and South American Market</REMARK> --Remark

    <SUBSERVICE>---</SUBSERVICE> --SubService

    <T_O>DAHKG</T_O> --T_O

    <HOTELID>100663300</HOTELID> -- HotelId

    <HOTELNAME>LANGHAM PLACE MONGKOK</HOTELNAME> -- HotelName

    <BYPMC></BYPMC>

    <SUPP_TYPE>-</SUPP_TYPE>

    <PROVIDER></PROVIDER> --Provider

    <LEADING_FLAG></LEADING_FLAG> --LeadingFlag

    <CHILDRENAGE> --Children age part will go under HotelChildrenAges

    <FROM_DATE>20080101</FROM_DATE> --FromDate

    <TO_DATE>20501231</TO_DATE> --ToDate

    <CHILDAGE>12</CHILDAGE> --MaxChildAge

    <MINCHILDAGE>2</MINCHILDAGE> --MinChildAge

    </CHILDRENAGE>

    <ADDRESS>

    <ADDR_LINE_1>555 Shanghai Street</ADDR_LINE_1> --AddressLineOne

    <ADDR_LINE_2>Mongkok, Kowloon</ADDR_LINE_2> --AddressLineTwo

    <ADDR_LINE_3></ADDR_LINE_3> --AddressLineThree

    <ADDR_CITY>HONG KONG</ADDR_CITY> --City

    <ADDR_REGION></ADDR_REGION> --Region

    <ADDR_COUNTRY>HK</ADDR_COUNTRY> --Country

    </ADDRESS>

    </HOTEL>.

    please help me any one

  • What have you tried?

    First, let's stick your XML in a variable so that we can play with it: -

    DECLARE @XML XML = '<HOTEL>

    <SEQNO>1</SEQNO>

    <GRADE>5</GRADE>

    <PRODUCT>H</PRODUCT>

    <CITYAREA>CKOWM</CITYAREA>

    <CITY>HKG</CITY>

    <CITYAREANAME>Mong Kok </CITYAREANAME>

    <SERVICE>H5LPL</SERVICE>

    <REMARK>Only for Europe, Middle East, US and South American Market</REMARK>

    <SUBSERVICE>---</SUBSERVICE>

    <T_O>DAHKG</T_O>

    <HOTELID>100663300</HOTELID>

    <HOTELNAME>LANGHAM PLACE MONGKOK</HOTELNAME>

    <BYPMC />

    <SUPP_TYPE>-</SUPP_TYPE>

    <PROVIDER />

    <LEADING_FLAG />

    <CHILDRENAGE>

    <FROM_DATE>20080101</FROM_DATE>

    <TO_DATE>20501231</TO_DATE>

    <CHILDAGE>12</CHILDAGE>

    <MINCHILDAGE>2</MINCHILDAGE>

    </CHILDRENAGE>

    <ADDRESS>

    <ADDR_LINE_1>555 Shanghai Street</ADDR_LINE_1>

    <ADDR_LINE_2>Mongkok, Kowloon</ADDR_LINE_2>

    <ADDR_LINE_3 />

    <ADDR_CITY>HONG KONG</ADDR_CITY>

    <ADDR_REGION />

    <ADDR_COUNTRY>HK</ADDR_COUNTRY>

    </ADDRESS>

    </HOTEL>';

    Right, so parsing the XML can be done like this: -

    SELECT YourXML.xmlData.value(N'(GRADE)[1]', N'INT') AS [StarRating],

    YourXML.xmlData.value(N'(CITYAREA)[1]', N'NVARCHAR(50)') AS [CityLocation],

    YourXML.xmlData.value(N'(CITY)[1]', N'CHAR(3)') AS [ResortCode],

    YourXML.xmlData.value(N'(CITYAREANAME)[1]', N'NVARCHAR(50)') AS [ResortName],

    YourXML.xmlData.value(N'(SERVICE)[1]', N'CHAR(5)') AS [HotelCode],

    YourXML.xmlData.value(N'(REMARK)[1]', N'NVARCHAR(MAX)') AS [Remark],

    YourXML.xmlData.value(N'(SUBSERVICE)[1]', N'NVARCHAR(MAX)') AS [SubService],

    YourXML.xmlData.value(N'(T_O)[1]', N'NVARCHAR(50)') AS [T_O],

    YourXML.xmlData.value(N'(HOTELID)[1]', N'BIGINT') AS [HotelId],

    YourXML.xmlData.value(N'(HOTELNAME)[1]', N'NVARCHAR(MAX)') AS [HotelName],

    YourXML.xmlData.value(N'(PROVIDER)[1]', N'NVARCHAR(MAX)') AS [Provider],

    YourXML.xmlData.value(N'(LEADING_FLAG)[1]', N'NVARCHAR(MAX)') AS [LeadingFlag],

    childrenAgeXml.xmlData.value(N'(FROM_DATE)[1]', N'DATE') AS [FromDate], -- Use DATETIME or DATE please!

    childrenAgeXml.xmlData.value(N'(TO_DATE)[1]', N'DATE') AS [ToDate], -- Use DATETIME or DATE please!

    childrenAgeXml.xmlData.value(N'(CHILDAGE)[1]', N'INT') AS [MaxChildAge],

    childrenAgeXml.xmlData.value(N'(MINCHILDAGE)[1]', N'INT') AS [MinChildAge],

    addressXml.xmlData.value(N'(ADDR_LINE_1)[1]', N'NVARCHAR(MAX)') AS [AddressLineOne],

    addressXml.xmlData.value(N'(ADDR_LINE_2)[1]', N'NVARCHAR(MAX)') AS [AddressLineTwo],

    addressXml.xmlData.value(N'(ADDR_LINE_3)[1]', N'NVARCHAR(MAX)') AS [AddressLineThree],

    addressXml.xmlData.value(N'(ADDR_CITY)[1]', N'NVARCHAR(MAX)') AS [City],

    addressXml.xmlData.value(N'(ADDR_REGION)[1]', N'NVARCHAR(MAX)') AS [Region],

    addressXml.xmlData.value(N'(ADDR_COUNTRY)[1]', N'NVARCHAR(MAX)') AS [Country]

    FROM @XML.nodes(N'/HOTEL') AS YourXML ( xmlData )

    OUTER APPLY @XML.nodes(N'/HOTEL/CHILDRENAGE') AS childrenAgeXml ( xmlData )

    OUTER APPLY @XML.nodes(N'/HOTEL/ADDRESS') AS addressXml ( xmlData );

    Does that help?


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Hi,

    i am trying it in sql server 2008 is it work in it??what i am trying to do is There are two tables in my database of my server HotelList, ChildrenAges

    i am trying to write a proc to upload the xml files into above tables .i had xmlfile in my system

    xml to table column mapping are given below

    <HOTEL>

    <SEQNO>1</SEQNO>

    <GRADE>5</GRADE> --StarRating

    <PRODUCT>H</PRODUCT>

    <CITYAREA>CKOWM</CITYAREA> --CityLocation

    <CITY>HKG</CITY> --ResortCode

    <CITYAREANAME>Mong Kok </CITYAREANAME> --ResortName

    <SERVICE>H5LPL</SERVICE> --HotelCode

    <REMARK>Only for Europe, Middle East, US and South American Market</REMARK> --Remark

    <SUBSERVICE>---</SUBSERVICE> --SubService

    <T_O>DAHKG</T_O> --T_O

    <HOTELID>100663300</HOTELID> -- HotelId

    <HOTELNAME>LANGHAM PLACE MONGKOK</HOTELNAME> -- HotelName

    <BYPMC></BYPMC>

    <SUPP_TYPE>-</SUPP_TYPE>

    <PROVIDER></PROVIDER> --Provider

    <LEADING_FLAG></LEADING_FLAG> --LeadingFlag

    <CHILDRENAGE> --Children age part will go under HotelChildrenAges

    <FROM_DATE>20080101</FROM_DATE> --FromDate

    <TO_DATE>20501231</TO_DATE> --ToDate

    <CHILDAGE>12</CHILDAGE> --MaxChildAge

    <MINCHILDAGE>2</MINCHILDAGE> --MinChildAge

    </CHILDRENAGE>

    <ADDRESS>

    <ADDR_LINE_1>555 Shanghai Street</ADDR_LINE_1> --AddressLineOne

    <ADDR_LINE_2>Mongkok, Kowloon</ADDR_LINE_2> --AddressLineTwo

    <ADDR_LINE_3></ADDR_LINE_3> --AddressLineThree

    <ADDR_CITY>HONG KONG</ADDR_CITY> --City

    <ADDR_REGION></ADDR_REGION> --Region

    <ADDR_COUNTRY>HK</ADDR_COUNTRY> --Country

    </ADDRESS>

    </HOTEL>

  • I had tried to write proceedure as below

    create proc sp_DestAsiaHotelList_Temp

    @xml xml

    as

    begin

    declare @idoc int

    exec sp_xml_preparedocument @idoc OUTPUT, @XML

    insert into DestAsiaHotelList_Temp(HotelID, HotelName, StarRating, CityLocation, ResortCode, ResortName, HotelCode, Remark, Subservice, T_O, Provider, LeadingFlag, AddressLineOne, AddressLineTwo, AddressLineThree, City, Region, Country, DateCreated)

    select(HotelID, HotelName, StarRating, CityLocation, ResortCode, ResortName, HotelCode, Remark, Subservice, T_O, Provider, LeadingFlag, AddressLineOne, AddressLineTwo, AddressLineThree, City, Region, Country)

    FROM OPENXML(@idoc, 'C:\Desktop\HotelListStaticData.xml')

    with

    (

    HotelID varchar(50) 'HOTELID',

    HotelName varchar(200) 'HotelName',

    StarRating varchar(50) 'GRADE',

    CityLocation varchar(100) 'CITYAREA',

    ResortCode varchar(50) 'CITY',

    ResortName varchar(150) 'CITYAREANAME',

    HotelCode varchar(50) 'SERVICE',

    Remark varchar(MAX) 'REMARK',

    Subservice varchar(50) 'SUBSERVICE',

    T_O varchar(100) 'T_O',

    Provider varchar(100) 'PROVIDER',

    LeadingFlag varchar(50) 'LEADING_FLAG',

    AddressLineOne varchar(200) 'ADDR_LINE_1',

    AddressLineTwo varchar(150) 'ADDR_LINE_2',

    AddressLineThree varchar(150) 'ADDR_LINE_3',

    City varchar(100) 'ADDR_CITY',

    Region varchar(100) 'ADDR_REGION',

    Country varchar(100) 'ADDR_COUNTRY',

    )

    --DestAsiaHotelChildrenAges_Temp

    insert into DestAsiaHotelChildrenAges_Temp (HotelCode, FromDate, ToDate, MinChildAge, MaxChildAge, DateCreated)

    select (HotelCode, FromDate, ToDate, MinChildAge, MaxChildAge)

    from openxml(@idoc, 'C:\Desktop\HotelListStaticData.xml')

    with(

    HotelCode varchar(50) 'SERVICE',

    FromDate date 'FROM_DATE',

    ToDate date 'TO_DATE',

    MinChildAge varchar(5) 'MINCHILDAGE',

    MaxChildAge varchar(5) 'CHILDAGE',

    )

    EXEC sp_xml_removedocument @idoc

    end

  • Msg 102, Level 15, State 1, Procedure sp_DestAsiaHotelList_Temp, Line 10

    Incorrect syntax near ','.

    Msg 319, Level 15, State 1, Procedure sp_DestAsiaHotelList_Temp, Line 14

    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

    Msg 102, Level 15, State 1, Procedure sp_DestAsiaHotelList_Temp, Line 41

    Incorrect syntax near ','.

    Msg 319, Level 15, State 1, Procedure sp_DestAsiaHotelList_Temp, Line 45

    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

    i am getting the error as above, please help me anyone

  • i am trying like this with the code u have shared with me but it is getting some errors

    create proc sp_DestAsiaHotelList_Temp

    as

    begin

    DECLARE @XML XML = '<HOTEL>

    <SEQNO>1</SEQNO>

    <GRADE>5</GRADE>

    <PRODUCT>H</PRODUCT>

    <CITYAREA>CKOWM</CITYAREA>

    <CITY>HKG</CITY>

    <CITYAREANAME>Mong Kok </CITYAREANAME>

    <SERVICE>H5LPL</SERVICE>

    <REMARK>Only for Europe, Middle East, US and South American Market</REMARK>

    <SUBSERVICE>---</SUBSERVICE>

    <T_O>DAHKG</T_O>

    <HOTELID>100663300</HOTELID>

    <HOTELNAME>LANGHAM PLACE MONGKOK</HOTELNAME>

    <BYPMC />

    <SUPP_TYPE>-</SUPP_TYPE>

    <PROVIDER />

    <LEADING_FLAG />

    <CHILDRENAGE>

    <FROM_DATE>20080101</FROM_DATE>

    <TO_DATE>20501231</TO_DATE>

    <CHILDAGE>12</CHILDAGE>

    <MINCHILDAGE>2</MINCHILDAGE>

    </CHILDRENAGE>

    <ADDRESS>

    <ADDR_LINE_1>555 Shanghai Street</ADDR_LINE_1>

    <ADDR_LINE_2>Mongkok, Kowloon</ADDR_LINE_2>

    <ADDR_LINE_3 />

    <ADDR_CITY>HONG KONG</ADDR_CITY>

    <ADDR_REGION />

    <ADDR_COUNTRY>HK</ADDR_COUNTRY>

    </ADDRESS>

    </HOTEL>';

    exec sp_xml_preparedocument @XML

    --insert into DestAsiaHotelList_Temp(HotelID, HotelName, StarRating, CityLocation, ResortCode, ResortName, HotelCode, Remark, Subservice, T_O, Provider, LeadingFlag, AddressLineOne, AddressLineTwo, AddressLineThree, City, Region, Country, DateCreated)

    SELECT YourXML.xmlData.value('(GRADE)1','varchar(50)') AS StarRating,

    YourXML.xmlData.value('(CITYAREA)1','VARCHAR(100)') AS CityLocation,

    YourXML.xmlData.value('(CITY)1','VARCHAR(50)') AS ResortCode,

    YourXML.xmlData.value('(CITYAREANAME)1','VARCHAR(150)') AS ResortName,

    YourXML.xmlData.value('(SERVICE)1','VARCHAR(50)') AS HotelCode,

    YourXML.xmlData.value('(REMARK)1','VARCHAR(MAX)') AS Remark,

    YourXML.xmlData.value('(SUBSERVICE)1','VARCHAR(50)') AS SubService,

    YourXML.xmlData.value('(T_O)1','VARCHAR(100)') AS T_O,

    YourXML.xmlData.value('(HOTELID)1','VARCHAR(50)') AS HotelId,

    YourXML.xmlData.value('(HOTELNAME)1','VARCHAR(200)') AS HotelName,

    YourXML.xmlData.value('(PROVIDER)1','VARCHAR(100)') AS Provider,

    YourXML.xmlData.value('(LEADING_FLAG)1','VARCHAR(50)') AS LeadingFlag,

    addressXml.xmlData.value('(ADDR_LINE_1)1','VARCHAR(150)') AS AddressLineOne,

    addressXml.xmlData.value('(ADDR_LINE_2)1','VARCHAR(150)') AS AddressLineTwo,

    addressXml.xmlData.value('(ADDR_LINE_3)1','VARCHAR(150)') AS AddressLineThree,

    addressXml.xmlData.value('(ADDR_CITY)1','VARCHAR(100)') AS City,

    addressXml.xmlData.value('(ADDR_REGION)1','VARCHAR(100)') AS Region,

    addressXml.xmlData.value('(ADDR_COUNTRY)1','VARCHAR(100)') AS Country

    FROM @XML.nodes('/HOTEL') AS YourXML ( xmlData )

    OUTER APPLY @XML.nodes(N'/HOTEL/ADDRESS') AS addressXml ( xmlData );

    with

    (

    HotelID varchar(50) '@HOTELID',

    HotelName varchar(200) 'HotelName',

    StarRating varchar(50) 'GRADE',

    CityLocation varchar(100) 'CITYAREA',

    ResortCode varchar(50) 'CITY',

    ResortName varchar(150) 'CITYAREANAME',

    HotelCode varchar(50) 'SERVICE',

    Remark varchar(MAX) 'REMARK',

    Subservice varchar(50) 'SUBSERVICE',

    T_O varchar(100) 'T_O',

    Provider varchar(100) 'PROVIDER',

    LeadingFlag varchar(50) 'LEADING_FLAG',

    AddressLineOne varchar(200) 'ADDR_LINE_1',

    AddressLineTwo varchar(150) 'ADDR_LINE_2',

    AddressLineThree varchar(150) 'ADDR_LINE_3',

    City varchar(100) 'ADDR_CITY',

    Region varchar(100) 'ADDR_REGION',

    Country varchar(100) 'ADDR_COUNTRY',

    )

    --DestAsiaHotelChildrenAges_Temp

    --insert into DestAsiaHotelChildrenAges_Temp (HotelCode, FromDate, ToDate, MinChildAge, MaxChildAge, DateCreated)

    select childrenAgeXml.xmlData.value('(FROM_DATE)1','DATE') AS FromDate, -- Use DATETIME or DATE please!

    childrenAgeXml.xmlData.value('(TO_DATE)1','DATE') AS ToDate, -- Use DATETIME or DATE please!

    childrenAgeXml.xmlData.value('(CHILDAGE)1','VARCHAR(5)') AS MaxChildAge,

    childrenAgeXml.xmlData.value('(MINCHILDAGE)1','VARCHAR(5)') AS MinChildAge

    from @XML.nodes('/HOTEL/CHILDRENAGE') AS YourXML ( xmlData )

    OUTER APPLY @XML.nodes(N'/HOTEL/CHILDRENAGE') AS childrenAgeXml ( xmlData );

    with(

    HotelCode varchar(50) 'SERVICE',

    FromDate date 'FROM_DATE',

    ToDate date 'TO_DATE',

    MinChildAge varchar(5) 'MINCHILDAGE',

    MaxChildAge varchar(5) 'CHILDAGE',

    )

    EXEC sp_xml_removedocument @xml

    end

  • it is showing errors like this

    Msg 102, Level 15, State 1, Procedure sp_DestAsiaHotelList_Temp, Line 63

    Incorrect syntax near '('.

    Msg 319, Level 15, State 1, Procedure sp_DestAsiaHotelList_Temp, Line 97

    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

  • create proc sp_DestAsiaHotelList_Temp

    as

    begin

    declare @xml xml

    set @xml= '<HOTEL>

    <SEQNO>1</SEQNO>

    <GRADE>5</GRADE> --StarRating

    <PRODUCT>H</PRODUCT>

    <CITYAREA>CKOWM</CITYAREA> --CityLocation

    <CITY>HKG</CITY> --ResortCode

    <CITYAREANAME>Mong Kok</CITYAREANAME> --ResortName

    <SERVICE>H5LPL</SERVICE> --HotelCode

    <REMARK>Only for Europe, Middle East, US and South American Market</REMARK> --Remark

    <SUBSERVICE>---</SUBSERVICE> --SubService

    <T_O>DAHKG</T_O> --T_O

    <HOTELID>100663300</HOTELID> -- HotelId

    <HOTELNAME>LANGHAM PLACE MONGKOK</HOTELNAME> -- HotelName

    <BYPMC></BYPMC>

    <SUPP_TYPE>-</SUPP_TYPE>

    <PROVIDER></PROVIDER> --Provider

    <LEADING_FLAG></LEADING_FLAG> --LeadingFlag

    <CHILDRENAGE> --Children age part will go under HotelChildrenAges

    <FROM_DATE>20080101</FROM_DATE> --FromDate

    <TO_DATE>20501231</TO_DATE> --ToDate

    <CHILDAGE>12</CHILDAGE> --MaxChildAge

    <MINCHILDAGE>2</MINCHILDAGE> --MinChildAge

    </CHILDRENAGE>

    <ADDRESS>

    <ADDR_LINE_1>555 Shanghai Street</ADDR_LINE_1> --AddressLineOne

    <ADDR_LINE_2>Mongkok, Kowloon</ADDR_LINE_2> --AddressLineTwo

    <ADDR_LINE_3></ADDR_LINE_3> --AddressLineThree

    <ADDR_CITY>HONG KONG</ADDR_CITY> --City

    <ADDR_REGION></ADDR_REGION> --Region

    <ADDR_COUNTRY>HK</ADDR_COUNTRY> --Country

    </ADDRESS>

    </HOTEL>'

    exec sp_xml_preparedocument @idoc OUTPUT, @XML

    insert into DestAsiaHotelList_Temp(HotelID, HotelName, StarRating, CityLocation, ResortCode, ResortName, HotelCode, Remark, Subservice, T_O, Provider, LeadingFlag, AddressLineOne, AddressLineTwo, AddressLineThree, City, Region, Country)

    select

    T.C.value('@HotelID','varchar(50)') as HotelID,

    T.C.value('@HotelName','varchar(200)') as HotelName,

    T.C.value('@GRADE','varchar(50)') as StarRating,

    T.C.value('@CITYAREA','varchar(100)')as CityLocation,

    T.C.value('@CITY','varchar(50)')as ResortCode,

    T.C.value('@CITYAREANAME','varchar(150)')as ResortName,

    T.C.value('@SERVICE','varchar(50)')as HotelCode,

    T.C.value('@Remark','varchar(max)')as Remark,

    T.C.value('@Subservice','varchar(50)')as Subservice,

    T.C.value('@T_O ','varchar(100)')as T_O ,

    T.C.value('@Provider','varchar(100)')as Provider,

    T.C.value('@LEADING_FLAG','varchar(50)')as LeadingFlag,

    T.C.value('@ADDR_LINE_1','varchar(200)')as AddressLineOne,

    T.C.value('@ADDR_LINE_2','varchar(150)')as AddressLineTwo,

    T.C.value('@ADDR_LINE_3','varchar(150)')as AddressLineThree,

    T.C.value('@ADDR_CITY','varchar(100)')as City,

    T.C.value('@ADDR_REGION','varchar(100)')as Region,

    T.C.value('@ADDR_COUNTRY','varchar(100)')as Country

    FROM OPENXML(@xml, '/Hotel/Hotel')

    with

    (

    HotelID varchar(50) 'HOTELID',

    HotelName varchar(200) 'HotelName',

    StarRating varchar(50) 'GRADE',

    CityLocation varchar(100) 'CITYAREA',

    ResortCode varchar(50) 'CITY',

    ResortName varchar(150) 'CITYAREANAME',

    HotelCode varchar(50) 'SERVICE',

    Remark varchar(MAX) 'REMARK',

    Subservice varchar(50) 'SUBSERVICE',

    T_O varchar(100) 'T_O',

    Provider varchar(100) 'PROVIDER',

    LeadingFlag varchar(50) 'LEADING_FLAG',

    AddressLineOne varchar(200) 'ADDR_LINE_1',

    AddressLineTwo varchar(150) 'ADDR_LINE_2',

    AddressLineThree varchar(150) 'ADDR_LINE_3',

    City varchar(100) 'ADDR_CITY',

    Region varchar(100) 'ADDR_REGION',

    Country varchar(100) 'ADDR_COUNTRY'

    )

    --DestAsiaHotelChildrenAges_Temp

    insert into DestAsiaHotelChildrenAges_Temp (@HotelCode, @FromDate, @ToDate, @MinChildAge, @MaxChildAge)

    select

    T.C.value('@SERVICE','varchar(50)') as HotelCode,

    T.C.value('@FROM_DATE','date') as FromDate,

    T.C.value('@TO_DATE','date') as ToDate,

    T.C.value('@MINCHILDAGE','varchar(5)') as MinChildAge,

    T.C.value('@CHILDAGE','varchar(5)') as MaxChildAge

    from openxml(@xml, '/Hotel/Hotel')

    with

    (

    HotelCode varchar(50) 'SERVICE',

    FromDate date 'FROM_DATE',

    ToDate date 'TO_DATE',

    MinChildAge varchar(5) 'MINCHILDAGE',

    MaxChildAge varchar(5) 'CHILDAGE'

    )

    EXEC sp_xml_removedocument @idoc

    end

    error is

    Msg 137, Level 15, State 2, Procedure sp_DestAsiaHotelList_Temp, Line 69

    Must declare the scalar variable "@idoc".

    Msg 137, Level 15, State 2, Procedure sp_DestAsiaHotelList_Temp, Line 121

    Must declare the scalar variable "@HotelCode".

    Msg 137, Level 15, State 2, Procedure sp_DestAsiaHotelList_Temp, Line 141

    Must declare the scalar variable "@idoc".

  • I had tried al the replies what i had sent to u,please help me

  • edara1234 (7/16/2014)


    I had tried al the replies what i had sent to u,please help me

    "All the replies" suggests that there were more than one. There wasn't. I replied, then you spammed the forum with a load of stuff.

    My original post had this in it: -

    DECLARE @XML XML = '<HOTEL>

    <SEQNO>1</SEQNO>

    <GRADE>5</GRADE>

    <PRODUCT>H</PRODUCT>

    <CITYAREA>CKOWM</CITYAREA>

    <CITY>HKG</CITY>

    <CITYAREANAME>Mong Kok </CITYAREANAME>

    <SERVICE>H5LPL</SERVICE>

    <REMARK>Only for Europe, Middle East, US and South American Market</REMARK>

    <SUBSERVICE>---</SUBSERVICE>

    <T_O>DAHKG</T_O>

    <HOTELID>100663300</HOTELID>

    <HOTELNAME>LANGHAM PLACE MONGKOK</HOTELNAME>

    <BYPMC />

    <SUPP_TYPE>-</SUPP_TYPE>

    <PROVIDER />

    <LEADING_FLAG />

    <CHILDRENAGE>

    <FROM_DATE>20080101</FROM_DATE>

    <TO_DATE>20501231</TO_DATE>

    <CHILDAGE>12</CHILDAGE>

    <MINCHILDAGE>2</MINCHILDAGE>

    </CHILDRENAGE>

    <ADDRESS>

    <ADDR_LINE_1>555 Shanghai Street</ADDR_LINE_1>

    <ADDR_LINE_2>Mongkok, Kowloon</ADDR_LINE_2>

    <ADDR_LINE_3 />

    <ADDR_CITY>HONG KONG</ADDR_CITY>

    <ADDR_REGION />

    <ADDR_COUNTRY>HK</ADDR_COUNTRY>

    </ADDRESS>

    </HOTEL>';

    SELECT YourXML.xmlData.value(N'(GRADE)[1]', N'INT') AS [StarRating],

    YourXML.xmlData.value(N'(CITYAREA)[1]', N'NVARCHAR(50)') AS [CityLocation],

    YourXML.xmlData.value(N'(CITY)[1]', N'CHAR(3)') AS [ResortCode],

    YourXML.xmlData.value(N'(CITYAREANAME)[1]', N'NVARCHAR(50)') AS [ResortName],

    YourXML.xmlData.value(N'(SERVICE)[1]', N'CHAR(5)') AS [HotelCode],

    YourXML.xmlData.value(N'(REMARK)[1]', N'NVARCHAR(MAX)') AS [Remark],

    YourXML.xmlData.value(N'(SUBSERVICE)[1]', N'NVARCHAR(MAX)') AS [SubService],

    YourXML.xmlData.value(N'(T_O)[1]', N'NVARCHAR(50)') AS [T_O],

    YourXML.xmlData.value(N'(HOTELID)[1]', N'BIGINT') AS [HotelId],

    YourXML.xmlData.value(N'(HOTELNAME)[1]', N'NVARCHAR(MAX)') AS [HotelName],

    YourXML.xmlData.value(N'(PROVIDER)[1]', N'NVARCHAR(MAX)') AS [Provider],

    YourXML.xmlData.value(N'(LEADING_FLAG)[1]', N'NVARCHAR(MAX)') AS [LeadingFlag],

    childrenAgeXml.xmlData.value(N'(FROM_DATE)[1]', N'DATE') AS [FromDate], -- Use DATETIME or DATE please!

    childrenAgeXml.xmlData.value(N'(TO_DATE)[1]', N'DATE') AS [ToDate], -- Use DATETIME or DATE please!

    childrenAgeXml.xmlData.value(N'(CHILDAGE)[1]', N'INT') AS [MaxChildAge],

    childrenAgeXml.xmlData.value(N'(MINCHILDAGE)[1]', N'INT') AS [MinChildAge],

    addressXml.xmlData.value(N'(ADDR_LINE_1)[1]', N'NVARCHAR(MAX)') AS [AddressLineOne],

    addressXml.xmlData.value(N'(ADDR_LINE_2)[1]', N'NVARCHAR(MAX)') AS [AddressLineTwo],

    addressXml.xmlData.value(N'(ADDR_LINE_3)[1]', N'NVARCHAR(MAX)') AS [AddressLineThree],

    addressXml.xmlData.value(N'(ADDR_CITY)[1]', N'NVARCHAR(MAX)') AS [City],

    addressXml.xmlData.value(N'(ADDR_REGION)[1]', N'NVARCHAR(MAX)') AS [Region],

    addressXml.xmlData.value(N'(ADDR_COUNTRY)[1]', N'NVARCHAR(MAX)') AS [Country]

    FROM @XML.nodes(N'/HOTEL') AS YourXML ( xmlData )

    OUTER APPLY @XML.nodes(N'/HOTEL/CHILDRENAGE') AS childrenAgeXml ( xmlData )

    OUTER APPLY @XML.nodes(N'/HOTEL/ADDRESS') AS addressXml ( xmlData );

    Which returns: -

    StarRating CityLocation ResortCode ResortName HotelCode Remark SubService T_O HotelId HotelName Provider LeadingFlag FromDate ToDate MaxChildAge MinChildAge AddressLineOne AddressLineTwo AddressLineThree City Region Country

    ----------- -------------------------------------------------- ---------- -------------------------------------------------- --------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------- ---------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    5 CKOWM HKG Mong Kok H5LPL Only for Europe, Middle East, US and South American Market --- DAHKG 100663300 LANGHAM PLACE MONGKOK 2008-01-01 2050-12-31 12 2 555 Shanghai Street Mongkok, Kowloon HONG KONG HK

    (1 row(s) affected)

    This is running on @@VERSION -

    Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)

    Jun 28 2012 08:36:30

    Copyright (c) Microsoft Corporation

    Developer Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)

    With compatibility mode set to 100.

    My assumption is that either you have your database set to a lower compatibility mode, you're not using SQL Server 2008R2 or you copied something incorrectly when trying to modify my code for use in your system.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • what does the OUTER APPLY means and for what sake it is used in the query u have shared with me

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply