|
本帖最后由 microyip 于 2016-3-4 21:45 编辑
如图,想将数据库里的”字段1“与表格对应的数据的”字段1“相同的,数据库对应的那条记录的”字段2“全部设为”ABC“
Function Excel_SQL()
'本例,数据库须存在GA_2016库,数据表SQLDB,并包含“字段1、字段2、字段3、字段4”
Dim sSQL_Server As String
Dim sSQL_Password As String
Dim sDB_Name As String
Dim sDBFormName As String
Dim aExcel_Conn As ADODB.Connection
Dim sSQL_Conn As String
Dim sSQL_Case As String
Application.ScreenUpdating = False
sSQL_Server = "192.168.1.229"
sSQL_Password = "RuningTime"
sDB_Name = "GA_2016"
sDBFormName = "SQLDB"
Set aExcel_Conn = CreateObject("Adodb.Connection")
aExcel_Conn.ConnectionString = _
"Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
aExcel_Conn.Open
sSQL_Conn = _
"[ODBC;Driver={SQL Server};Server=" & sSQL_Server & ";Database=" & sDB_Name & ";UID=sa;PWD=" & sSQL_Password & ";]"
sSQL_Case = "Insert Into " & sSQL_Conn & "." & sDBFormName & " Select * From [测试数据表$K1:N10] As R Where R.字段3<>'ExcelDB.3'"
aExcel_Conn.Execute sSQL_Case
'Insert Into 语句执行正常
sSQL_Case = "Select * From " & sSQL_Conn & "." & sDBFormName
[测试数据表!U2].CopyFromRecordset aExcel_Conn.Execute(sSQL_Case)
'Select 语句执行正常
sSQL_Case = "Update " & sSQL_Conn & "." & sDBFormName & " As W ,[测试数据表$K1:N9] As R Set W.字段2='ABC' Where W.字段1=R.字段1"
sSQL_Case = "Update " & sSQL_Conn & "." & sDBFormName & " As W ,[测试数据表$K1:N9] As R Set W.字段2='ABC' Where W.字段1='aa'"
Debug.Print sSQL_Case
aExcel_Conn.Execute sSQL_Case
'无论哪句的Update 语句都无法执行
aExcel_Conn.Close
Set aExcel_Conn = Nothing
Application.ScreenUpdating = True
End Function
求指正
|
|