Solved

Retrieving recordset from a stored procedure

Posted on 2002-06-11
9
175 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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