简体   繁体   English

使用select into将所选信息从excel导入到SQL

[英]Importing selected information from excel to with SQL with select into

I am trying to import specific information from specific file and place in the workbook. 我正在尝试从特定文件中导入特定信息,并将其放入工作簿中。

Let: 让:

"Bluelake.xlsm" will be the file name (workbook) "Time" will be the Sheet The information is in a table "C3" to "C8". “ Bluelake.xlsm”将是文件名(工作簿)“ Time”将是工作表信息在表“ C3”至“ C8”中。

The parameters for the sql table are: sql表的参数为:

TableName: processStatus TableName:processStatus
Columns: processStatus where values are: varchar(32) /any string 列:processStatus,值是:varchar(32)/任何字符串

My question: 我的问题:

  1. How should the code look like provided the specific excel coordinates 提供特定的excel坐标后,代码应如何显示
  2. How do I incorporate the variable definition varchar(32) 如何合并变量定义varchar(32)

I know the bookish example for "insert into": 我知道“插入”的书呆子示例:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); INSERT INTO table_name(column1,column2,column3,...)VALUES(value1,value2,value3,...);

and unfortunately I do not know how to expand it. 不幸的是,我不知道如何扩展它。

Thank you in advance. 先感谢您。

INSERT INTO processStatus (processStatus1, processStatus2, processStatus3
            ,processStatus4, processStatus5, processStatus6)
SELECT (C3, C4, C5, C6, C7, C8)
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
                'Excel 12.0;HDR=YES;Database=Bluelake.xlsx',
                'select * from [Time$]');

Note: If you actually have named columns in the Excel sheet you will need to replace them here. 注意:如果实际上在Excel工作表中已命名列,则需要在此处替换它们。

And you did not provide the column names for your processStatus table so I replaced them with generic names that you will also need to replace in your code. 而且您没有为processStatus表提供列名,因此我用通用名称替换了它们,您也需要在代码中替换它们。

Your "Database" needs to be the fully qualified file path to your Excel file. 您的“数据库”必须是Excel文件的标准文件路径。

Finally, if you do need to change the data type you will need to CAST each column. 最后,如果确实需要更改数据类型,则需要CAST每列。 Ex: 例如:

CAST(C3 AS VARCHAR(32))

Hope this helps! 希望这可以帮助!

edit: One other thing, you might need to use a different OLEDB driver for XLSX. 编辑:另一件事,您可能需要为XLSX使用不同的OLEDB驱动程序。 It may be easier to save as the older format (XLS) from Excel if it is a simple spreadsheet. 如果它是一个简单的电子表格,则可能更容易从Excel中另存为旧格式(XLS)。 Or you can take it one step further, using the SQL Import Wizard to bring the Excel data into a table, then your INSERT INTO statement is much more simplified as you will simply SELECT FROM your table instead of using OPENROWSET to query the Excel file. 或者,您可以更进一步,使用SQL导入向导将Excel数据放入表中,然后您的INSERT INTO语句将更加简化,因为您只需从表中进行选择,而不使用OPENROWSET来查询Excel文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM