forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL_Server_table_to_csv.ps1
57 lines (50 loc) · 1.52 KB
/
SQL_Server_table_to_csv.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
.SYNOPSIS
Export SQL Server table to csv file
.DESCRIPTION
This script export SQL Server table to csv file
.PARAMETER sqlCmd.CommandText
SQL query for export data
.EXAMPLE
C:\PS>
<Description of example>
.NOTES
Author: Bill Graziano
Original Link: http://www.sqlteam.com/article/fast-csv-import-in-powershell-to-sql-server
Created Date: 2014-03-18
#>
$ConnectionString = "localhsot"
$streamWriter = New-Object System.IO.StreamWriter ".\SimpleCsvOut3.txt"
$sqlConn = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.Connection = $sqlConn
$sqlCmd.CommandText = "SELECT * FROM sys.tables"
$sqlConn.Open();
$reader = $sqlCmd.ExecuteReader();
# Initialze the arry the hold the values
$array = @()
for ( $i = 0 ; $i -lt $reader.FieldCount; $i++ )
{ $array += @($i) }
# Write Header
$streamWriter.Write($reader.GetName(0))
for ( $i = 1; $i -lt $reader.FieldCount; $i ++)
{ $streamWriter.Write($("," + $reader.GetName($i))) }
$streamWriter.WriteLine("") # Close the header line
while ($reader.Read())
{
# get the values;
$fieldCount = $reader.GetValues($array);
# add quotes if the values have a comma
for ($i = 0; $i -lt $array.Length; $i++)
{
if ($array[$i].ToString().Contains(","))
{
$array[$i] = '"' + $array[$i].ToString() + '"';
}
}
$newRow = [string]::Join(",", $array);
$streamWriter.WriteLine($newRow)
}
$reader.Close();
$sqlConn.Close();
$streamWriter.Close();