0

I'm a newbie in sql i just wanna know if this is possible? If yes, can someone help me please.

Here is the scenario:

I have two tables named Table A and Table B as shown in image link below.

Table A has myId and desc columns. Table B has myId and other columns.

image here

Here is the catch, I wanted to insert (not update assuming the scenario in image link that table B is empty though) the highlighted myId in table A to table B.

5
  • 1
    I don't see what table A has to do with the question. Are you choosing "2" through some special logic? Commented Aug 31, 2018 at 1:17
  • I think this is a simple insert B(myid) select myid from A. But you will need some more details on what you want the result to look like, and where your other columns are coming from. Also, include the tables in your question not as an image. Ideally a create table followed by inserts will make it super easy for people to answer. Commented Aug 31, 2018 at 1:19
  • no, what I mean on that just to show the "assumed result" if that is possible if the value in table A highlighted can be inserted in table B. Sorry if it makes you confused. Commented Aug 31, 2018 at 1:21
  • The ID can be easily inserted. Whats not clear is why you have 4 rows. Whats the logic here. The answer I see below assumes you want exactly 4 rows, is this correct? Commented Aug 31, 2018 at 1:27
  • I'll update this. tomC Commented Aug 31, 2018 at 1:30

2 Answers 2

1

From what you describe, you can use insert . . . select. I don't see what A has to do with the question.

insert into b (myId, etc1, etc2, etc3)
     select 2, 'some data here', 'some data here', 'some data here'
     union all
     select 2, 'some data here', 'some data here', 'some data here'
     union all
     select 2, 'some data here', 'some data here', 'some data here';

This assumes that thisTableId is auto-incremented.

You can get the data from a table just as easily:

insert into b (myId, etc1, etc2, etc3)
     select a.myid, 'some data here', 'some data here', 'some data here'
     from a
     where a.myid = 2
     union all
     select a.myid, 'some data here', 'some data here', 'some data here'
     from a
     where a.myid = 2
     union all
     select a.myid, 'some data here', 'some data here', 'some data here'
     from a
     where a.myid = 2;
Sign up to request clarification or add additional context in comments.

1 Comment

It has some significant reason in A since the goal is to get the value in A which will be inserted in B. Is this attainable using loop in sql? Yeah thisTableId is auto-incremented.
0

This shows this in a loop, assuming the MYID is actually somehow being selected from TableA. But it answers the question about loops.

;WITH n AS (
    SELECT 1 as n
    UNION ALL
    SELECT 1 + n as n from n WHERE n < 4 
)
insert into b (myId, etc1, etc2, etc3)
select myid, 'some data here', 'some data here', 'some data here'
from TableA where myID=2
cross join n

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.