Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

PowerShell to Dynamically download SQL table data to csv

$
0
0

Re: PowerShell to Dynamically download SQL table data to csv

I worked on my first powershell today. My goal is to dynamically download SQL Table data to csv files. There is a ifs_ExportTables that is a controller table. If you want to download a table, then enter a row into ifs_ExportTables. I do not have all the bells and whistles yet, but would appreciate any feed back. (1) is there an easier way to reference the column data (2) is the loadwithpartialname the best way to load (3) will the loadwithpartialname work with sql 2005 - 2014 (4) is there a specific version of PS that is required (I am working with 4.0) .

Utilmately this will go out to several clients that have SQL 2005 - 2014, who knows what OS, and I can require a certain version of

CREATE DATABASE aaatest2
CREATE TABLE [ifs_ExportTables](
	[RecID] INT IDENTITY(1,1),
	[ExportGroup] INT NULL, 
	[TableName] VARCHAR(128) NULL,
	[ExportPath] VARCHAR(4000),
	[Delimiter] VARCHAR (128) NULL,
	[ColumnHeaders] INT NULL,
	[OutFileName] VARCHAR (128) NULL,
	[OutFileAddDate] INT NULL,
	[Enabled] INT NULL)
CREATE TABLE [aatable1](
	[RecID] INT IDENTITY(1,1),
	[field1] INT NULL, 
	[field2] VARCHAR(128) NULL)
CREATE TABLE [aatable2](
	[RecID] INT IDENTITY(1,1),
	[field21] INT NULL, 
	[field22] VARCHAR(128) NULL)
INSERT INTO [ifs_ExportTables]([ExportGroup],[TableName],[Delimiter],[ColumnHeaders],[OutFileName],[OutFileAddDate],[Enabled])
	VALUES (1, 'aatable1', '|', 0,'',1,1)
INSERT INTO [ifs_ExportTables]([ExportGroup],[TableName],[Delimiter],[ColumnHeaders],[OutFileName],[OutFileAddDate],[Enabled])
	VALUES (1, 'aatable2', '|', 0,'',1,1)	
INSERT INTO [aatable1]([field1],[field2])
VALUES ('11', 'aaa')
INSERT INTO [aatable1]([field1],[field2])
VALUES ('111', 'bbb')
INSERT INTO [aatable1]([field1],[field2])
VALUES ('111', 'ccc')
INSERT INTO [aatable2]([field21],[field22])
VALUES ('22', 'ddd')
INSERT INTO [aatable2]([field21],[field22])
VALUES ('222', 'eee')
INSERT INTO [aatable2]([field21],[field22])
VALUES ('2222', 'fff')

This is the PS code. Set your server and output path.

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$sqlsvr = 'fidev360bi02\fidev360bi02'
$dir = 'c:\temp\pssmo\'
$database = 'aaatest'
$diagnostics = 1
$sqlserver = New-Object Microsoft.SqlServer.Management.Smo.Server($sqlsvr)
$db = $sqlserver.databases[$database]
$Tables = $db.ExecuteWithResults("Select * FROM ifs_exporttables")
foreach ($DataRow in $Table[0].Tables[0].Rows)
    {
    $TableName = $DataRow[2].tostring()    # ifs_ExportTables.TableName
    $Delimiter = $DataRow[4].tostring()    # ifs_ExportTables.Delimiter
    $Header = $DataRow[5].tostring()       # ifs_ExportTables.ColumnHeaders 
    if ($diagnostics = 1) 
        {write-host -ForegroundColor Green "Creating Table $TableName to path $dir$TableName.csv" }
    $sqlserverR = New-Object Microsoft.SqlServer.Management.Smo.Server($sqlsvr)
    $dbR = $sqlserverR.databases[$database]
    $TablesR = $dbR.ExecuteWithResults("Select * FROM $TableName")
    $result = $TablesR[0].tables.item(0)
    if ($Header -eq '1')
    	{$result |export-csv -Delimiter $Delimiter –notype -path $dir$TableName.csv  -Encoding ASCII}
    else
	    {$result | ConvertTo-Csv -NoTypeInformation -Delimiter $Delimiter | Select-Object -Skip 1 | Out-File -FilePath $dir$TableName.csv  -Encoding ASCII}
    }


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>