[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Get Record Count from SP called by an SP?

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
alivemedia
Asked:
alivemedia
3 Solutions
 
NightmanCTOCommented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
imran_fastCommented:
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
 
Anthony PerkinsCommented:
Post the contents of the SearchCategoryRelations & SearchSite stored procedures and we can suggest solutions.
0
 
satish_nagdevCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now