将excel中的数据导入到Sql Server数据库中的多个表中
我有一个 Excel 工作表,其中的数据类似于下表:
EmployeeName Manager Name
E1 M1
E2 M2
E3 M3
E4 M1
我需要将其导出到一个数据库中,该数据库有两个单独的员工表和经理表。
我导出到的 Sql 表具有如下结构:
Create Table Manager (Manager_Id int Primary Key Identity(1,1),
ManagerName nvarchar(50))
Create Table Employee (EmployeeID int Primary Key Identity(1,1) ,
EmployeeName nvarchar(50),
Manager_Id int)
ManagerId 是员工表中的外部引用。
由于我在 Employee 表之前需要 Manager 表,因此我首先使用 SQL Server 导入和导出向导成功导入了经理数据。
我的下一步是将数据导入到员工表中。这就是问题所在。 现在,我想要 Sql Server 数据库中的表中的经理 ID 和 Excel 工作表中的员工姓名。我虽然在导入和导出向导中的 Sql 语句选项中必须有一种方法来编写可以引用 excel 和 sql 表的查询,但我收到错误“这不是查询”
是否有更好的方法将数据导入多个表?
I have an excel sheet where in the data looks similar to the table below :
EmployeeName Manager Name
E1 M1
E2 M2
E3 M3
E4 M1
I need to export this into a database which has two separate table for employee and manager.
Sql tables which I am exporting to has structure like this:
Create Table Manager (Manager_Id int Primary Key Identity(1,1),
ManagerName nvarchar(50))
Create Table Employee (EmployeeID int Primary Key Identity(1,1) ,
EmployeeName nvarchar(50),
Manager_Id int)
ManagerId is a foreign reference in the employee table.
Since I need Manager Table before the Employee table, I successfully imported the manager data first using SQL Server Import and Export wizard.
My next step was to import data into the employee table.This is where the problem comes in.
Now, I want manager Id from a table sitting in the Sql Server database and the employee names from the excel sheet. I though there must be a way in the Import and Export wizard in the Sql Statement option to write a query which can refer to both excel and the sql table but I get an error "This is not a query"
Is there a better way of importing data to multiple tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将 Employee 数据导入到 Employee 表中,而是将其导入到 SQL Server 上的 Temp 表中。然后,您将能够编写一个查询,从 Temp 表中检索员工姓名,将其连接到 Manager 表,然后插入到真正的 Employee 表中。
Instead of importing the Employee data to the Employee table, import it into a Temp table on SQL server. Then you will be able to write a query which retrieves the employee name from the Temp table, joins it to the Manager table and then inserts into the real Employee table.
对此有很多解决方案,但如果数据集相对较小,我只需在 Excel 中执行此操作:
There are many solutions to this, but if the data set is relatively small, I'd just do this in Excel: