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
Thanks so much Dimant. I am just learning SQL Server and sometimes struggling to find obvious answers. [quoted text, click to view] "Uri Dimant" wrote: > 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 > > > > > > "NevilleT" <NevilleT@discussions.microsoft.com> wrote in message > news:5F951F57-F299-4DD1-B12E-7FE6BAFC371A@microsoft.com... > >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 > > > > > > > > > >
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] "NevilleT" <NevilleT@discussions.microsoft.com> wrote in message news:5F951F57-F299-4DD1-B12E-7FE6BAFC371A@microsoft.com... >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 > > > >
Thanks Hugo. There is only one record in the table so it should not be a problem. [quoted text, click to view] "Hugo Kornelis" wrote: > On Mon, 27 Nov 2006 22:10:01 -0800, NevilleT wrote: > > >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'. > > 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. > > >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: > (snip) > > I'm far from a VB expert, but at first glance your code looks okay. > > -- > Hugo Kornelis, SQL Server MVP
[quoted text, click to view] On Mon, 27 Nov 2006 22:10:01 -0800, NevilleT wrote: >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'.
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] >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:
(snip) I'm far from a VB expert, but at first glance your code looks okay. --
[quoted text, click to view] On Tue, 28 Nov 2006 16:31:37 -0800, NevilleT wrote: >Thanks Hugo. There is only one record in the table so it should not be a >problem.
Hi Neville, In that case, why use TOP at all? --
Don't see what you're looking for? Try a search.
|