Solved

Get Record Count from SP called by an SP?

Posted on 2006-11-05
5
298 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
5 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 300 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 100 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 100 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now