?
Solved

Get Record Count from SP called by an SP?

Posted on 2006-11-05
5
Medium Priority
?
313 Views
Last Modified: 2012-05-05
I have a sp that calls 2 differnt sp's based on a variable passed to it.

I want to check to see how many records are returned in the called sp, so if one is empty I can call the other as a backup.

Currently here is my sp that calls the other sp's

ALTER PROCEDURE [dbo].[MasterSearch]
      @SearchType AS NVARCHAR(25),
      @SearchTerms AS NVARCHAR(100),
      @Latitude FLOAT,      
      @Longitude FLOAT
AS
BEGIN
      SET NOCOUNT ON;
IF (@SearchType='Category')
      BEGIN
            EXEC SearchCategoryRelations @SearchTerms, @Latitude, @Longitude
      END
IF (@SearchType='Business')
      BEGIN
            EXEC SearchSite @SearchTerms, @Latitude, @Longitude
      END

      INSERT INTO Searchs(SearchTerms,SearchType) VALUES (@SearchTerms,@SearchType)
END
0
Comment
Question by:alivemedia
[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 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 1200 total points
ID: 17876342
You can do that.

SearchCategoryRelations  and SearchSite would need to define @SearchTerms (which I assume is the count that you want) as an output parameter. I have used count here instead (thought you might be using the nvarchar value for something else)
e.g.
Create PROCEDURE [dbo].[SearchCategoryRelations ]
     @Count AS int output,
     @SearchTerms AS NVARCHAR(100),
     @Latitude FLOAT,    
     @Longitude FLOAT
etc...

And internally you set @Count to the count that you are executing.

to call it from the other proc:
declare @Count int
EXEC SearchSite @Count output,@SearchTerms, @Latitude, @Longitude
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17876344
You need to modify ur inner sps so that they will return the no of rows affected as output parameter, otherwise you need to insert the data from the sp into a temp table and take the count but this is not a good way, use the first option
0
 
LVL 28

Assisted Solution

by:imran_fast
imran_fast earned 400 total points
ID: 17876348
You have to include an out put parameter for your first sp something like

create procedure SearchCategoryRelations @SearchTermsAS NVARCHAR(100), @Latitude float, @Longitude float, @Rec int output

begin

/*your code *
select  @Rec = count(*)  from table
return @rec
end

now in orignal procedure


ALTER PROCEDURE [dbo].[MasterSearch]
     @SearchType AS NVARCHAR(25),
     @SearchTerms AS NVARCHAR(100),
     @Latitude FLOAT,    
     @Longitude FLOAT
AS
BEGIN
declare @rec int
     SET NOCOUNT ON;
IF (@SearchType='Category')
     BEGIN
          EXEC SearchCategoryRelations @SearchTerms, @Latitude, @Longitude, @rec output
     END
IF (@SearchType='Business' or isnull(@rec,0) = 0 )
     BEGIN
          EXEC SearchSite @SearchTerms, @Latitude, @Longitude
     END

     INSERT INTO Searchs(SearchTerms,SearchType) VALUES (@SearchTerms,@SearchType)
END
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17876589
Post the contents of the SearchCategoryRelations & SearchSite stored procedures and we can suggest solutions.
0
 
LVL 4

Assisted Solution

by:satish_nagdev
satish_nagdev earned 400 total points
ID: 17886633
hi,
as i can see you have set NOCOUNT on could you tell why so? other wise you could have used @@RowCount to check if the called query or whatever returned/affected  records or not.

regards,
satish,
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

752 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