简体   繁体   中英

How to insert multiple values from one table to another?

Need a small help in creating an oracle query.

I have a table in which I want to insert data from another table.

I get the account numbers from this table by using query:

select AccountNumbers from Company;

It returns me,

1234
2345
3456
4567
5678

Now I want to insert this data in another table.

Insert into tempTable (id, group, accountNumber) values (seq_mytemp_sequence, 'A', (select AccountNumbers from Company) );

I have created the sequence which works fine. But it fails saying that the sub query returns more than one row which is obvious.

I want the data in temp table to be inserted in such way that that it will be like this,

1 A 1234
2 A 2345
3 A 3456
4 A 4567
5 A 5678

I tried using loop, but its difficult to fetch individual account as the rownum doesnt work for anything other than 1.

Can someone guide me about how to do this? Your help appreciated.

Shouldn't it be like:

Insert into tempTable (id, group, accountNumber) 
select seq_mytemp_sequence, 'A', AccountNumbers from Company ;

And not like:

Insert into tempTable (id, group, accountNumber) 
values (seq_mytemp_sequence, 'A', (select AccountNumbers from Company) );

You need to use INSERT INTO ... SELECT and when you want a number from a sequence you need .NEXTVAL :

INSERT INTO tempTable (id, "group", accountNumber)
SELECT SEQ_MYTEMP_SEQUENCE.NEXTVAL,
       'A',
       AccountNumbers
FROM   Company

(Also, please do not give columns names which are key words like GROUP as you will have to wrap them in double quotes and then Oracle will enforce case sensitivity on the column name)

插入表名(Productid,“名称”,帐号),从Producttable中选择ProductId,“ ABC”,“ XYZ”

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