PC Review


Reply
Thread Tools Rate Thread

Cant call stored procedure which uses table variables?

 
 
Sunny
Guest
Posts: n/a
 
      10th Jan 2007
Hi,

I am using SQL Server 2000 and Access 2002. I am trying to use one of my
stored procedure in vba code. Following is the code and sql script.

VBA Code:
Sub DisplayClientId
Dim adoCommand As ADODB.Command
Dim rs As New ADODB.Recordset
Dim i As Integer
Set adoCommand = New Command
adoCommand.CommandText = "spGetClientId"
adoCommand.CommandType = adCmdStoredProc
Set adoCommand.ActiveConnection = cnObject -- Defined globally

Set rs = adoCommand.Execute

Set adoCommand = Nothing
i = 0
Do While Not rs.EOF
i = i + 1
MsgBox (i & " " & rs!ClientId)
rs.MoveNext
Loop
Set rs = Nothing
End Sub

StoredProcedure
CREATE PROCEDURE spGetClientId AS
DECLARE @tmpClients TABLE (ClientId char(5))
INSERT INTO @tmpClients
select ClientId from clients
select * from @tmpClients
GO

Above stored procedure does not return any record, and gives me error:
"Operation is not allowed when the object is closed"

But when I change stored procedure as follow, just works fine.

Alternate StoredProcedure
CREATE PROCEDURE spGetClientId AS
select ClientId from clients
GO

Both procedure works fine in query analyzer. I guess there is a problem
using table variable. Can anyone explain this behaviour? What is alternate
solution, I have to use intermidiate results to produce final results thats
why I have used table variable.

Thanks.


 
Reply With Quote
 
 
 
 
Sunny
Guest
Posts: n/a
 
      10th Jan 2007
Can anyone help me?
"Sunny" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I am using SQL Server 2000 and Access 2002. I am trying to use one of my
> stored procedure in vba code. Following is the code and sql script.
>
> VBA Code:
> Sub DisplayClientId
> Dim adoCommand As ADODB.Command
> Dim rs As New ADODB.Recordset
> Dim i As Integer
> Set adoCommand = New Command
> adoCommand.CommandText = "spGetClientId"
> adoCommand.CommandType = adCmdStoredProc
> Set adoCommand.ActiveConnection = cnObject -- Defined globally
>
> Set rs = adoCommand.Execute
>
> Set adoCommand = Nothing
> i = 0
> Do While Not rs.EOF
> i = i + 1
> MsgBox (i & " " & rs!ClientId)
> rs.MoveNext
> Loop
> Set rs = Nothing
> End Sub
>
> StoredProcedure
> CREATE PROCEDURE spGetClientId AS
> DECLARE @tmpClients TABLE (ClientId char(5))
> INSERT INTO @tmpClients
> select ClientId from clients
> select * from @tmpClients
> GO
>
> Above stored procedure does not return any record, and gives me error:
> "Operation is not allowed when the object is closed"
>
> But when I change stored procedure as follow, just works fine.
>
> Alternate StoredProcedure
> CREATE PROCEDURE spGetClientId AS
> select ClientId from clients
> GO
>
> Both procedure works fine in query analyzer. I guess there is a problem
> using table variable. Can anyone explain this behaviour? What is alternate
> solution, I have to use intermidiate results to produce final results
> thats why I have used table variable.
>
> Thanks.
>



 
Reply With Quote
 
Gary Walter
Guest
Posts: n/a
 
      10th Jan 2007
I have no experience with this error,
but did you try setting cmd to nothing
at end of sub?

Sub DisplayClientId
Dim adoCommand As ADODB.Command
Dim rs As ADODB.Recordset
Dim i As Integer

Set adoCommand = New ADODB.Command
adoCommand.CommandText = "spGetClientId"
adoCommand.CommandType = adCmdStoredProc
adoCommand.ActiveConnection = cnObject -- Defined globally

Set rs = adoCommand.Execute

i = 0
Do While Not rs.EOF
i = i + 1
MsgBox (i & " " & rs!ClientId)
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set adoCommand = Nothing

End Sub

Is there any reason you decided not to
use temp table in stored proc?

(untested)

CREATE PROCEDURE spGetClientId
AS
CREATE TABLE #tmpClients ( ClientId char(5))
INSERT INTO #tmpClients
select ClientId from clients
select * from #tmpClients
DROP #tmpClients
GO

