假设你使用SQL Server:
insert into users (name)
SELECT distinct a.UserName
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [Sheet1$]) a
where not exists (select null from Users where Name = a.UserName)
insert into Products (Name)
SELECT distinct a.ProductName
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [Sheet1$]) a
where not exists (select null from Products where Name = a.ProductName)
insert into Ratings (Rating)
SELECT distinct a.Rating
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [Sheet1$]) a
where not exists (select null from Ratings where Name = a.Rating)
insert into ProdRatings (UsrID, ProdID, RatID, DateRated)
SELECT Users.UsrID, Products.ProdID, Ratings.RatID, a.DateRated
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [Sheet1$]) a
inner join Users
on a.UserName = Users.Name
inner join Products
on a.ProductName = Products.Name
inner join Ratings
on a.Rating = Ratings.Rating
left join ProdRatings
on Users.UsrID = ProdRatings.UsrID
and Products.ProdID = ProdRatings.ProdID
and Ratings.RatID = ProdRatings.RatID
where ProdRatings.ID is null
首先导入新用户,产品和等级,然后ProdRatings。
如果不使用SQL Server,首次导入Excel文件在你的标准的方式进入临时表,然后做局部插入。我希望这适合你的需求。