Search code examples
asp-classicparametersrecordsetprocedure

How to run a stored procedure with param and store result as a record set in classic asp


I wasn't able to find a question/answer that covers this fully hence why I am asking. What I need to do is run a stored procedure that takes 1 parameter. It will return a set of results which I need to store in a record set. I plan to loop through this recordset later. I'm pretty inexperienced when it comes to older asp, but here is what I have to far:

dim myConn
Set myConn = Server.CreateObject("ADODB.Connection")
myConn.Open = ("DSN=example-dsn;SERVER=example-server;DATABASE=example-db;UID=user;PWD=pass;")

dim oStoredProc : Set oStoredProc = Server.CreateObject("ADODB.Command")
With oStoredProc
    .ActiveConnection = myConn
    .CommandType = adCmdStoredProc
    .CommandText = "myStoredProcedure"
    .Parameters.Append(.CreateParameter("@PARAM1", ADODB.adInteger, ADODB.adParamInput, 10, 2012))
    Dim rs : Set rs = .Execute() 

End With

// Will loop through it here.

My guess is that I'm not setting up the recordset right, but like I said, I'm not really sure. If anyone can point me in the right direction I'd appreciate it!


Solution

  • Alright there were a few things I was doing wrong but here is what ended up working for me. First off it turns out I didn't need a parameter passed in, but that was not the problem anyway. One of the main issues what that 'adCmdStoredProc' wasn't recognized, which is odd because I've seen it used everywhere else, but replacing it with it's corresponding value, 4, did work.

    dim myConn, cmd
    
    Set myConn = Server.CreateObject("ADODB.Connection")
    myConn.Open = ("DSN=[BLAH];SERVER=[SERVER];DATABASE=[BLAH];UID=[User];PWD=[Pass];")
    
    dim oStoredProc : Set oStoredProc = Server.CreateObject("ADODB.Command")
    oStoredProc.CommandType = 4 
    oStoredProc.CommandText = "StoredProcedureName"
    oStoredProc.ActiveConnection = myConn
    // Add parameters here if needed.
    
    Dim rs 
    Set rs = oStoredProc.Execute()
    
    // I Loop through here
    
    rs.Close
    myConn.Close
    Set rs = Nothing
    Set oStoredProc = Nothing
    Set myConn = Nothing
    

    I hope this helps if anyone else needs it.