Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retrieving recordset from a stored procedure

Posted on 2002-06-11
9
Medium Priority
?
183 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 200 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 2

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

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 2

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 2

Author Comment

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

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

926 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