简体   繁体   中英

sql server (insert row for each result nested)

I got 3 different tables on sql server (attrib,att_art, art). 'attrib' has a list of attributes, 'art' has a list of items and att_art should join both, like this:

ATTRIB
att1
att2
att3
...

ART
ar1
ar2
ar3
...

Both tables have an unique id that joins on att_art on id_att and id_ar

This is what I need to do:

For each item on art and for each attrib at the same time, I need to insert a new row on att_art. Like this:

att1   ar1
att2   ar1
att3   ar1
att4   ar2
att5   ar2
att6   ar2
....

How could I do this? i'm kinda new on sql and it's been overwhelming!

Use Cross apply.............

Select * from ATTRIB a
cross apply (select * from ART) b

The most typical way would be cross join with insert :

insert into att_art(attrib, art)
    select attrib.attrib, art.item
    from attrib cross join
         art;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM