I have an issue with bcp to create xml file.
If I execute the single query
SELECT @header, @inland
FOR XML RAW(''), ROOT('root'), ELEMENTS, TYPE
I receive the correct XML data.
But if i insert the query into exec master..xp_cmdshell
declare @cmd varchar(2000) = 'bcp "SELECT @header,@inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+@num+'.xml" -U xx -P xxxxx -c -C ANSI -t;' ;
exec master..xp_cmdshell @cmd;
The system returns the error
SQLState = 37000, NativeError = 137
Error = [Microsoft][SQL Native Client][SQL Server]Must declare the scalar variable "@header".SQLState = 37000, NativeError = 8180
Error = [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared.
@header
is declared as XML
Where is the error?
Thanks and regards
'bcp ' + @@header + ',' + @@inland + ' FOR XML RAW....
. You might want to add double quote before @@header. – Han