Solved

Get Record Count from SP called by an SP?

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 Std. License Key owner or vendor 4 56
SQL- GROUP BY 4 24
Need SSIS project 2 28
Access 2010 - Issue Viewing Records Added to a Linked SQL Table 13 48
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…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 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