Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 21st, 2005, 01:42 PM
Registered User
Join Date: Dec 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems returning count in Stored Procedure

I have a stored procedure that loops through various tables to get the counts of listings in certain areas. The first part works fine. The second part always returns 0 for the count. I have the SP writing out the select count SQL statement, and when I run that in qry analyzer, it returns the correct count. What do I have to do/change to make the second part of my SP return te correct count?

Any help will be appreciated. The problem is in the block IF @type=A on the line where I set @count
I have marked this line below.

Thanks in advance,

Code below:

CREATE PROCEDURE dbo.sp_CompanyAreaCount


DECLARE @area varchar(50)
DECLARE @count int
DECLARE @type char(1)
DECLARE @mlsAreas varchar(200)
DECLARE @tempAreas varchar(200)

SELECT AreaName, AreaType FROM CompanyArea


FETCH NEXT FROM cl into @area, @type


    IF @type = 'C'
            SET @count = (Select Count(*) as listingCount FROM listing WHERE City = @area)
             UPDATE CompanyAreaTest SET Listings = @count WHERE AreaName = @area AND AreaType = 'C'
    IF @type = 'A'
            SET @mlsAreas = ''
            DECLARE cm CURSOR FOR
            SELECT IsNull(MLSArea, -1) FROM CompanyAreaDetail WHERE AreaName = @area
            OPEN cm
            FETCH NEXT FROM cm into @tempAreas
            WHILE @@FETCH_STATUS = 0
                set @mlsAreas = @mlsAreas + @tempAreas + ','
                FETCH NEXT FROM cm INTO @tempAreas
        set @mlsAreas = @mlsAreas + '-1'
  Problem here --> SET @count = (Select Count(*) as listingCount FROM dbo.listing WHERE mapArea1 IN (@mlsAreas) AND mapArea1 <> '')
        UPDATE CompanyAreaTest SET Listings = @count WHERE AreaName = @area AND AreaType = 'A'
        print @area
        print @mlsAreas
        print @count
        print "Select Count(*) as ListingCount FROM listing WHERE mapArea1 IN (" + @mlsAreas + ") AND MapArea1 <> ''"
             CLOSE cm
             DEALLOCATE cm

        FETCH NEXT FROM cl INTO @area, @type

Reply With Quote
  #2 (permalink)  
Old December 21st, 2005, 03:24 PM
Registered User
Join Date: Dec 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

I figured it out. I had to remove the 'AS ListingCount' from both queries.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning a value (stored procedure scandalous ASP.NET 2.0 Basics 9 November 7th, 2007 03:41 PM
Stored Procedure Problems Tarcash ASP.NET 2.0 Basics 10 June 20th, 2007 12:46 AM
Count character value using Stored Procedure itsurshailesh SQL Language 1 November 14th, 2006 01:31 AM
Returning Values from a Stored Procedure kadjw SQL Server ASP 1 September 13th, 2006 12:08 PM
Returning Stored Procedure Results from ASP page JennaAckerson Classic ASP Basics 0 December 23rd, 2005 11:44 AM

All times are GMT -4. The time now is 04:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.