all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

Output parameter



Output parameter NevilleT
11/27/2006 10:10:01 PM
sql server programming: I have an Access Project that is using a SQL Server 2000 database. I want to
create a function in Access to read a table called tblDisplayOptions that
only has one record. It is used for configuration. One of the fields is
called CompanyName. I want to retrieve the company name and use it on
various screens and reports. The stored procedure is:

CREATE PROCEDURE qryCompanyName
@Company nvarchar OUTPUT
AS
SELECT @Company = Top 1 CompanyName
FROM dbo.tblDisplayOptions
GO

The proc fails with error

Server: Msg 156, Level 15, State 1, Procedure qryCompanyName, Line 5
Incorrect syntax near the keyword 'Top'.
Server: Msg 2812, Level 16, State 62, Line 5
Could not find stored procedure 'qryCompanyName'.

The second part of the question is, assuming the proc works, how do I get
the parameter in VBA? I set up a parameter in VBA and ran the stored proc
but not sure how I recover the value into a string variable to use elsewhere.
The relevant part of the code is:

Dim cmd As ADODB.Command
Dim prmCompany As ADODB.Parameter

Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CurrentProject.Connection ' Use
current connection
.CommandText = "qryCompanyName" ' Name of the stored procedure
.CommandType = adCmdStoredProc

Set prmCompany = cmd.CreateParameter(Name:="Company", _
Type:=adParamReturnValue, _
Direction:=adParamOutput) ' Create a
prameter
.Parameters.Append prmCompany

.Execute ' Execute the
command

Set cmd = Nothing
End With



Re: Output parameter NevilleT
11/27/2006 11:27:02 PM
Thanks so much Dimant. I am just learning SQL Server and sometimes
struggling to find obvious answers.

[quoted text, click to view]
Re: Output parameter Uri Dimant
11/28/2006 12:00:00 AM
Hi

CREATE PROCEDURE qryCompanyName
@Company nvarchar OUTPUT
AS
SELECT Top 1 @Company = CompanyName
FROM dbo.tblDisplayOptions
GO
-- Declare the variable to receive the output value of the procedure.
DECLARE @Company_out nvarchar

-- Execute the procedure and save the output value in a variable.

EXECUTE qryCompanyName
@Company = @Company_out OUTPUT

SELECT @Company_out





[quoted text, click to view]

Re: Output parameter NevilleT
11/28/2006 4:31:37 PM
Thanks Hugo. There is only one record in the table so it should not be a
problem.

[quoted text, click to view]
Re: Output parameter Hugo Kornelis
11/28/2006 11:50:59 PM
[quoted text, click to view]

Hi Neville,

In addition to Uri's answer - are you aware that the result of a query
that uses TOP without ORDER BY is undefined? You could get a different
result each time you run the query, as TOP will return the first row
encountered in what happens to be the best order of accessing the table
at that moment.

[quoted text, click to view]
(snip)

I'm far from a VB expert, but at first glance your code looks okay.

--
Re: Output parameter Hugo Kornelis
11/30/2006 12:08:47 AM
[quoted text, click to view]

Hi Neville,

In that case, why use TOP at all?

--
AddThis Social Bookmark Button