I get a file from a particular vendor every week. Once in a while their process changes, and I wind up getting extra fields, sometimes in the middle of the row. Fortunately, I always get a proper header, and they don't appear to remove the existing fields. Unfortunately, nobody ever knows WHY or how it changed. After changing it several times, I decided to get clever.
So, what I'd like to do: query SQL Server, get the columns from the target view/table, then SELECT just the fields that exist in both, into the table.
My current code (below) doesn't do that - it just does an import-csv, "select the fields I know are good", then insert into a table. Works, yes, but slow as crap. And dumb, since I have to hardcode the fields I know are good. I'd like a more extensible process.
Additionally, I'd like to stream it in - my existing process crawls (15 minutes for a 50mb file), probably because it has to load the entire file, run it through the select & convert, then do the insert. But it works.
I played with High-Performance Techniques for Importing CSV to SQL Server using PowerShell, but kept running into "I don't know powershell well enough to fix" issues.
Here's what I have so far. Ultra-simplified, though I could make it simpler by building a view on top of it that does the conversion from "That First Field to FirstField". Thanks in advance!
In SQL Server:
CREATE DATABASE my_database
go
USE my_database
go
CREATE TABLE my_target_table
(FirstField VARCHAR(100), StoreId INT,State VARCHAR(3))
Now the powershell script:
#import all the stuff we need. Using These are Chad Miller's datatable scripts.
#http://gallery.technet.microsoft.com/ScriptCenter/7985b7ef-ed89-4dfd-b02a-433cc4e30894/
#http://gallery.technet.microsoft.com/ScriptCenter/2fdeaf8d-b164-411c-9483-99413d6053ae/
. C:\scripts\out-datatable.ps1;
. C:\scripts\invoke-sqlcmd2.ps1;
. C:\scripts\write-datatable.ps1;
if (test-path Variable:\my_import) {
remove-variable my_import
}
$ImportFilePath = "c:\test\my_import_file.txt"
#save the below 3 lines as "c:\test\my_import_file.txt"
#That First Field|Store Id|UselessField|State
#1|15|AAA|MI
#12|4|AB|RI
$my_import = import-csv $ImportFilePath -Delimiter "|"| `
select @{Label="FirstField";Expression={$_."That First Field"}}, `
@{Label="StoreId";Expression={$_."Store ID"}}, `
@{Label="State";Expression={$_."State"}} `
|out-datatable
Write-DataTable -ServerInstance my_server -Database my_database -TableName my_target_table -Data $my_import