Relative Content

Tag Archive for sql-serverpowershellimportexcel

Materializing SQL Query Results in Excel Using a Modded Version of ImportExcel’s Export-Excel Function Not Working Correctly When Invoked From SQL Job

I wrote a script that runs queries on a SQL Server DB (2019) and writes the results to Excel files using the Export-Excel function of the PS module ImportExcel. It works great for mid-sized datasets, but it is very slow for larger ones. Also, for sufficiently large datasets, memory availability becomes a problem. I suspect both of these issues can be attributed to having to first load data into a System.Data.DataTable, so, in an attempt to circumvent this, I modified Export-Excel to accept a System.Data.IDataReader. This was a huge performance improvement, and it works perfectly when I run the script on my machine. However, when I attempt to invoke the script from within a SQL job, it just fills the a file’s first column with the value “System.Data.Common.DataRecordInternal” for every row in the dataset. Maybe this has something to do with differences in assembly versions on my machine and the server. Note that both System.Data.DataTable and System.Data.IDataReader are in the same assembly. The SQL job has a PowerShell step that looks like the following: