Calling a Stored Procedure from Excel with multiple parameters

后端 未结 4 779
生来不讨喜
生来不讨喜 2021-01-14 04:54

I have set up a connection to my SQL server to the database where the stored procedure is located. The Stored Procedure works fine in SQLServer. The Stored Procedure runs

相关标签:
4条回答
  • 2021-01-14 05:15

    1 – expose parametars in excel

    2 – define that parameters are entered from excel

    3 – OK and refresh.

    OK, You don't have to enter parameters in the command text, you can define command text in a way to expect parameters to be passed to the store procedure from excel cell. This is useful especially if your country don’t use US data format. Also, it enables end user to make refresh with some other parameters without need to edit command text. It is simple – in the excel that have connection to sql server in command text after store procedures name you enter ? – as many ? as you have different parameters that your store procedure expects (divided by ,) – like this - execute dbo.your_procedure ?,? After that, you go in the parameters tab (on the same form that you entered command text) and define from witch cell witch parameter is passed to store procedure. Of course, in the stored procedure also need to be specified what parameters are expected:

    CREATE procedure [dbo].[your_procedure]

    ( @DateFrom datetime, @DateTo datetime ) As

    --- your store procedure ---

    In excel – parameter 1 will be send to the store procedure to a DateFrom parametar.

    0 讨论(0)
  • 2021-01-14 05:15

    You're missing that comma

    "EXECUTE dbo.GetBillingHeatMap '" & Range("A9").Value & "','" & Range("B9").Value & "'"

    0 讨论(0)
  • 2021-01-14 05:24

    When you execute a stored procedure, the statement generally looks like:

    EXECUTE myProc('param1', 'param2')
    

    The way your command will expand will come out like:

    EXECUTE myProc 'param1param2`
    

    Which is nonsense. Try instead:

    With ActiveWorkbook.Connections("CARLA-PC-Billing-SP").OLEDBConnection.CommandText = "EXECUTE dbo.GetBillingHeatMap ('" & Range("A9").Value & "','" & Range("B9").Value & "');"
    

    You may still run into issues with the date formatting, so you can handle that in VBA too:

    With ActiveWorkbook.Connections("CARLA-PC-Billing-SP").OLEDBConnection.CommandText = "EXECUTE dbo.GetBillingHeatMap ('" & FORMAT(Range("A9").Value, "m/d/yyyy") & "','" & FORMAT(Range("B9").Value, "m/d/yyyy") & "');"
    

    Lastly, I find it good practice to send dynamically generated sql to a variable, and print it to the debug/immediate window before executing so I can catch stuff like that.

    sqlStatement = "EXECUTE dbo.GetBillingHeatMap ('" & FORMAT(Range("A9").Value, "m/d/yyyy") & "','" & FORMAT(Range("B9").Value, "m/d/yyyy") & "');"
    debug.print sqlStatement
    With ActiveWorkbook.Connections("CARLA-PC-Billing-SP").OLEDBConnection.CommandText = sqlStatement
    

    And now you'll have your statement in your immediate window, which you can copy and paste into a SQL client to execute independently of your code.

    0 讨论(0)
  • 2021-01-14 05:30
    Sub Samp()
        Public con As New ADODB.Connection
        Public PassRecordset As New ADODB.Recordset
        Public objCmd As New ADODB.Command
    
        Dim Para1 As ADODB.Parameter
        Dim Para2 As ADODB.Parameter
        Dim Para3 As ADODB.Parameter
    
        'Check the database connection
        If con.State = 0 Then If Not CreateConnection 
           Then Exit Sub
    
        Set objCmd = New ADODB.Command
        objCmd.ActiveConnection = con
    
        With objCmd
          .ActiveConnection = con
          .CommandType = adCmdStoredProc
          .CommandText = StoredProcedureName
    
       End With
        
       Set Para1 = objCmd.CreateParameter("@TextPara1", adVarChar, adParamInput, 50, "Value1")
       objCmd.Parameters.Append Para1
    
       Set Para2 = objCmd.CreateParameter("@TextPara2", adVarChar, adParamInput, 255, "Value2")
       objCmd.Parameters.Append Para2
        
       Set Para3 = objCmd.CreateParameter("@DatePara3", adDate, adParamInput, , "DateValue")
       objCmd.Parameters.Append Para3
        
       Set PassRecordset = objCmd.Execute
        
       Sheet1.Range("A1").CopyFromRecordset PassRecordset
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题