Execute stored procedure from VBScript

Posted on 2009-02-11
Last Modified: 2012-05-06
Hi guys,

As you can see from the code attached I am currenlty creating a connection to a SQL Server DB and then executing two SQL statements 1 to insert some data and the other to return. This is the proof of concept and is working well.

I now want to move the code into stored procedures as the SQL needs to be rolled out to many scripts and I want to centrally manage the code. I have created the stored procedures with the input parameters and they work fine.

How do I now change the code below to execute the stored procedures attaching the parameters?

Any help will be greatfully recieved.
Str_Connect = "Provider=SQLOLEDB;Data Source=srv02\sqlexpress;Initial Catalog=IPPbxDynamic"

Set CnnSQL=CreateObject("ADODB.Connection")

CnnSQL.Open Str_Connect,"***", "*****"

If CnnSQL.State=1 Then	'** Check if the connection is alive


'  Insert the current call into the DB


  strInsertSQL = "Insert INTO tblDynamicCallDetails (CallId, ScriptName) VALUES ('123','456')"	' ** Build the dynamic SQL Statement



'  Insert the current call into the DB


  strReturnSQL = "Select * from tblDynamicCallDetails"	' ** Build the dynamic SQL Statement

  Set Rs=CreateObject("ADODB.Recordset")

  Set Rs=CnnSQL.Execute(strSQL)

  Do while not Rs.EOF

	'write the responses back to variables




  Set Rs = Nothing


  'Msgbox "Not Connected"

End If


Set cnnSQL = Nothing

Open in new window

Question by:Nigel_Taylor
    LVL 39

    Assisted Solution

    by:Kyle Abrahams

    strInsertSQL ="
    "declare @p1 int;declare @p2 varchar(100);select @p1 = 1, @p2='test'; exec mystoredproc @p1, @p2"

    if you need the parameters instead of test you can call a replace or just break it down.

    select @p1 = " & myParam &  <rest of query>
    LVL 75

    Accepted Solution

    '   Set Rs=CreateObject("ADODB.Recordset")           Not needed before or now
    strSQL = "exec YourStoredProcedure '123', '123'"
    Set Rs=CnnSQL.Execute(strSQL)

    Author Closing Comment

    Thanks guys for the solutions. That has been a great help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now