Schedule import csv to SQL Server 2014 Express edition

前端 未结 1 1632
不知归路
不知归路 2021-01-27 18:19

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.

相关标签:
1条回答
  • 2021-01-27 18:43

    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 (e.g. daily at 3:00 AM) to run the command above.

    0 讨论(0)
提交回复
热议问题