Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So let's say my select statement returns the following

Id   Name
--   ----
1    Name1
2    Name2
3    Name3


and i have a stored procedure (called doLogic) that takes the Id and do some logic

what i want to do is to pass each Id returned from the select statement to the stored procedure, so i thought i should loop but i didn't know how to do it. i read things about cursor but couldn't do it.

What I have tried:

so basicly i select my table

select *from table
i should loop here
{
exec doLogic table.Id -- and pass each tableId inside
}
Posted
Updated 5-Aug-20 22:38pm
v2
Comments
[no name] 6-Aug-20 2:16am    
What do you plan to do with the results / return codes? Where are these "multiple" requests coming from? Do you know what a DDOS attack is?

1 solution

SQL is a set based language. Very very rarely do you need to use a loop.
Consider changing the stored procedure to get the data it requires by joining to the table instead.

If the "some logic" is not trying to update a table, just return a value, then create a Function instead and call it as part of the select e.g.
SQL
select [Id], [name], MyFunction([name]) as Result from [table]
I wrote an article some time ago about alternatives to loops, but it also includes some examples of loops if you absolutely have to use one - see Processing Loops in SQL Server[^]

EDIT after OP comment
---------------------
Quote:
what my stored procedure does
is inserting into two tables using the same Id and delete from 2 other tables using the same Id

SQL
create procedure doLogic
@Id int

Insert into table1(Id) values @Id
Insert into table2(Id) values @Id

delete from table3 where Id = @Id
delete from table4 where Id = @Id

Return 0


Here is some sample data that matches to the tables you have used in your examples
SQL
create table #table (id int identity(1,1), [name] varchar(50))
insert into #table ([name]) values
('name A'),('name B'),('name C'),('name D')

create table #table1 (id int)	
create table #table2 (id int)   

create table #table3 (id int)
insert into #table3 (id) values (3),(7) 

create table #table4 (id int)
insert into #table4 (id) values (1),(4),(8),(9) 
Using that sample data, in the stored procedure, I could replace
SQL
Insert into table1(Id) values @Id
Insert into table2(Id) values @Id
that does the insert one by one (or RBAR = Row By Agonising Row - can't remember who says that. I'll look it up later), with two lines that will do the entire table at once i.e.
SQL
insert into #table1 select [id] from #table 
insert into #table2 select [id] from #table
I can use a couple of alternative ideas for replacing
SQL
delete from table3 where Id = @Id
delete from table4 where Id = @Id
The first one is trivial and just deletes anything that matches ...
SQL
delete from #table3 where [id] in (select id from #table)
This second one is essentially exactly the same but I've included it to try to show that you can make the sub-query as complex as you like
SQL
delete from #table4 where [id] in (select #table4.id from #table inner join #table4 on #table.id = #table4.id)

Yous also mentioned
Quote:
and can u explain the function example you mentioned more? how to replace it with the select that returns all records? that i need to pass to the stored procedure?
A user-function cannot amend records it can only return values so it's not appropriate now that I know what you are trying to do in your stored procedure. Leave that for another time
 
Share this answer
 
v2
Comments
Member 14800672 6-Aug-20 5:29am    
Hi Chill,

what my stored procedure does
is inserting into two tables using the same Id and delete from 2 other tables using the same Id

create procedure doLogic
@Id int

Insert into table1(Id) values @Id
Insert into table2(Id) values @Id

delete from table3 where Id = @Id
delete from table4 where Id = @Id

Return 0


that's why i was thinking i should loop and insert each Id, what do u think is the best thing to do i my case?

and can u explain the function example you mentioned more? how to replace it with the select that returns all records? that i need to pass to the stored procedure?
CHill60 6-Aug-20 6:14am    
I've updated my solution
Member 14800672 6-Aug-20 10:34am    
Thanks alot Chill60, it was very helpful, would you mind looking at this?
it's some how similar
https://www.codeproject.com/Questions/5275815/How-to-insert-more-than-one-record-in-a-new-table
CHill60 6-Aug-20 10:50am    
I wasn't quick enough! :) Richard has already posted a solution for you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900