0

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!

1
  • How will the code know which atts to associate with which arts? If it's all with all, that okay (but a but weird) ... otherwise we'd need another source of data. If you're just after an efficient way to input them "manually" we can help with that too. Commented Jun 2, 2017 at 11:13

2 Answers 2

1

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

Select * from ATTRIB a
cross apply (select * from ART) b
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.