Solved

Run a SQL Stored Procedure from a .vbs File and return a parameter

Posted on 2013-12-04
5
1,853 Views
Last Modified: 2013-12-04
Hello

I need to execute a SQL Stored Procedure with a single input parameter (Integer) and return the result (Integer).
The code then needs to open a URL with the integer as a parameter ("exo://core_opportunity(x)")

I'm not sure how to connect to the database or return the parameter.
I need to run this from a .vbs file

Many Thanks
0
Comment
Question by:p-plater
  • 3
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39697250
<%
' make sure to include your connection string to your db first
dim result,MyNumber
MyNumber=25
result=someFunction(MyNumber)

Function someFunction(MyParam)

   	
	Set cmd = Server.CreateObject("ADODB.Command")
With cmd
   
   	.ActiveConnection = MyConnection
    .CommandType = adCmdStoredProc
   	.CommandText = "name_of_stored_procedure" 'Name of your stored procedure
   
   	.Parameters.Append .CreateParameter("@variableX",adDouble, adParamInput)
   	.Parameters("@variableX") = MyParam 'set any variables for the SP
   
   set someFunction = .Execute

 
set cmd = nothing


end Function
%>

Open in new window

0
 

Author Comment

by:p-plater
ID: 39697280
Thanks Padas

I am getting the error " Arguments are of the wrong type, are out of acceptable range or are in conflict with each other"

Below is my Code
dim MyConnection, strSQL 
set MyConnection = CreateObject("ADODB.Connection") 
MyConnection.provider="sqloledb" 
MyConnection.Mode = "3" 
MyConnection.open  "dcserver", "sa", "xxxxxxxx" 
MyConnection.DefaultDatabase = "CRM_Test" 

	
	
' make sure to include your connection string to your db first
dim result,MyNumber
MyNumber=25
result=someFunction(MyNumber)

msgbox result

Function someFunction(MyParam)

   	
Set cmd = CreateObject("ADODB.Command")

With cmd
   
   	.ActiveConnection = MyConnection
    .CommandType = 4'adCmdStoredProc
   	.CommandText = "x_test" 'Name of your stored procedure
   	.Parameters.Append .CreateParameter("@act",adDouble, adParamInput)
   	.Parameters("@act") = MyParam 'set any variables for the SP
end with
   
   set someFunction = .Execute

 
set cmd = nothing


end Function

Open in new window


Also my Stored Procedure Code
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Ronald Kidd
-- Create date: 2013-12-05
-- Description:	Open
-- =============================================
ALTER PROCEDURE [dbo].[x_test] 
	-- Add the parameters for the stored procedure here
	@act int = 0
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
    declare @RETURN_VALUE as int
   set @RETURN_VALUE = 25
	SELECT @RETURN_VALUE
	
	insert into X_PRG_ERRORLOG(COMPUTER,STAFF,TRIGGERED,ERROR)
		values('test','ters','test',@act)
	
	return 25
	
END

GO

Open in new window

0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 500 total points
ID: 39697292
Oh, I always have adovbs included too http://support.web.com/vserver/ntguide/microsoft_adovbs_inc_file.htm

What line is the error happening on?
0
 

Author Comment

by:p-plater
ID: 39697301
Line 27
0
 

Author Comment

by:p-plater
ID: 39697310
OK
I've changed line 27 to"Parameters.Append .CreateParameter("@act",5, 1)"
And "cmd" to the .execute

It now runs but the message box errors - Doesn't return the parameter.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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

14 Experts available now in Live!

Get 1:1 Help Now