Count of Records

Hi, I have a stored proc that will accept zipcode and radius as parameters
and return list of zipcodes based on the params.

I am inserting the results into a different table. I have couple of questions,

How do I loop through the records from above and insert into another table
How do I get a count of records thrown by the query.

If the count is >0 I want to insert into another table
rowmarkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Normally with a stored procedure it is just returning rows... you can insert them into a table using the insert command, but you cannot do a 'where' clause... You could do the "where" part in your stored procedure...


so, to insert have a look at the sample below...

The stored procedure is simply returning a pile of rows and storeing it into a table.
-- step 1 create a stored procedure for our example
 
create procedure usp_Get_ZipCode (@zip varchar(20))
as
begin
 
  select 'zip 1' as zipcode, 'suburb 1' as suburb, 1 as number_suburbs union all
  select 'zip 2' as zipcode, 'suburb 2' as suburb, 2 as number_suburbs union all
  select 'zip 3' as zipcode, 'suburb 3' as suburb, 3 as number_suburbs union all
  select 'zip 4' as zipcode, 'suburb 4' as suburb, 2 as number_suburbs union all
  select 'zip 5' as zipcode, 'suburb 5' as suburb, 1 as number_suburbs union all
  select 'zip 6' as zipcode, 'suburb 6' as suburb, 3 as number_suburbs 
 
end
go
 
-- step 2 create a table to receive data - note it must match at least the output of the stored procedure
 
create table #my_zipcodes(id int identity, zipcode varchar(60), suburb varchar(60), burroughs int)
go
 
-- step 3 store results of stored procedure into the table
 
insert #my_zipcodes (zipcode, suburb, burroughs)
exec usp_get_zipcode 'any'
go
 
select * from #my_zipcodes

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
Oh, and then to insert into another table, just use the insert command with a select :

insert my_other_table (< column list >)
select <column list>
from #my_zipcodes
where <conditions>


if there is nothing in #my_zipcodes that satisfy the <conditions> then it is a empty set and nothing is added.

if there is possibility of duplicate rows that might already exists then would have to extend those <conditions> with :

insert my_other_table (< column list >)
select <column list>
from #my_zipcodes Z
where <conditions>
AND not exists (select NULL from my_othertable O where O.zipcode = Z.zipcode and O.suburb = Z.suburb)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.