Skip to content

Error when reading/parsing the output as native XML #113

Description

@Niritl-Madeira

Hi Daniel,

I’ve been using your code, and I must say it’s fantastic! It has saved me a ton of work. Thank you for sharing it!

However, I encountered a small issue while extracting a large table using the XML option and calling your procedure via PowerShell. Here's the code I’ve been using:

$connectionString = "Server=$serverName;Database=$databaseName;Integrated Security=True;"
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()

# Create a SQL command
$command = $connection.CreateCommand()
$command.CommandText = "EXEC sp_generate_merge @results_to_text = 0, @include_use_db = 0, @max_rows_per_batch = 100, @delete_if_not_matched = 0, @schema = N'dbo', @table_name = N'XXX';"

# Execute the command and read the XML output
$xmlReader = $command.ExecuteXmlReader()

# Load the XML into an XML document
$xmlDocument = New-Object System.Xml.XmlDocument
$xmlDocument.Load($xmlReader)

# Save the XML to a file
$xmlDocument.Save($outputFilePath)

The issue I ran into is that the only way I was able to get the output saved to a file was by changing this line:

SELECT [processing-instruction(x)]=@output FOR XML PATH(''), TYPE;

to:

SELECT @output AS a FOR XML PATH('');

I’m not entirely sure why this change was necessary, but I thought it might be worth bringing to your attention in case there’s something in the XML handling that could be causing it.

Thanks again for your excellent work on this code—it’s been incredibly helpful!

Best regards,
Nirit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions