Если вы открыты для использования VBA, попробуйте это.
Sub InsertInto()
'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String
'Create a new Connection object
Set cnn = New adodb.Connection
'Set the connection string
cnn.ConnectionString = "Your_Server_Name;Database=Your_DB_Name;Trusted_Connection=True;"
'Create a new Command object
Set cmd = New adodb.Command
'Open the connection
cnn.Open
'Associate the command with the connection
cmd.ActiveConnection = cnn
'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText
'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = 2013-01-13 WHERE EMPID = 1"
'Pass the SQL to the Command object
cmd.CommandText = strSQL
'Open the Connection to the database
cnn.Open
'Execute the bit of SQL to update the database
cmd.Execute
'Close the connection again
cnn.Close
'Remove the objects
Set cmd = Nothing
Set cnn = Nothing
End Sub
Аналогично, вы можете сделать что-то вроде этого.
Sub WriteDataToSQLServer()
Dim cn As Object
Dim rs As Object
Dim n As Long
Dim strQuery As String
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "sqloledb"
.ConnectionString = "Data Source=server_name;Initial Catalog=database_name;Integrated Security=SSPI;"
.Open
End With
strQuery = "Table name here"
Set rs = CreateObject("ADODB.Recordset")
With rs
.Open strQuery, cn, 1
For n = 1 To 10
.AddNew
.Fields("Field1").Value = Cells(n, "A").Value
.Fields("Field2").Value = Cells(n, "B").Value
.Update
Next n
.Close
End With
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub