+ Reply to Thread
Results 1 to 25 of 25

SQL Stored Procedure From Excel

Hybrid View

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    SQL Stored Procedure From Excel

    Hi,

    I have a stored Procedure that updates only 1 row (No changes in the count of rows).

    I am using the below code to execute the Stored Procedure.

    Sub ExecuteStoredProcUpdate_Yield()
    
        Dim SQLConn As ADODB.Connection
        Dim SQLCommand As ADODB.Command
        Dim SourceQuery As String
    
        Set SQLConn = New ADODB.Connection
        Set SQLCommand = New ADODB.Command
        
        SQLConn.ConnectionString = ConnString
        
        SourceQuery = "EXECUTE DB_Common..Update_Yield"
             
        On Error GoTo CloseConnection
        
        SQLConn.Open
        
        With SQLCommand
            .ActiveConnection = SQLConn
            .CommandType = adCmdStoredProc
            .CommandText = SourceQuery
        End With
                    
        On Error GoTo 0
        SQLConn.Close
        
        Exit Sub
        
    CloseConnection:
        MsgBox "SQL Stored Procedure Did Not Execute Sucessfully!", vbOKOnly, "SQL Error"
        SQLConn.Close
    
    End Sub
    Is there a way I come to know If the Stored Proc Executed properly?

    Is there a way of knowing if it is running as desired at all?
    Cheers!
    Deep Dave

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Stored Procedure From Excel

    What does the sproc return? What database are you using?

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by Kyle123 View Post
    What does the sproc return? What database are you using?
    Hi Kyle,

    The Stor Proc does not return anything.. All it does is update the Value of a Row in the Table to Zero..
    Last edited by NeedForExcel; 05-22-2015 at 12:11 AM.

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    You can pass a variable to either of Command.Execute method Or Connection.Execute methods as an argument Recordsaffected. After execution t will hold the number of affected records.
    You can also use option adExecuteNoRecords to tell the provider not to construct Recordset Object if it is not a row-returning query
    If you are pleased with a member's answer then use the Star icon to rate it.

  5. #5
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by buran View Post
    You can pass a variable to either of Command.Execute method Or Connection.Execute methods as an argument Recordsaffected. After execution t will hold the number of affected records.
    You can also use option adExecuteNoRecords to tell the provider not to construct Recordset Object if it is not a row-returning query
    Hi Buran,

    Actually I am learning ADO hence I am not understanding very clearly what exactly is to be done..

    Can you please clarify..

  6. #6
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    First of all, I don't see the Execute method of SQLCommand, so I'm not sure how do you actually use this code to run the stored proc.
    Second - I'm not able to test, so below code may have errors

    Sub ExecuteStoredProcUpdate_Yield()
    
        Dim SQLConn As ADODB.Connection
        Dim SQLCommand As ADODB.Command
        Dim SourceQuery As String
        Dim lngRecordsAffected As Long 'This will be the variable to hold the number of affected records
    
        Set SQLConn = New ADODB.Connection
        Set SQLCommand = New ADODB.Command
        
        SQLConn.ConnectionString = ConnString
        
        SourceQuery = "EXECUTE DB_Common..Update_Yield"
             
        On Error GoTo CloseConnection
        
        SQLConn.Open
        
        With SQLCommand
            .ActiveConnection = SQLConn
            .CommandType = adCmdStoredProc
            .CommandText = SourceQuery
            
            ' Next it should execute the command.
            ' Note that adExecuteNoRecords prevents creating Recordset object
            ' It's possible to use it, because you said you procedure does not return anything
            
            .Execute Recordsaffected:=lngRecordsAffected, Options:=adExecuteNoRecords
        End With
        
        'Stored procedure executed without rising an error. Display msg how many records were affected
        MsgBox "Execution of SQL Stored procedure affected " & lngRecordsAffected & " records."
                    
        On Error GoTo 0
        SQLConn.Close
        
        Exit Sub
        
    CloseConnection:
        MsgBox "SQL Stored Procedure Did Not Execute Sucessfully!", vbOKOnly, "SQL Error"
        SQLConn.Close
    
    End Sub

  7. #7
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by buran View Post
    First of all, I don't see the Execute method of SQLCommand, so I'm not sure how do you actually use this code to run the stored proc.
    Actually I tried using the Execute method, which gave a run time error when I ran the code..

    Let me see how this code works.. I will get back in some time..

    Cheers!

  8. #8
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    I tried it, but It returns error on the Execute method..

    Are you asking the DB Name? If yes, it is DB_Common

  9. #9
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    What is your DB?

  10. #10
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    No, I mean MS SQL, MySQL, Oracle, etc...
    also what is the exact error msg?

  11. #11
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Its SQL.

    The error screenshot -
    Attached Images Attached Images

  12. #12
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by NeedForExcel View Post
    Its SQL.
    SQL is the language, it comes in different flavours and implementations. The underlying database could be MS SQL Server, MySQL, Oracle, Ms Access, sqlite PostgreSQL, to name a few.
    I'm not sure about the double dots in your SQL statement. I, as well as Kyle123, am asking about that.
    For example in MS SQL it's enough to supply it's stored procedure name to command object CommandText property. In this case you should use
    .CommandType = adCmdStoredProc
    However in your code you use SQL string, so I think
    .CommandType = adCmdText
    That is something, together with the 2 dots in the sql string, that I didn't notice

  13. #13
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Hi Buran,

    Its MS SQL (SSMS)...

    And, Double dots were new to me too, but that is how it is used in the place where I work..

    And yes, If I use .CommandType = adCmdText, do I still need the Execute method?

  14. #14
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by NeedForExcel View Post
    Hi Buran,

    And yes, If I use .CommandType = adCmdText, do I still need the Execute method?
    yes, you should use Execute

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Stored Procedure From Excel

    I'm pretty sure that MSSQL sprocs always return an int. If nothing is specified or null is returned it's 0, your sproc should really have a return value to indicate the outcome - so you need to change that.

    Then it's simply:

        SQLConn.Open
        
        Dim returnValue As ADODB.Parameter
        
        
        
        With SQLCommand
            .ActiveConnection = SQLConn
            .CommandType = adCmdStoredProc
            .CommandText = SourceQuery
            
            'Create an output param for the return value
            Set returnValue = .CreateParameter("@RETURN_VALUE", adInteger, adParamReturnValue)
            'Add the output param
            .Parameters.Append returnValue
            .Execute
        End With
        
        If returnValue = 0 Then MsgBox "Something went wrong", vbCritical
    Your sproc should then have a line:

    	RETURN @@rowcount
    That returns the number of rows affected
    Last edited by Kyle123; 05-22-2015 at 04:01 AM.

  16. #16
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Thank you Buran & Kyle as always..

    Guess I'll need a guy with some SQL Experience to help me create that output parameter.

    Marking the thread solved as of now, but will get back in case I need further assistance..

  17. #17
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SQL Stored Procedure From Excel

    So did it work?

  18. #18
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by buran View Post
    So did it work?
    Not as of yet.. Trying real hard..

  19. #19
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Stored Procedure From Excel

    It's only that one line, will look something like:

    EDIt had to attach as it's SQL (firewall doesn't like it)
    Attached Files Attached Files

  20. #20
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Quote Originally Posted by Kyle123 View Post
    It's only that one line, will look something like:

    EDIt had to attach as it's SQL (firewall doesn't like it)
    Not sure If I am allowed to do that.. Let me check it out..

    By the way, can you guide me as to where I can get some reading material on ADODB?

  21. #21
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Stored Procedure From Excel

    Without doing it, there's no way of knowing whether anything was updated using only the stored proc.

    Re: the reading, not really, there's not a great deal to it. The MSDN is good for checking things out, but there's really only Commands, Connections and Recordsets

  22. #22
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Alright!

    Thank You for the help

  23. #23
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: SQL Stored Procedure From Excel

    Hi Kyle & Buran,

    I managed to add to the Stored Proc as required..

    ALTER PROCEDURE [dbo].[Update_Yield]
    	-- Add the parameters for the stored procedure here
    
    @RowCount integer OUTPUT
    AS
    
    //Queries//
    
    return
    
    END

    And My Final ADO Code is

    Sub ExecuteStoredProcUpdate_Yield()
    
        Dim SQLConn As ADODB.Connection
        Dim SQLCommand As ADODB.Command
        Dim SourceQuery As String
        Dim lngRecordsAffected As Long 'This will be the variable to hold the number of affected records
    
        Set SQLConn = New ADODB.Connection
        Set SQLCommand = New ADODB.Command
        
        SQLConn.ConnectionString = ConnString
        
        SourceQuery = "EXECUTE DB_Common..Update_Yield"
             
        On Error GoTo CloseConnection
        
        SQLConn.Open
        
        Dim returnValue As ADODB.Parameter
        
        
        With SQLCommand
            .ActiveConnection = SQLConn
            .CommandType = adCmdStoredProc
            .CommandText = SourceQuery
            
            'Create an output param for the return value
            Set returnValue = .CreateParameter("@RowCount", adInteger, adParamReturnValue)
            'Add the output param
            .Parameters.Append returnValue
            .Execute
        End With
        
        If returnValue = 0 Then MsgBox "Something went wrong", vbCritical
        On Error GoTo 0
        SQLConn.Close
        
        Exit Sub
        
    CloseConnection:
        MsgBox "SQL Stored Procedure Did Not Execute Sucessfully!", vbOKOnly, "SQL Error"
        SQLConn.Close
    
    End Sub
    But still not working.. After the .Execute Method, it jumps to the CloseConnection label..
    Last edited by NeedForExcel; 05-22-2015 at 07:13 AM.

  24. #24
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,983

    Re: SQL Stored Procedure From Excel

    Shouldn't your SourceQuery variable just be the name of the procedure?
    SourceQuery = "DB_Common..Update_Yield"
    Last edited by romperstomper; 05-22-2015 at 07:57 AM.
    Everyone who confuses correlation and causation ends up dead.

  25. #25
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Stored Procedure From Excel

    Your sproc looks nothing like the one I uploaded - so it's completely wrong.

    Your params are wrong too, the param names are crucial - it should be exactly the same as my example

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. SQL Stored Procedure (Without Parameters) In Excel
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-13-2015, 06:44 AM
  2. Insert stored procedure within excel vba
    By drobinson782001 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2013, 01:24 PM
  3. Replies: 0
    Last Post: 10-02-2012, 03:06 PM
  4. How to use Oracle stored procedure in Excel
    By mnjogin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-10-2010, 06:04 AM
  5. [SOLVED] Getting stored procedure result to excel
    By mkarja in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-09-2005, 09:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1