問題描述
我需要每天從在線可用的 csv 中執(zhí)行數(shù)據(jù)加載,例如http://www.supplier.com/products.csv一旦我將 csv 轉(zhuǎn)儲到 sql 表中,我就可以進(jìn)行處理,然后我需要更新/插入等.問題是我不知道如何自動加載數(shù)據(jù).
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 運(yùn)行的 SQL 作業(yè)/任務(wù),給它一個 uri,然后它可以訪問 csv 中的數(shù)據(jù)...
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 代理作業(yè)在本地下載文件并使用 批量插入:
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
其他方法:
關(guān)于批量導(dǎo)入和批量導(dǎo)出操作
使用 BULK INSERT 或 OPENROWSET 導(dǎo)入批量數(shù)據(jù)
您可以使用 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)
這篇關(guān)于如何從在線 CSV 插入 SQL Server 數(shù)據(jù)庫數(shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!