Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Row Counts inside a Stored Proc?

Author  Topic 

label
Posting Yak Master

197 Posts

Posted - 2003-12-03 : 09:52:01
I've got the following stored procedure:


CREATE PROCEDURE dbo.zz_populate_email_endings AS

Begin

Declare @company varchar(100),
@coID int,
@results int,
@emailending varchar(100)

Declare Cursid Cursor for
Select company_id from sales_company order by company_id

Create Table #temp1 (Ending Varchar(200), Company varchar(200), EmailCount int)

open Cursid
fetch next from cursid into @coID
while (@@fetch_status<>-1)
Begin

select @company=company_name from sales_company where company_id=@coID

Insert into #temp1
select
substring(email, charindex('@', email), len(email)+1-charindex('@', email)) as [EmailEnding],
max(isnull(company_name, 'n/a')),
count(substring(email, charindex('@', email), len(email)+1-charindex('@', email))) as EmailTotal
from
smc_new_products.dbo.usr_smc
where
company_name like @company+'%'
or
email like '@' + @company + '%'
group by substring(email, charindex('@', email), len(email)+1-charindex('@', email))
order by emailtotal desc

--Find out if there are 0 results, 1 result, or many results here
--?

if @results=0
begin
update sales_company
set multiple_hits=0
where company_id=@coID
end

if @results=1
begin
update sales_company
set multiple_hits=0
where company_id=@coID

exec ap_link_email_company @company, @emailending
end

if @results>1
begin
update sales_company
set multiple_hits=1
where company_id=@coID
end

--reset stuff
truncate table #temp1

fetch next from cursid into @coID
End

deallocate Cursid

Print 'complete'

End
GO


I have two quick (and hopefully easy to answer) questions:

1. How do I determine how many rows I have in my #temp1 table?
2. I think I've used the "exec" command correctly to kick off another stored proc in my "if @results=1 " section....haven't I?

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-03 : 10:56:37
After the INSERT

DECLARE @Results int, @Error int
SELECT @Results = @@ROWCOUNT, @Error = @@ERROR


Check @Results for # of rows, and makes sure to check @@Error to make sure its equal to 0...otherwise you need to error handle...

The Conditional statements look good...nice and clean...fall through logic...no condition can be entered if another wa entered...very clean...




Brett

8-)
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2003-12-03 : 11:36:17
quote:
Originally posted by X002548

DECLARE @Results int, @Error int
SELECT @Results = @@ROWCOUNT, @Error = @@ERROR




Thanks, that worked perfectly!

Go to Top of Page
   

- Advertisement -