如何使用excel中的select插入数据

时间:2014-02-12 05:46:28

标签: c# sql-server excel oledb

我在excel中插入数据大数据,所有数据都来自sql server,我想准备插入查询,这将一次插入一堆记录...

INSERT INTO [Sheet1$] (ID,Name)
SELECT '1' AS ID,'XYZ' AS Name UNION
SELECT '1' AS ID,'XYZ' AS Name UNION
SELECT '1' AS ID,'XYZ' AS Name

但它会给我一个像

这样的错误
Query input must contain at least one table or query.

1 个答案:

答案 0 :(得分:0)

尝试此选项。

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=D:\testing.xlsx;;HDR=YES;IMEX=1', 'SELECT Id, Name FROM [Sheet1$]')
SELECT '1' AS ID,'XYZ' AS Name
UNION ALL
SELECT '1' AS ID,'XYZ' AS Name 
UNION ALL
SELECT '1' AS ID,'XYZ' AS Name

这里我假设您已经在sql server中启用了 Ad Hoc Distributed Queries 选项。