SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to loop and insert records


how to loop and insert records

Author
Message
Patrick.I
Patrick.I
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 262
i have 3 tables like so below:

table -A
pid mid
--- ----
1 7
2 29

table -B
sid pid mid
--- --- ---
1 1 7
2 1 7
3 2 29

table -C
sid toy
--- ---
1 car
2 ball
3 toy phone

what i want is to loop through table A and C and add values sid,pid and mid into table -B
column sid,pid and mid in table -B isn't auto incremented

Any ideas and thanks in advance
SQLZ
SQLZ
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3114 Visits: 940
Hi Patrick,

I'm not sure you'd need to use a loop. Would a cross join not work?


select sid, pid, mid
from testA, TestC



Karl
source control for SQL Server
Todd Engen
Todd Engen
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2805 Visits: 6336
In your example, what values did you use from table A and table C to generate table B?
shamshudheen
shamshudheen
SSC-Addicted
SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)SSC-Addicted (474 reputation)

Group: General Forum Members
Points: 474 Visits: 211
Patrick Ige (4/1/2008)
i have 3 tables like so below:

table -A
pid mid
--- ----
1 7
2 29

table -B
sid pid mid
--- --- ---
1 1 7
2 1 7
3 2 29

table -C
sid toy
--- ---
1 car
2 ball
3 toy phone

what i want is to loop through table A and C and add values sid,pid and mid into table -B
column sid,pid and mid in table -B isn't auto incremented

Any ideas and thanks in advance


Patrick,

what is the relation between A and C table

if you do cross join you will get cortesan product records. check your resulted table

shamsudheen
Patrick.I
Patrick.I
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 262
thanks guys all fixed using cross joins
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search