简体   繁体   中英

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".

The parameters for the sql table are:

TableName: processStatus
Columns: processStatus where values are: varchar(32) /any string

My question:

  1. How should the code look like provided the specific excel coordinates
  2. How do I incorporate the variable definition varchar(32)

I know the bookish example for "insert into":

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.

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.

Your "Database" needs to be the fully qualified file path to your Excel file.

Finally, if you do need to change the data type you will need to CAST each column. Ex:

CAST(C3 AS VARCHAR(32))

Hope this helps!

edit: One other thing, you might need to use a different OLEDB driver for XLSX. It may be easier to save as the older format (XLS) from Excel if it is a simple spreadsheet. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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