Solved

Get Record Count from SP called by an SP?

Posted on 2006-11-05
5
308 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

809 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