Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving recordset from a stored procedure

Posted on 2002-06-11
9
Medium Priority
?
180 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 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

721 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