Can you import .CSV data into SQL Server using commands from Powershell 3 or 4?

*爱你&永不变心* 提交于 2019-12-24 19:37:01

问题


Is there a Powershell script for PowerShell version 3 or version 4 that allows you to import values from a .CSV file into a SQL table?

  1. I'm trying to figure out whether I can script a PowerShell job to import data, using enterprise Servers running older versions of PowerShell (It would be preferred if this can be done without importing any additional modules)

  2. (Nice to have, but not necessary) Also interested to know if there's a way to report the success or failure of that job, to a log file?

This is the script I've tested in PowerShell 5, just can't use this on my older servers:

$database = 'DATABASE';
$server = 'SERVER';
$table = 'TABLE';
$MyData = import-csv \\TESTPATH\licenses_v2.csv -delimiter '|' | 
select-object FirstName,LastName,Department,Title;
Write-DbaDataTable -sqlinstance $server -database $database -table $table -inputobject $MyData -KeepNulls;  

回答1:


The answer is yes, I just need to install the DBATools module.



来源:https://stackoverflow.com/questions/49786434/can-you-import-csv-data-into-sql-server-using-commands-from-powershell-3-or-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!