简体   繁体   中英

Schedule import csv to SQL Server 2014 Express edition

Is there a way to have a .csv or .txt imported into SQL Server automatically?

I know how to do it manually using Date import & Export tool. But is it possible to do it automatically?

You can use Windows Task Scheduler to run bcp commands automatically. The command that will be run automatically will import your csv file using bulk copy program (bcp) utility. It can import or export data from/to files. For example to import a csv file to a table in SQL Server, you can use command like this:

bcp.exe dbo.MyTable in "C:\Some Folder\Data.csv" -s MYPC\SQLEXPRESS -d MyDatabase -U LoginName -P StrongP@ssw0rd

Where:

  • dbo.MyTable is the schema and table name, where data should be imported.
  • in tells the direction (put data in the database, or get data out of it).
  • "C:\\Some Folder\\Data.csv" is the name and path to the file holding the data to be imported.
  • MYPC\\SQLEXPRESS is the computer and SQL Server instance name.
  • MyDatabase is the name of the database, where dbo.MyTable is.
  • LoginName and StronP@ssw0rd are the credentials to be used to connect to the server (or -E instead of -U and -P to connect using Windows Authentication).

Then create a new scheduled task ( Start -> Task Scheduler -> Create Basic Task ) and set a schedule according your requirements (eg daily at 3:00 AM) to run the command above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM