Solved

Retrieving recordset from a stored procedure

Posted on 2002-06-11
9
172 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now