WantsToLearn
Programmer
Hello All,
From where do you retrieve the Return value from a stored procedure in Sql Server? Thanks!
From where do you retrieve the Return value from a stored procedure in Sql Server? Thanks!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
cmd.Parameters.Add("@return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
CREATE PROC udp_Test
@MyValue
AS
Select *
From MyTable
Where = @MyValue
CREATE PROCEDURE dbo.sp_GetUserEmailTest
(
@UserID char(7),
@Email varchar(50) Out
)
AS
Select @Email = Email
From tbl_Users
Where UserID = @userID
return 1
GO
Public Const SQL_Connection As String = "Server=ServerPath;DataBase=Database;Trusted_Connection=Yes"
Function GetUserEmail()
Dim cnn As SqlConnection = New SqlConnection(SQL_Connection)
Dim cmd As SqlCommand = New SqlCommand
Dim result As String
Try
With cmd
.Connection = cnn 'Connect to the SQL Server
.CommandType = CommandType.StoredProcedure 'Make the command a stored procedure call
.CommandText = "sp_GetUserEmailTest" 'The name of the stored procedure
.Parameters.Add("@userID", SqlDbType.Char, 7) 'The parameter the query needs
.Parameters("@userID").Direction = ParameterDirection.Input
.Parameters("@userID").Value = gUser.ID 'Pass the users employee ID to the stored procedure
.Parameters.Add("@email", SqlDbType.VarChar, 50)
.Parameters("@email").Direction = ParameterDirection.Output
.Parameters.Add("@Return_Value", SqlDbType.Int)
.Parameters("@Return_Value").Direction = ParameterDirection.ReturnValue
Try
'Open the connection
cnn.Open()
'Run the command
.ExecuteReader()
'Close the connection as soon as possible
cnn.Close()
Catch ex As Exception
MessageBox.Show("Failed to connect to the database!" & ControlChars.CrLf & ex.Message)
End Try
MsgBox(.Parameters("@email").Value)
MsgBox(.Parameters("@return_value").Value)
End With
Catch ex As Exception
MessageBox.Show("Failed to get the email!" & ControlChars.CrLf & ex.Message)
End Try
End Function