473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Insert into a table with sequence and a value from other table

8
Hi,

I have a table TARGET whose structure is

seq_no number -- should be populated with sequence SEQ_TAR
fsa varchar2(10)

Table SOURCE structure is
-------------------------
fsa varchar2(10)

my requirement is
i have to insert into the TARGET table the fsa value from
SOURCE table along with the sequence number using sequence SEQ_TAR.nextval.
Jan 11 '08 #1
3 20964
Dave44
153 100+
Hi,

I have a table TARGET whose structure is

seq_no number -- should be populated with sequence SEQ_TAR
fsa varchar2(10)

Table SOURCE structure is
-------------------------
fsa varchar2(10)

my requirement is
i have to insert into the TARGET table the fsa value from
SOURCE table along with the sequence number using sequence SEQ_TAR.nextval.
i fear this may be a school assignment and so i dont want to do your work for you as you will not learn anything that way.

it sounds like you just want to do an insert into a table rows from another table + the sequence. you have written the code to get the sequence value so just put the 2 together. insert into ... select .... If there are restrictions to which rows you want to insert then you will need to add a where clause to the select.
Jan 11 '08 #2
hgriva
8
i fear this may be a school assignment and so i dont want to do your work for you as you will not learn anything that way.

it sounds like you just want to do an insert into a table rows from another table + the sequence. you have written the code to get the sequence value so just put the 2 together. insert into ... select .... If there are restrictions to which rows you want to insert then you will need to add a where clause to the select.
Sorry i missed out a major part
So i'm once again giving the scenario


I have a table TARGET whose structure is

seq_no number -- should be populated with sequence SEQ_TAR
fsa varchar2(10)
rec_ct number

Table SOURCE structure is
-------------------------
fsa varchar2(10)
sal number

my requirement is
i have to insert into the TARGET table the fsa value from
SOURCE table along with the sequence number using sequence SEQ_TAR.nextval.

like
Expand|Select|Wrap|Line Numbers
  1.  
  2. insert into
  3. target(seq_no,fsa,rec_ct)
  4. select
  5. (SEQ_TAR.nextval,
  6.  fsa,
  7.  sum(sal)
  8.  from source
  9.  group by fsa
  10.  )
  11.  

this is giving me a error.
Kindly help.
Jan 11 '08 #3
Dave44
153 100+
Sorry i missed out a major part
So i'm once again giving the scenario


I have a table TARGET whose structure is

seq_no number -- should be populated with sequence SEQ_TAR
fsa varchar2(10)
rec_ct number

Table SOURCE structure is
-------------------------
fsa varchar2(10)
sal number

my requirement is
i have to insert into the TARGET table the fsa value from
SOURCE table along with the sequence number using sequence SEQ_TAR.nextval.

like
insert into
target(seq_no,fsa,rec_ct)
select
(SEQ_TAR.nextval,
fsa,
sum(sal)
from source
group by fsa
)

this is giving me a error.
Kindly help.
yes, makes sense, invalid syntax. The opening bracket for the second set of brackets should be in front of the select (actually the second set isnt needed).
Expand|Select|Wrap|Line Numbers
  1. Elapsed: 00:00:00.59
  2. [159]SQL> create table t (col1  varchar2(100));
  3.  
  4. Table created.
  5.  
  6. [159]SQL> insert into t (col1) (select 1 from dual);
  7.  
  8. 1 row created.
  9.  
  10. Elapsed: 00:00:00.18
  11. [159]SQL> insert into t (col1) select 1 from dual;
  12.  
  13. 1 row created.
  14.  
  15.  
further though, your select statement should be executable without the insert portion (its a good way to test that it returns what you want as well). In your case you cant group by one field and select 2.

Expand|Select|Wrap|Line Numbers
  1. Elapsed: 00:00:00.01
  2. [159]SQL> drop table t;
  3.  
  4. Table dropped.
  5.  
  6. Elapsed: 00:00:01.01
  7. [159]SQL> create table t (col1  number, col2 varchar2(30), col3 varchar2 (30));
  8.  
  9. Table created.
  10.  
  11. Elapsed: 00:00:00.01
  12. [159]SQL> create sequence t_seq;
  13.  
  14. Sequence created.
  15.  
  16. Elapsed: 00:00:00.04
  17. [159]SQL> insert into t select t_seq.nextval,dummy,dummy from dual;
  18.  
  19. 1 row created.
  20.  
  21.  
without using some sort of grouping function like max() or min() or sum() on the other columns you end up with this error
Expand|Select|Wrap|Line Numbers
  1. [159]SQL> select col1,col2,col3 from t group by col2;
  2. select col1,col2,col3 from t group by col2
  3.        *
  4. ERROR at line 1:
  5. ORA-00979: not a GROUP BY expression
  6.  
based on your requirement i dont think you need to group by anything. just insert the sequence and fsa field into the target table.
Jan 11 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: | last post by:
Hello, here is the code: <? $a=array(1,2,3,4,5,6,7,8,9,10); $b=array(1,3,5,7,9); $c=array(1,1,2,2,3,3); $d=array_count_values($b); $e=array_count_values($c);
11
by: grumfish | last post by:
I'm trying to add a row to a MySQL table using insert. Here is the code: connection = MySQLdb.connect(host="localhost", user="root", passwd="pw", db="japanese") cursor = connection.cursor()...
4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
1
by: Joshua L. San Juan | last post by:
Hi, I had dumped data using the command: pg_dump -U dbowner dbname -a -t table -f table-data.sql two tables (e.g. table1, table2). In the first table - the correct sequence was set as can...
7
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" +...
1
by: Jan | last post by:
I have a table with autoincrement unique ID plus name (required not to be blank) and other fields. I have a list of names in another table and would like to do insert to the name field of the...
2
by: fatjoez | last post by:
Hi there. I'm trying to create a Trigger for on insert, which will be triggered upon any attempted insert into a database table item. Basically want it to say, if the attempted insert value for...
6
by: yashumrhsn | last post by:
hi.... its very urgent.... i have a 2 tables in table1 contains 2 fields-username(text) and points(number) and table2 contains points(number) and image(attachment) fields.... what i want is, the...
1
by: JoeKid09 | last post by:
Hi Guys, Thanks in advance for your help. I unpivoted a table which I want to insert into another table. Below is my script. I got an error; anyone can see where my error is. insert into...
1
by: Phil Gsy | last post by:
Hi i would like to add a new table (TRADE_SEQ) to my document management database which would contain a squential number in the format currentyear#### so it would start at 20100001. The next sequence...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.