"Sunny" wrote:
> I am using SQL Server 2000 and Access 2002. I am trying to use one of my
> stored procedure in vba code. Following is the code and sql script.
>
> VBA Code:
> Sub DisplayClientId
> Dim adoCommand As ADODB.Command
> Dim rs As New ADODB.Recordset
> Dim i As Integer
> Set adoCommand = New Command
> adoCommand.CommandText = "spGetClientId"
> adoCommand.CommandType = adCmdStoredProc
> Set adoCommand.ActiveConnection = cnObject -- Defined globally
>
> Set rs = adoCommand.Execute
>
> Set adoCommand = Nothing
> i = 0
> Do While Not rs.EOF
> i = i + 1
> MsgBox (i & " " & rs!ClientId)
> rs.MoveNext
> Loop
> Set rs = Nothing
> End Sub
>
> StoredProcedure
> CREATE PROCEDURE spGetClientId AS
> DECLARE @tmpClients TABLE (ClientId char(5))
> INSERT INTO @tmpClients
> select ClientId from clients
> select * from @tmpClients
> GO
>
> Above stored procedure does not return any record, and gives me error:
> "Operation is not allowed when the object is closed"
>
> But when I change stored procedure as follow, just works fine.
>
> Alternate StoredProcedure
> CREATE PROCEDURE spGetClientId AS
> select ClientId from clients
> GO
>
> Both procedure works fine in query analyzer. I guess there is a problem
> using table variable. Can anyone explain this behaviour? What is alternate
> solution, I have to use intermidiate results to produce final results
> thats why I have used table variable.
>
> Thanks.
>



 
Reply With Quote
 
Sunny
Guest
Posts: n/a
 
      10th Jan 2007
It workd after I added SET NOCOUNT ON in stored procedure.
"Gary Walter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have no experience with this error,
> but did you try setting cmd to nothing
> at end of sub?
>
> Sub DisplayClientId
> Dim adoCommand As ADODB.Command
> Dim rs As ADODB.Recordset
> Dim i As Integer
>
> Set adoCommand = New ADODB.Command
> adoCommand.CommandText = "spGetClientId"
> adoCommand.CommandType = adCmdStoredProc
> adoCommand.ActiveConnection = cnObject -- Defined globally
>
> Set rs = adoCommand.Execute
>
> i = 0
> Do While Not rs.EOF
> i = i + 1
> MsgBox (i & " " & rs!ClientId)
> rs.MoveNext
> Loop
>
> rs.Close
> Set rs = Nothing
> Set adoCommand = Nothing
>
> End Sub
>
> Is there any reason you decided not to
> use temp table in stored proc?
>
> (untested)
>
> CREATE PROCEDURE spGetClientId
> AS
> CREATE TABLE #tmpClients ( ClientId char(5))
> INSERT INTO #tmpClients
> select ClientId from clients
> select * from #tmpClients
> DROP #tmpClients
> GO
>
> "Sunny" wrote:
>> I am using SQL Server 2000 and Access 2002. I am trying to use one of my
>> stored procedure in vba code. Following is the code and sql script.
>>
>> VBA Code:
>> Sub DisplayClientId
>> Dim adoCommand As ADODB.Command
>> Dim rs As New ADODB.Recordset
>> Dim i As Integer
>> Set adoCommand = New Command
>> adoCommand.CommandText = "spGetClientId"
>> adoCommand.CommandType = adCmdStoredProc
>> Set adoCommand.ActiveConnection = cnObject -- Defined globally
>>
>> Set rs = adoCommand.Execute
>>
>> Set adoCommand = Nothing
>> i = 0
>> Do While Not rs.EOF
>> i = i + 1
>> MsgBox (i & " " & rs!ClientId)
>> rs.MoveNext
>> Loop
>> Set rs = Nothing
>> End Sub
>>
>> StoredProcedure
>> CREATE PROCEDURE spGetClientId AS
>> DECLARE @tmpClients TABLE (ClientId char(5))
>> INSERT INTO @tmpClients
>> select ClientId from clients
>> select * from @tmpClients
>> GO
>>
>> Above stored procedure does not return any record, and gives me error:
>> "Operation is not allowed when the object is closed"
>>
>> But when I change stored procedure as follow, just works fine.
>>
>> Alternate StoredProcedure
>> CREATE PROCEDURE spGetClientId AS
>> select ClientId from clients
>> GO
>>
>> Both procedure works fine in query analyzer. I guess there is a problem
>> using table variable. Can anyone explain this behaviour? What is
>> alternate solution, I have to use intermidiate results to produce final
>> results thats why I have used table variable.
>>
>> Thanks.
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call stored procedure with table parameter Jeremy Microsoft ADO .NET 2 6th May 2008 08:22 PM
Cant call stored procedure which uses table variables? Sunny Microsoft Access 5 11th Jan 2007 02:10 PM
Cant call stored procedure which uses table variables? Sunny Microsoft Access Queries 3 10th Jan 2007 11:09 PM
How 2 create table and seect records using stored procedure and call it in asp vedavyas.rao@gmail.com Microsoft Dot NET Framework Forms 1 9th Jan 2006 03:51 PM
Passing variables to stored procedure source =?Utf-8?B?Sm9lbA==?= Microsoft Access Reports 3 28th Apr 2005 10:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:35 AM.