SunQuest
           MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old March 25th, 2004, 04:01 PM
mark_thurston mark_thurston is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 16 mark_thurston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 5 sec
Reputation Power: 0
Loops inside a stored procedure

I need to know how to loop through the results of a select inside a stored procedure and execute inserts. The following is the pseudo code.

Code:
for each row in table t1 {
  select the ID from t1
  insert the ID into table t2
}

Thanks.

Last edited by mark_thurston : March 25th, 2004 at 04:03 PM. Reason: formatting

Reply With Quote
  #2  
Old March 25th, 2004, 04:57 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
You don't need a loop

Code:
  insert into t2(id) select id from t1

Reply With Quote
  #3  
Old March 26th, 2004, 10:09 AM
mark_thurston mark_thurston is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 16 mark_thurston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 5 sec
Reputation Power: 0
Loops inside a stored procedure

I guess that I over-simplified the code. In the loop I need to get a couple of fields from t1 and do a like command on them.

Code:
for each row in table t1 {
  select the t2.ID from t2 where t2.textValue like '%' + t1.textValue + '%'
  insert the t2.ID into table t3 
}


Sorry for the previous ambiguity.

Reply With Quote
  #4  
Old March 26th, 2004, 10:54 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
You don't need a loop

Code:
  insert into t3(id) select id from t2
   where exists (select * from t1
    where t2.textvalue like '%' + t1.textValue + '%')

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Loops inside a stored procedure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway