I'm trying to generate xml files based on a base table using bcp. Here is the script:
SET @Script = '
bcp " ;WITH XMLNAMESPACES (''urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01'' as po,\
''aaa://xml.aaa.net/K809/k8msgEnvelope'' AS env, \
''aaa://www.w3.org/2001/XMLSchema-instance'' as xsi,\
default ''aaa://xml.aaa.net/K809'') \
SELECT ''aaa://xml.aaa.net/K809 k8Order.xsd'' AS [@xsi:schemaLocation],\
'' AS [header/env:envelope/env:source/@password],\
'' AS [header/env:envelope/env:source/@machine],\
A.SUPPLIER AS [header/env:envelope/env:source/@Endpoint],\
A.SourceLocation AS [header/env:envelope/env:source/@Branch],\
'' AS [header/env:envelope/env:destination/@machine],\
A.SUPPLIER AS [header/env:envelope/env:destination/@Endpoint],\
A.SourceLocation AS [header/env:envelope/env:destination/@Branch],\
''ORDERRESPONSE'' AS [header/env:envelope/env:payload],\
A.FREEATTR3 AS [header/env:envelope/env:cfcompany],\
''ILDLive'' AS [header/env:envelope/env:service],\
(select ''ABC 1234567'' AS [po:OrderReferences/po:CrossReference],cast(''<Extensions xmlns="aaa://xml.aaa.net/k8msg/k8OrderExtensions"><Direct>FALSE</Direct></Extensions>'' as xml).query(''/*''),\
A.SUPPLIER AS [po:Supplier/po:SupplierReferences/po:BuyersCodeForSupplier],\
A.EXPDATEPRD AS [po:Delivery/po:PreferredDate],\
''Test text for Special instructions'' AS [po:Delivery/po:SpecialInstructions],\
''New item'' AS [po:OrderLine/@TypeDescription],\
''New'' AS [po:OrderLine/@TypeCode],\
''Add'' AS [po:OrderLine/@Action],\
A.ITEM AS [po:OrderLine/po:Product/po:BuyersProductCode],\
A.ExternalItemMasterID AS [po:OrderLine/po:Product/po:SuppliersProductCode],\
A.UnitOfMeasureDesc AS [po:OrderLine/po:Quantity/@UOMDescription],\
A.VolumetricValue AS [po:OrderLine/po:Quantity/@UOMCode],\
CAST(SUM(A.QEDIT) AS INT) AS [po:OrderLine/po:Quantity/po:Amount],\
DATEDIFF(DAY,''1989/12/31'',A.EXPDATEPRD) AS [po:OrderLine/po:Delivery/po:PreferredDate]\
for xml path(''po:PurchaseOrder''), type).query(''declare default element namespace "urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01";<PurchaseOrder> { /po:PurchaseOrder/* } </PurchaseOrder>'') as [body] \
FROM [TBCO_Staging}.[dbo].[BaseTemp] AS A \
GROUP BY A.Supplier,A.FREEATTR3,A.SUPPLIER,A.EXPDATEPRD,A.ExternalItemMasterID,A.ITEM,A.VolumetricValue,A.UnitOfMeasureDesc, A.SourceLocation \
FOR XML PATH(''kmsg'')" \
queryout "F:\directory\Exports\XMLFiles\test.xml" -S someserver -T -w -r -t -c -C 65001
'
EXEC MASTER..xp_cmdshell @Script
I'm facing these erorrs:
Error = [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ' AS [header/env:envelope/env:payload],A.FREEATTR3 AS [header/env:envelope/env:cfcompany],'.
Error = [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Missing end comment mark '*/'.
Error = [Microsoft][ODBC Driver 17 for SQL Server]Unable to resolve column level collations
The first error points to a static value I'm passing (''ORDERRESPONSE'' in this case) which I don't understand. It seems correctly encapsulated. The script has a problem with all static values I'm passing though, so changing just the one doesn't help.
The second error is the missing end comment mark. I received assistance from @AlwaysLearning yesterday who gave me pointers to use in the XML script. This piece worked well displaying the results but when trying to use this within bcp fails. Obviously I can't add an end comment as this isn't supposed to be used as a comment. It is as if bcp ignores the encapsulation and sees the start comment in the context outside where it is used.
The third error message I'm unsure about.
I've searched for solutions to all these problems to no avail. I realize it might not be a very pretty script however switching over to Powershell now isn't a solution really as our company doesn't have someone with PS knowledge.
To generate the base table:
CREATE TABLE [dbo].[BaseTemp](
[RowNo] [bigint] NULL,
[Supplier] [varchar](12) NULL,
[SourceLocation] [varchar](8) NULL,
[FREEATTR3] [varchar](70) NULL,
[EXPDATEPRD] [int] NULL,
[Item] [varchar](32) NOT NULL,
[ExternalItemMasterID] [varchar](50) NULL,
[UnitOfMeasureDesc] [varchar](20) NULL,
[VolumetricValue] [varchar](50) NULL,
[QEDIT] [int] NULL,
[EXPDATEPRDAsDate] [int] NULL
)
Populate base table:
INSERT INTO [dbo].[BaseTemp]
([RowNo]
,[Supplier]
,[SourceLocation]
,[FREEATTR3]
,[EXPDATEPRD]
,[Item]
,[ExternalItemMasterID]
,[UnitOfMeasureDesc]
,[VolumetricValue]
,[QEDIT]
,[EXPDATEPRDAsDate])
VALUES
(2
,'050025'
,'1053'
,'01'
,'12120'
,1153105
,'5103'
,'Each'
,'EA'
,1836
,20751)