You want to import a xml file into SQL Server, but have one xml-file for developing and testing that is easy to read, and one for production that is using really short node names to minimize the size of the file.
The development file could look something like this:
<BookingName>My own booking</BookingName>
And the production file could look something like this:
<bn>My own booking</bn>
And you want to be able to use the same SQL-script to import the file, no matter wether is the production or development file. Try this then:
Declare @xmlfile xml
Declare @docHandle int
Declare @xmlPath varchar(50)
-- Step 1 - Set the base path based on something, filename, or have a startnode in the xml-file
if @fileName = 'C:\prodXmlFile.xml'
@xmlPath = '/r/bo'
@xmlPath = '/Root/Booking'
@xmlfile=InvoicesXML FROM (
SELECT * FROM OPENROWSET(BULK
SINGLE_BLOB) AS Invoices )
EXEC sp_xml_preparedocument @docHandle OUTPUT,
SELECT * INTO Bookings
bookingId int 'bookingId | boid',
bookingName int 'bookingName | bn'
-- This is the trick, an xpath expression the takes bookingId if it there, and boid if it is there
-- end try begin catch end catch
EXEC sp_xml_removedocument @docHandle