Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored procedure does not return value 1

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
0
0
CN
In the query analyzer it works perfect but the sproc will not return its value to my vb program.
Here is the sproc:
CREATE PROCEDURE [mysproc]
@number int,
@@outnum int OUTPUT
AS
insert into test ([testid]) values (@number)
select @@outnum = @number
GO

Here is the vb code:
Dim conn As New ADODB.Connection
Dim comm As New ADODB.Command
Dim intInteger As Integer
conn.ConnectionString = "Provider=SQLOLEDB.1;Password=****;Persist Security Info=True;User ID=****;Initial Catalog=test;Data Source=****"
conn.Open
With comm
.ActiveConnection = conn
.CommandText = "mysproc"
.CommandType = adCmdStoredProc
'Input parameters
.Parameters.Append .CreateParameter("@number", adInteger, adParamInput, , 22)

'Output parameters
.Parameters.Append .CreateParameter("@@outnum", adInteger, adParamOutput, , intInteger)
.Execute , , ADODB.adExecuteNoRecords
' sproc has been executed and the value 22 has been inserted
' intInteger is still 0 WHY????
End With

When I run the sproc in the query analyzer I get the output value:
declare @outnum int
exec mysproc 22, @outnum output
select @outnum as test

output:
(1 row(s) affected)

test
-----------
22

(1 row(s) affected)

 
It is fixed:

.Parameters.Append .CreateParameter("@@outnum", adInteger, adParamOutput)
.Execute , , ADODB.adExecuteNoRecords
' sproc has been executed and the value 22 has been inserted
' not to set intInteger
intInteger = .Parameters("@@outnum")
 
Strictly speaking, a "procedure" never returns a value.
One that does is called a "function". Try declaring it a
function.
Also, variables passed "by value" by default - meaning they can't change the original variable. You must EXPLICITLY pass "by reference".
Use VB's HELP button for more details.
 
THOMASNG is correct speaking in a pure VB point of view, passing a variable by value does not change the value of the variable passed to a function unless:
myvar = myfunction(myvar)

The sproc is a SQL server stored procedure (it's what they call a stored PROCEDURE).
Thisone can return values (for errorhandling) and return values (called output parameters)
 
I quote him: "Also, variables passed "by value" by default".
 
Yes, correct VB passes variables by refrerence (default).
You have to provide byval if you want to pass by value.
 
I had similar problem with running store procedures with output parameters. You code was very helpfull. Thank you very much!!!

Nelly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top