This question already has an answer here:
这个问题在这里已有答案:
- How to create and populate a table in a single step as part of a CSV import operation? 2 answers
- 如何在CSV导入操作中一步创建和填充表格? 2个答案
I have several CSV files, which I want to import into an SQL Server database. I know if this is possible with BULK insert, but I want a solution, so that also the import table is automatically created before on basis the first row of the CSV files,are the column names.
我有几个CSV文件,我想将其导入SQL Server数据库。我知道如果BULK插入可以实现这一点,但是我想要一个解决方案,因此在基于CSV文件的第一行之前自动创建导入表的是列名。
2 个解决方案
#1
26
SQL Server Management Studio provides an Import/Export wizard tool which have an option to automatically create tables.
SQL Server Management Studio提供了一个导入/导出向导工具,该工具可以选择自动创建表。
You can access it by right clicking on the Database in Object Explorer and selecting Tasks->Import Data...
您可以通过右键单击对象资源管理器中的数据库并选择任务 - >导入数据来访问它...
From there wizard should be self-explanatory and easy to navigate. You choose your CSV as source, desired destination, configure columns and run the package.
从那里向导应该是不言自明的,易于导航。您选择CSV作为源,所需目标,配置列并运行包。
If you need detailed guidance, there are plenty of guides online, here is a nice one: http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
如果你需要详细的指导,网上有很多指南,这里有一个很好的指南:http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
#2
8
You can create a temp table variable and insert the data into it, then insert the data into your actual table by selecting it from the temp table.
您可以创建临时表变量并将数据插入其中,然后通过从临时表中选择数据将数据插入到实际表中。
declare @TableVar table
(
firstCol varchar(50) NOT NULL,
secondCol varchar(50) NOT NULL
)
BULK INSERT @TableVar FROM 'PathToCSVFile' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
GO
INSERT INTO dbo.ExistingTable
(
firstCol,
secondCol
)
SELECT firstCol,
secondCol
FROM @TableVar
GO
#1
26
SQL Server Management Studio provides an Import/Export wizard tool which have an option to automatically create tables.
SQL Server Management Studio提供了一个导入/导出向导工具,该工具可以选择自动创建表。
You can access it by right clicking on the Database in Object Explorer and selecting Tasks->Import Data...
您可以通过右键单击对象资源管理器中的数据库并选择任务 - >导入数据来访问它...
From there wizard should be self-explanatory and easy to navigate. You choose your CSV as source, desired destination, configure columns and run the package.
从那里向导应该是不言自明的,易于导航。您选择CSV作为源,所需目标,配置列并运行包。
If you need detailed guidance, there are plenty of guides online, here is a nice one: http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
如果你需要详细的指导,网上有很多指南,这里有一个很好的指南:http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
#2
8
You can create a temp table variable and insert the data into it, then insert the data into your actual table by selecting it from the temp table.
您可以创建临时表变量并将数据插入其中,然后通过从临时表中选择数据将数据插入到实际表中。
declare @TableVar table
(
firstCol varchar(50) NOT NULL,
secondCol varchar(50) NOT NULL
)
BULK INSERT @TableVar FROM 'PathToCSVFile' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
GO
INSERT INTO dbo.ExistingTable
(
firstCol,
secondCol
)
SELECT firstCol,
secondCol
FROM @TableVar
GO