1

Hi I'm trying to print a sql table to a file in tab delimited format.

Right now I am using the powershell line

Invoke-SQLCmd -ServerInstance $Server -Database $Database -query "select distinct * from WHIProducts" | Export-Csv -path "$inventorypath\inventory_$date\$filename" -NoTypeInformation -Delimiter "`t"

The problem I am running into is a system.outofmemoryexception.

Does anyone know of a way to do this directly through a sql query so that way I can just run the query through powershell? Or is there a more efficient way using powershell or another scripting language? This is has to be done automatically so going to SSMS and print the results to text file isn't enough.

I am using SQL Server express.

Patrick Mahoney
  • 95
  • 1
  • 3
  • 9
  • 1
    Read a row at a time with [SQLDataReader](https://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.110).aspx) and the [`Read()` method](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read(v=vs.110).aspx), and use `Export-CSV -append`? Here' [an example](http://stackoverflow.com/questions/33572538/how-do-i-execute-a-select-query-against-a-sqlserver-database-and-iterate-results/33573606#33573606) – BenH Mar 02 '17 at 16:14

0 Answers0