4

I have a 2 table structure:

Table A:

RID; Name; LP
E    F     1
E    F     2
E    F     3
E    F     12
E    F     152

Table B:

LP
1
2
3
12
152
...
156
157
180
itd.

Query something like:

Insert into TableA(RID, Name, LP) Values ('E', 'F', Select LP from TableB)

I want to achive:

RID; Name; LP
E    F     1
E    F     2
E    F     3
E    F     12
E    F     152
E    F     156
E    F     157
E    F     180
etc.
1
  • Which database - sql-server or postgresql? Commented Dec 29, 2011 at 13:09

4 Answers 4

10

You're close. Try

INSERT INTO TableA(RID, Name, LP)
SELECT 'E', 'F', LP FROM TableB
-- Omit this where clause, if duplicate LP's are OK
WHERE TableB.LP NOT IN (SELECT LP FROM TableA)
Sign up to request clarification or add additional context in comments.

4 Comments

Can you do this in JOOQ?
@Friso: Of course! Feel free to ask a new question about it and I'll answer
@Lucas Eder, after a lot of puzzling I figured it out. Might post something anyway if I find the time as it maybe helpful to others. Thank for the reply (and your great work!)
@Friso: Sure! It's common practice here on Stack Overflow to answer your own questions to help future visitors. Looking forward!
1
Insert into TableA(RID, Name, LP) 
Select 
   'E', 'F', LP 
 from 
   TableB

Comments

0

The "quick and dirty" solution i came up with is :

Insert into TableA(RID, Name, LP) Values ('E', 'F', Select LP from TableB where rowid = 1)

Insert into TableA(RID, Name, LP) Values ('E', 'F', Select LP from TableB where rowid = 2)

...

and so on...

1 Comment

That is really "quick and dirty" ;-)
0

select * into [NEW_TABLE] from [OLD_TABLE]

No need to create new table. It'll be created automatically.

Enjoy.

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.