問題描述
我需要每天從在線可用的 csv 中執行數據加載,例如http://www.supplier.com/products.csv一旦我將 csv 轉儲到 sql 表中,我就可以進行處理,然后我需要更新/插入等.問題是我不知道如何自動加載數據.
I need to perform a dataload every day from a csv available online e.g. http://www.supplier.com/products.csv Once I've dumped the csv into a sql table I can do the processing I then need to update / insert etc. The problem is that I don't know how to automate the dataload.
我希望我可以使用計劃在每天 06:00 運行的 SQL 作業/任務,給它一個 uri,然后它可以訪問 csv 中的數據...
I was hoping I could use a SQL job / task, scheduled to run each day at 06:00, give it a uri and that it could then access the data in the csv...
我該怎么做?
推薦答案
您可以安排 SQL 代理作業在本地下載文件并使用 批量插入:
You can schedule a SQL Agent job to download the file locally and use BULK INSERT:
CREATE TABLE StagingCSV
(
col1 VARCHAR(60),
col2 VARCHAR(60),
col3 VARCHAR(60),
col4 VARCHAR(60),
-- ...
)
GO
(錯誤行將被忽略)
BULK
INSERT StagingCSV
FROM 'c:\mycsvfile.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
其他方法:
關于批量導入和批量導出操作
使用 BULK INSERT 或 OPENROWSET 導入批量數據
您可以使用 Powershell 下載文件:
You can use Powershell to download a file:
$clnt = new-object System.Net.WebClient
$url = "http://www.supplier.com/products.csv "
$file = "c:\temp\Mycsv.txt"
$clnt.DownloadFile($url, $file)
這篇關于如何從在線 CSV 插入 SQL Server 數據庫數據?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!