English 中文(简体)
Excel 使用公式动态生成 SQL 脚本
原标题:Excel to dynamically generate SQL script using formula
I am trying to dynamically generate SQL INSERT scripts via Excel formulas My Excel sheet looks as below. Below is my formula ="INSERT INTO MYTABLE (ROLE_ID,GROUP,SUBGROUP) values( "&D8&" , "&$E$8&" , "&$F$8&" )" Now, I want the expected output to be generated as below; So basically, for Column G, I want the generated script to use data from Columns D, E and F. However, the information of Column D would change as per the current row, Columns E and F would repeat till we get an empty row (or as we can use value 0 in Column D to identify that it should now use new values for GROUP and SUBGROUP) ="INSERT INTO SOME_TABLE (MYROLE) values( "&E7&" );" INSERT INTO SOME_TABLE (MYROLE) values(null); INSERT INTO SOME_TABLE (MYROLE) values( My Role ); =IF(ISBLANK(E7), "INSERT INTO SOME_TABLE (MYROLE) values(null);", "INSERT INTO SOME_TABLE (MYROLE) values( "&E7&" );") insert into MYSCHEMA.MY_DESTINATION_TABLE(COL_ID, COL2) select USER1 , COL2 from MYSCHEMA.MY_SOURCE_TABLE where USER_ID = USER1 ; INSERT INTO MYSCHEMA.MY_DESTINATION_TABLE (COL_ID, COL2) SELECT COL_ID, COL2 FROM MYSCHEMA.MY_SOURCE_TABLE WHERE COL_ID IN (SELECT COL_ID FROM MYSCHEMA.MY_SOURCE_TABLE); INSERT INTO MYSCHEMA.MY_DESTINATION_TABLE (COL_ID, DESTINATION_COL2) SELECT COL_ID, SOURCE_COL2 FROM MYSCHEMA.MY_SOURCE_TABLE WHERE COL_ID IN (SELECT COL_ID FROM MYSCHEMA.MY_SOURCE_TABLE);
最佳回答
You can use: =IF(D8:D16=0,"","INSERT INTO MYTABLE (ROLE_ID,GROUP,SUBGROUP) values( "&D8:D16&" , "&E8:E16&" , "&F8:F16&" )")
问题回答

暂无回答




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签