Solved

Retrieving recordset from a stored procedure

Posted on 2002-06-11
9
177 Views
Last Modified: 2010-05-02
I am using VB 6 and SQL 7. I am trying to execute a stored procedure using ADO. This stored procedure creates a temptable and places values in it and then returns a record. When I execute this stored procedure in Query Analyzer, it works fine. But I can't get the results using ADO the way I currently have it written. Help! This is the way I currently am writing the code:

strSQL = "exec xxx.dbo.sp_GetValue 2, 'xxx.dbo.people'"
  rsAssignedInfo.Open strSQL, cnIssueDatabase, adOpenStatic

msgbox(rsAssignedInfo("issueid"))


Lisa
0
Comment
Question by:lmred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7072248
lmred: There are two solutions to this, the basic reason is that because you have multiple steps in your stored procedure, SQL server is returning multiple recordsets, some of which are actually the messages that you see displayed when you run this in Query Analyzer. The options that you have are this:

1) Use SET NOCOUNT ON .. StoredProcedureCode .. SET NOCOUNT OFF to suppress the messages and only return the recordset that contains the data you want.

2) Use Set rsAssignedInfo = rsAssignedInfo.NextRecordset (perhaps more than once) until you get to the recordset that contains your data. As this is going to be the same every time you just need to find out how many closed recordsets are returned before the one with your data and use .NextRecordset method enough times to get to the correct recordset.
0
 
LVL 1

Author Comment

by:lmred
ID: 7072756
As advised, I added set nocount on and set nocount off, but I am still not receiving my results.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7072766
Can we see the stored procedure?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:lmred
ID: 7072780
Maybe it is where I am placing the set nocount on. Where do I place it in the stored procedure. Before declarations?
0
 
LVL 1

Author Comment

by:lmred
ID: 7072790
CREATE PROCEDURE [dbo].[sp_GetAssignedTo]

@IssueID int, @DBName varchar(200) AS

IF OBJECT_ID('tempdb..#rpt') IS NOT NULL DROP table #rpt

DECLARE @AssignedToName varchar(200)
DECLARE @AssignedTo varchar(200), @AssignedToDESC varchar(200), @separator INT, @key INT, @sql varchar(5000)
DECLARE @RetVal int

--select * from issueinfo
--set @IssueID = 348
--set @dbName = 'ErpIssues.docsadm.people'
SET NOCOUNT ON
go
select I.IssueID,I.AssignedTo as AssignedTo, @AssignedToDESC as AssignedToDESC
INTO #rpt
from Issueinfo I
where I.IssueID = @IssueID
--select * from #rpt
--Join ConnectorAssignedTo A on A.Issueid = I.IssueID
Declare csrUsers Cursor
For Select IssueId, AssignedTo, AssignedToDESC
from #rpt
For Update of AssignedToDESC
Open csrUsers
Fetch Next From csrUsers Into @IssueID, @AssignedTo, @AssignedToDESC
While @@Fetch_Status <> -1
BEGIN
--print @AssignedTo
 SET @AssignedTo = @AssignedTo + ';' --make last value get decoded
  SET @AssignedToDESC = ''
     IF @AssignedTo is not null BEGIN
          WHILE CHARINDEX(';', @AssignedTo) > 0  
          BEGIN
               SET @separator = CHARINDEX(';', @AssignedTo)--get character index number
               SET @key = CAST(LEFT(@AssignedTo, @separator - 1) AS INT)  --find position of first separat0r
               --get user fullname
               SET @AssignedToName = ''
               print 'EXEC'
               EXEC @RetVal=sp_GetFullName @DBName, @Key, @AssignedToName OUTPUT
               --print 'here'
               --print @AssignedToName
               --PRINT @AssignedToDESC
               print @retval
               If @RETVAL = 0 BEGIN
               SET @AssignedToDESC = @AssignedToDESC +
                 (SELECT CASE WHEN @AssignedToDESC IS NULL THEN ''
                              ELSE ';' + @AssignedToName  END)
               End
               SET @AssignedTo = SUBSTRING(@AssignedTo, @separator + 1, LEN(@AssignedTo) - @separator)
               print @assignedtoDESC
              END
     
     END
     SET @AssignedToDESC= SUBSTRING(@AssignedToDESC, 2,Len(@AssignedToDESC))  
