-1

I have a table in MS SQL Server 2012 which looks something like what is shown below but with several hundred records

id   | adminnumber  |
1      006              
2      012
3      239
4      546
5      123
6      637

I have an excel sheet which contains proposed updates for these several hundred records of the adminnumber field so the sheet contains something like

adminnumber original  | adminnumber change
006                     673 
012                     134
239                     435
546                     783
123                     347
637                     903

Is it possible to write a script to update the adminnumbers from adminnumber original to adminnumber change and is it possible to do so without creating a temporary table?

5
  • Yes it is possible and yes it is possible. Have you tried anything to solve this issue? If so, please provide what you tried and explain what you found to not work. Commented Jun 23, 2016 at 21:37
  • Sure: (1) send an SQL update for each record to the server (2) aggregate all the updates with a table variable (instead of a temp table). Yet, this option is limited to 1,000 updates at a time (3) create an SSIS package and execute it through Excel VBA upon request, (4) export all the updates into a CSV, upload that CSV to a shared location which is available for the SQL server, and then update on the server using OpenRowSet or BULK INSERT... these are just to name a few of the multiple options you have....Try to be more specific. Otherwise, we can't help. Commented Jun 23, 2016 at 21:38
  • @Ralph thanks how can I achieve 1 or 2? Ideally I just wanted to run 1 query to update all Commented Jun 23, 2016 at 22:04
  • One query only? If you have only 1,000 records (or less) to update then it will work with a table variable. Otherwise, you'll have to send several SQL statements to the server (in sets of 1,000 each). To establish a connection to an SQL using VBA you can use this: stackoverflow.com/questions/30289095/… To dynamically create the table variable for the SQL statement you can use this: stackoverflow.com/questions/37861150/… Commented Jun 23, 2016 at 22:09
  • @Ralph Thanks very much very helpful! Commented Jun 23, 2016 at 22:36

1 Answer 1

0

Consider a direct connection to workbook with distributed queries using SQL Server's OPENDATASOURCE or OPENROWSET functions:

UPDATE TableName
SET t.adminnumber = s.[adminnumber change]
FROM TableName t    
INNER JOIN    
    OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
        'Data Source=C:\Path\To\Workbook.xls;Extended Properties=Excel 8.0')...Sheet$ s    
ON t.adminnumber = s.[adminnumber original]

Alternatively, consider a parameterized query in VBA using ADO where you iteratively bind the old and new numbers in an update query command. Below loops through columns A and B in Sheet 1:

Public Sub UpdateSQL()
    Dim conn As Object, cmd As Object
    Dim constr As String, strSQL As String
    Dim adCmdText As Integer: adCmdText = 1
    Dim adInteger  As Integer: adInteger = 3
    Dim adParamInput As Integer: adParamInput = 1
    Dim i As Integer, lastrow As Long    

    ' OPEN DB CONNECTION '
    Set conn = CreateObject("ADODB.Connection")
    constr = "DRIVER={SQL Server};server=servername;database=databasename;" _
                   & "UID=username;PWD=password;"
    conn.Open constr

    strSQL = "UPDATE [TableName] SET [adminnumber] = ?" _
              & " WHERE [adminnumber] = ?"    
    lastrow = Worksheets(1).Cells(Worksheets(1).Rows.Count, "A").End(xlUp).Row

    For i = 2 To lastrow        
        ' SETTING COMMAND
        Set cmd = CreateObject("ADODB.Command")
        With cmd
            .ActiveConnection = conn
            .CommandText = strSQL
            .CommandType = adCmdText
            .CommandTimeout = 15
        End With            
        ' BINDING PARAMETERS
        cmd.Parameters.Append cmd.CreateParameter("newNum", adInteger, adParamInput, 3)
        cmd.Parameters(0).Value = Worksheets(1).Range("B" & i)
        cmd.Parameters.Append cmd.CreateParameter("oldNum", adInteger, adParamInput, 3)
        cmd.Parameters(1).Value = Worksheets(1).Range("A" & i)
        'EXECUTE QUERY
        cmd.Execute
    Next i    

    conn.Close        
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.