Using PowerShell 4.0 and SQL-Server, I want to merge records from one database to another. I use export-csv and import-csv. I export from one database to a csv file, and import from the csv file to a temporary table in another database, for subsequent MERGE.
TableA is
([ID] int not null,
[Name] varchar(25) not null,
[StartDate] datetime null,
[Department] varchar(25) not null)
Values are ID=1, Name=Joe, StartDate=NULL, Department=Sales
exportTable.ps1 (Ignoring database config)
Invoke Sqlcmd ("SELECT FROM TableA WHERE ID=1") | Export-Csv -path a.csv -NoTypeInformation -Append
This results in a.csv
"ID","Name","StartDate","Department"
"1","Joe","","Sales"
import Table.ps1
CreateTable TableATemporary
([ID] int not null,
[Name] varchar(25) not null,
[StartDate] datetime null)
Import-Csv a.csv | ForEach-Object {
$allValues = "'"+($_.Psobject.Properties.Value -join "','") + "'"
Invoke Sqlcmd ("INSERT INTO TableATemporary VALUES $allValues")
This gives a table of Values are ID=1, Name=Joe, StartDate=1900-01-01 00:00:00:000, Department=Sales
Rather than a null entry, the datetime field is a default value because the field in the csv file is ""
Is there any way for the Export-Csv cmdlet to write nothing to the csv file for the empty fields in the database, instead of "" ?
O'Brienor Department likeMen's Wear.