UPDATE #rpt
  SET AssignedToDesc = @AssignedToDesc
  WHERE CURRENT OF csrUsers
  FETCH NEXT FROM csrUsers INTO @IssueID, @AssignedTo, @AssignedToDESC  
END --WHILE
CLOSE csrUsers
DEALLOCATE csrUsers

SELECT * FROM #rpt
go
SET NOCOUNT OFF
GO
0
 
LVL 1

Author Comment

by:lmred
ID: 7072831
its working now....thanks!
0
 
LVL 1

Author Comment

by:lmred
ID: 7072838
...and it was your code that helped. Thanks Tim!

Lisa
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7072849
Ok, try it like this instead:

CREATE PROCEDURE [dbo].[sp_GetAssignedTo]

@IssueID int, @DBName varchar(200) AS

IF OBJECT_ID('tempdb..#rpt') IS NOT NULL DROP table #rpt

DECLARE @AssignedToName varchar(200)
DECLARE @AssignedTo varchar(200), @AssignedToDESC varchar(200), @separator INT, @key INT, @sql varchar(5000)
DECLARE @RetVal int

--select * from issueinfo
--set @IssueID = 348
--set @dbName = 'ErpIssues.docsadm.people'
SET NOCOUNT ON
select I.IssueID,I.AssignedTo as AssignedTo, @AssignedToDESC as AssignedToDESC
INTO #rpt
from Issueinfo I
where I.IssueID = @IssueID
--select * from #rpt
--Join ConnectorAssignedTo A on A.Issueid = I.IssueID
Declare csrUsers Cursor
For Select IssueId, AssignedTo, AssignedToDESC
from #rpt
For Update of AssignedToDESC
Open csrUsers
Fetch Next From csrUsers Into @IssueID, @AssignedTo, @AssignedToDESC
While @@Fetch_Status <> -1
BEGIN
--print @AssignedTo
SET @AssignedTo = @AssignedTo + ';' --make last value get decoded
 SET @AssignedToDESC = ''
    IF @AssignedTo is not null BEGIN
         WHILE CHARINDEX(';', @AssignedTo) > 0  
         BEGIN
              SET @separator = CHARINDEX(';', @AssignedTo)--get character index number
              SET @key = CAST(LEFT(@AssignedTo, @separator - 1) AS INT)  --find position of first separat0r
              --get user fullname
              SET @AssignedToName = ''
              print 'EXEC'
              EXEC @RetVal=sp_GetFullName @DBName, @Key, @AssignedToName OUTPUT
              --print 'here'
              --print @AssignedToName
              --PRINT @AssignedToDESC
              print @retval
              If @RETVAL = 0 BEGIN
              SET @AssignedToDESC = @AssignedToDESC +
                (SELECT CASE WHEN @AssignedToDESC IS NULL THEN ''
                             ELSE ';' + @AssignedToName  END)
              End
              SET @AssignedTo = SUBSTRING(@AssignedTo, @separator + 1, LEN(@AssignedTo) - @separator)
              print @assignedtoDESC
             END
   
    END
    SET @AssignedToDESC= SUBSTRING(@AssignedToDESC, 2,Len(@AssignedToDESC))  
UPDATE #rpt
 SET AssignedToDesc = @AssignedToDesc
 WHERE CURRENT OF csrUsers
 FETCH NEXT FROM csrUsers INTO @IssueID, @AssignedTo, @AssignedToDESC  
END --WHILE
CLOSE csrUsers
DEALLOCATE csrUsers

SELECT * FROM #rpt
/* This is optional but you can actually drop the temporary table here */
Drop Table #rpt
SET NOCOUNT OFF
GO

The extra Go statements will confuse the issue, it is better to leave them out.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7072854
No problem Lisa, glad to have been of help.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question