2017-11-07 1 views
0

Ich habe vor kurzem in SQL Server versucht, einige Abfrage zu erstellen, um Informationen (in Excel) von unserer ERP-System (JobBoss) -Datenbank zurückzugeben. Ich frage mich:Aktualisieren/Hochladen von Werten von Excel zu SQL Server-Datenbank

Gibt es eine Möglichkeit zum Aktualisieren/Ändern von Werten in der SQL Server-Datenbank von Excel?

Zum Beispiel habe ich eine Verbindung (in Excel) zu unserem SQL Server bereits hergestellt, und haben eine Abfrage, die bestimmte Werte aus bestimmten Tabellen auswählt, um einen Bericht zu erstellen. Allerdings habe ich mich gefragt, ob ich einfach die Werte in Excel ändern und dann irgendwie mit der Datenbank "uploaden/synchronisieren" kann?

Wenn ja, welche Möglichkeiten gibt es?

Danke

+0

Sicher ist es möglich, Änderungsdaten in SQL Server von Excel zu aktualisieren /. Dies geschieht über ODBC. –

+0

https://stackoverflow.com/questions/29299444/excel-vba-ado-update-sql-table-record –

Antwort

0

Von Excel zu SQL Server? Sie haben mehrere Möglichkeiten.

Setup sucht Link dieses: enter image description here

Sub Rectangle1_Click() 
'TRUSTED CONNECTION 
    On Error GoTo errH 

    Dim con As New ADODB.Connection 
    Dim rs As New ADODB.Recordset 
    Dim strPath As String 
    Dim intImportRow As Integer 
    Dim strFirstName, strLastName As String 

    Dim server, username, password, table, database As String 


    With Sheets("Sheet1") 

      server = .TextBox1.Text 
      table = .TextBox4.Text 
      database = .TextBox5.Text 


      If con.State <> 1 Then 

       con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;" 
       'con.Open 

      End If 
      'this is the TRUSTED connection string 

      Set rs.ActiveConnection = con 

      'delete all records first if checkbox checked 
      If .CheckBox1 Then 
       con.Execute "delete from tbl_demo" 
      End If 

      'set first row with records to import 
      'you could also just loop thru a range if you want. 
      intImportRow = 10 

      Do Until .Cells(intImportRow, 1) = "" 
       strFirstName = .Cells(intImportRow, 1) 
       strLastName = .Cells(intImportRow, 2) 

       'insert row into database 
       con.Execute "insert into tbl_demo (firstname, lastname) values ('" & strFirstName & "', '" & strLastName & "')" 

       intImportRow = intImportRow + 1 
      Loop 

      MsgBox "Done importing", vbInformation 

      con.Close 
      Set con = Nothing 

    End With 

Exit Sub 

errH: 
    MsgBox Err.Description 
End Sub 

Auch diese Option in Betracht ziehen.

Sub UpdateTable() 

    Dim cnn As Object 
    Dim wbkOpen As Workbook 
    Dim objfl As Variant 
    Dim rngName As Range 
    Workbooks.Open "C:\Users\Excel\Desktop\Excel_to_SQL_Server.xls" 
    Set wbkOpen = ActiveWorkbook 
    Sheets("Sheet1").Select 
    Set rngName = Range(Range("A1"), Range("A1").End(xlToLeft).End(xlDown)) 
    rngName.Name = "TempRange" 
    strFileName = wbkOpen.FullName 
    Set cnn = CreateObject("ADODB.Connection") 
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes"";" 
    nSQL = "INSERT INTO [odbc;Driver={SQL Server};Server=Excel-PC\SQLEXPRESS;Database=[Northwind].[dbo].[TBL]]" 
    nJOIN = " SELECT * from [TempRange]" 
    cnn.Execute nSQL & nJOIN 
    MsgBox "Uploaded Successfully" 
    wbkOpen.Close 
    Set wbkOpen = Nothing 

End Sub 

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 = "Excel-PC\SQLEXPRESS;Database=Northwind;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 = 2" 

'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 

Siehe den untenstehenden Link für einige zusätzliche Ideen, wie Sie dies tun können.

https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction

Verwandte Themen