2013-08-21 32 views
0

我想插入Excel電子表格中的某些列到基於其中一列中的值的sql中,我已經編寫了sql代碼,但是當我通過它繼續添加的代碼循環時一個新的querytbale。有沒有辦法將值插入到SQL刷新查詢表與新值並插入到相同的SQL表?如何循環查詢表中的sql代碼

sqlString = "Insert into table(fields) select '" & ws1.Cells(row, col) & "','" & ws1.Cells(row, co2) & "','" & ws1.Cells(row, col3) & "','" & ws1.Cells(row, col4) & "','" & ws1.Cells(row, col5) & "','" & ws1.Cells(row, col6) & "'" 


Set destination = ws2.Range("A1") 
Set pleasework = ws2.QueryTables.Add(conString, destination, sqlString) 

sqlString = "Insert into table(fields) select '" & ws1.Cells(row, col) & "','" & ws1.Cells(row, co2) & "','" & ws1.Cells(row, col3) & "','" & ws1.Cells(row, col4) & "','" & ws1.Cells(row, col5) & "','" & ws1.Cells(row, col6) & "'" 

Do 

With pleasework 

     row = row + 1 
     .CommandText = sqlString 
     .Refresh 
     count = count + 1 

End With 
Loop While count < 5 

回答

0

您可以discart你的代碼,並嘗試這樣的事:

query = INSERT INTO [ODBC;DRIVER=SQL Server;SERVER=MY_COMPUTER\SQLEXPRESS;Trusted_Connection=Yes;DATABASE=MY_DATABASE].[tbMYTALBE] ([FIELD1], [FIELD2], [FIELD3]) SELECT t.FIELD1, t.FIELD2, t.FIELD3 FROM [MY_SHEET$A1:D200] t 
Call recordSet.Open(query, connection) 

如果你需要它

我可以添加完整的代碼