简体   繁体   中英

SQL Query to insert values from one table into another

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.

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)
Insert into TableA(RID, Name, LP) 
Select 
   'E', 'F', LP 
 from 
   TableB

select * into [NEW_TABLE] from [OLD_TABLE]

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

Enjoy.

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...

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