Insert the record in the second table by making the trigger on the first table
I hv created 2 tables as shown below the code sql. I want to insert some record (line current value) column of the first table, next sequence value and sysdate into second table. I hv created trigger but I gave warning.so I want to know what is the problem in my sql code.
Pls correct!
SQL > create table myuser1 (u_id varchar2 (10), uname varchar2 (20), sex char(1));
SQL > create table myuser2 (reg_no number (5), reg_date Date, u_id varchar2 (20));
SQL > create the sequence myuser2_seq;
SQL > alter table myuser1 add constraint pk_myuser1 key (u_id) primary;
SQL > alter table myuser2 add constraint pk_myuser2 key (reg_no) primary;
SQL > CREATE TRIGGER MYTRIGGER
2 AFTER INSERT ON myuser1
3 FOR EACH LINE
4 BEGIN
5 INSERT INTO myuser2 (reg_no, reg_date, u_id)
6 VALUES (myuser2_seq.nextval, SELECT SYSDATE FROM DUAL, select u_id myuser)
1)
7 END;
8.
WARNING: Trigger created with compilation errors.
JKMourya wrote:
I hv created 2 tables as shown below the code sql. I want to insert some record (line current value) column of the first table, next sequence value and sysdate into second table. I hv created trigger but I gave warning.
so I want to know what is the problem in my sql code.
Pls correct!SQL > create table myuser1 (u_id varchar2 (10), uname varchar2 (20), sex char(1));
SQL > create table myuser2 (reg_no number (5), reg_date Date, u_id varchar2 (20));
SQL > create the sequence myuser2_seq;
SQL > alter table myuser1 add constraint pk_myuser1 key (u_id) primary;
SQL > alter table myuser2 add constraint pk_myuser2 key (reg_no) primary;
SQL > CREATE TRIGGER MYTRIGGER
2 AFTER INSERT ON myuser1
3 FOR EACH LINE
4 BEGIN
5 INSERT INTO myuser2 (reg_no, reg_date, u_id)
6 VALUES (myuser2_seq.nextval, SELECT SYSDATE FROM DUAL, select u_id myuser)
1)
7 END;
8.WARNING: Trigger created with compilation errors.
SELECT inside VALUES clause is NOT allowed.
do SELECT statements before the INSERT command
Tags: Database
Similar Questions
-
Insertion of records in two different tables at the same time?
Hello everyone, I have question about inserting records in two different tables at the same time, I'm looking for is by the way a unique id, which is created in the first statement insert to the second insert statement. Example of this problem:
< cfquery name = "addRecords1" datasource = 'test' >
Insert Into Table1 (name, Date, age)
Values (< cfqueryparam cfsqltype = "cf_sql_char" value = "#arguments.) "Name # ' >.
< cfqueryparam cfsqltype = 'cf_sql_date' value = '#arguments. "Date # ' >.
< cfqueryparam cfsqltype = "cf_sql_int" value = "#arguments. Age #"(>); "
Select SCOPE_IDENTITY() as RecID;
< / cfquery >
< cfquery name = "addRecords2" datasource = 'test' >
Insert into Table2(Company,City,Date,ID)
Values (< cfqueryparam cfsqltype = "cf_sql_char" value = "#arguments.Company #" >,)
< cfqueryparam cfsqltype = "cf_sql_char" value = "" #City # ">,"
< cfqueryparam cfsqltype = 'cf_sql_date' value = "" #Date # ">,"
( < cfqueryparam cfsqltype = "cf_sql_int" value = "How to pass RecID to insert in this table?" >).
< / cfquery >
In this example, I'm inserting records in table 1 and creation of IDENTITY SCOPE as RecId. I would like to pass this id and insert it in my table 2. This Id, I'll use in my second table as an identifier. If anyone knows anything about this please let me know. Thank you.
); QueryName - DOT - ColumnName, so it should be:
); HTH,
^_^
-
FM inserts the wrong doctype declaration / strange
When I create and save a new task of DITA in FM11 (using DITA 1.1), FM 'Concept' doctype written while referring to the correct task dtd. FM also auto-insère a concept as root element element. The XML looks like this:
The structapps file seems to me and also contains the correct doctype declaration. Do you have any idea where I could look to find the error?
Robert
OK, I found it now. Regardless of what is configured elsewhere, FM simply inserts the FIRST element of the EDDmarked as "valid" as root element.
I used a flat DSP with all elements in a single file.
So I have to work with separate EDDs for different types of items to have only a single valid root by file.
-
FETCH ONE RECORD IN THE SECOND TABLE OF CORRELATED SUB QUERY
Hi all
I have provided the script below, I want to single fecth record in the second table in the join query,
based on the example below, I want to go get one record of the table emp2 what matches with the emp_id of table emp1, please note emp2 may contain more record for the emp_id emp1 which respects
all records can be selected in the table emp2.
DROP TABLE emp1.
CREATE TABLE emp1 (emp_id NUMBER);
INSERT INTO emp1 VALUES (1);
INSERT INTO emp1 VALUES (2);
COMMIT;
DROP TABLE emp2.
CREATE TABLE emp2 (emp_id NUMBER, emp_name VARCHAR2 (100));
INSERT INTO emp2 VALUES (1, 'Name1');
INSERT INTO emp2 VALUES (2, 'Name2');
INSERT INTO emp2 VALUES (1, 'Name3');
INSERT INTO emp2 VALUES (2, 'Conjoint4');
COMMIT;
SELECT * from emp1.
SELECT * from emp2.
SELECT T1. EMP_ID, MIN (T2. EMP_NAME)
FROM EMP1, EMP2 T2 T1
WHERE T1. EMP_ID = T2. EMP_ID
GROUP T1. EMP_ID;
My output should be the same as the result set of query above, but I don't want this logic, please provide the solution by using a different logic, thanks in advance.
2811876 wrote:
Thanks for your comments :-)
My business logic will change to 'Fetch N second timeline table', that's the reason why I asked for a different approach, if I use max, min to achieve this does not allow me to evolve dynamically.
Although logic has not been expressed at all in your original question, so good job I asked.
You could do something like:
SQL > ed
A written file afiedt.buf1 with emp1 (select 1 as the emp_id of union double all the)
2. Select 2 double
3 )
4, emp2 (select 1 as emp_id, 'name 1' as emp_name double union all
5. Select "name 2' Union double every 2
6 select 1, 'name' 3' from dual union all
7. Select 2, 'name 4' double union all.
8 select 1, 'name 5' from dual union all '.
9 select 2, 'name 6' from dual '.
10 )
11-
12. end of test data
13-
14 select emp_id, emp_name
15 of)
16 select t1.emp_id, t2.emp_name
17, row_number() over (partition by order of t2.emp_name t1.emp_id) rn
emp1 t1 18
19 join t2 emp2 (t1.emp_id = t2.emp_id)
20 )
21 * where rn<=>=>
SQL > /.Enter the value for rows_required: 1
21 Alumni: where rn<=>=>
21 news: where rn<=>=>EMP_ID EMP_NA
---------- ------
1 name 1
2 name 2SQL > /.
Enter the value for rows_required: 2
21 Alumni: where rn<=>=>
21 news: where rn<=>=>EMP_ID EMP_NA
---------- ------
1 name 1
1 name 3
2 name 2
2 name 4 -
Insert the record of one table to another with the help of the cursor
Help, please!
the tables are - 1. countries (country_id pk, country_name, region_id)
2A (country_id, country_name, region_id)
table data are
1 to 1
2 b 2
3 C 3
NULL d 4
5 e 5
6 f 6
7 g 7
-----------------------
Insert the record in the table a country table with the help of the cursor, insert all the non-null records.
This procedure does not correct result
-----------------------
create or replace
Amit procedure as
cursor c1 is select * from a;
RW a % rowtype;
Start
Open c1;
Fetch c1 into rw.
While (C1% found)
loop
insert into countries values (rw.country_id, rw.country_name, rw.region_id);
commit;
Fetch c1 into rw.
If rw.country_id is null then
Fetch c1 into rw.
end if;
end loop;
Close c1;
exception
while others then
dbms_output.put_line ('exception = name ' |) RW.country_name);
end;You don't need cursor at all;
create or replace procedure amit as begin insert into countries (Country_ID, Country_Name, Region_ID) (select a.Country_ID ,a.Country_Name ,a.Region_ID from a ); dbms_output.put_line('Rows inserted : ' || sql%rowcount); commit; end; /
-
How to find inserted last record in the table.
Version: Oracle 10g
I have a table called 'Manufacturing' and 3 columns as mfno, itemname, quantity.
How to find inserted last record in the table 'manufacturing '.
As I got to know that the Rowid is not a result perfect result. Please provide your inputs.user13416294 wrote:
Version: Oracle 10gThis is not a version. It's a product name. A version is 10.1.0.2 or 10.2.0.4, etc.
I have a table called 'Manufacturing' and 3 columns as mfno, itemname, quantity.
How to find inserted last record in the table 'manufacturing '.Not possible as your data model do not answer for him. As simple as that.
If there is a need to determine an order or associate some time to an entity, then that should be part of the data model - and a relationship, or one or several attributes are necessary to represent this information. Your data model in this case is therefore unable to meet your requirements.
If the requirements are valid, set the data model. In other words - your question has nothing to do with Oracle and nothing to do with the other pseudo columns in Oracle, the rowscn or the rowid. It is a question of pure data modeling. Nothing more.
-
How to insert huge records in the table for the practice.
Hello
I created a new database in my personal inbox. Now, I want some specimen for different practical feature of oracle 10g. I need at least 1,00,000 records with different lines and columns.
any buddy tell me of any request or package that can create the table and insert unique records in this table.
Looking for valuable advice.
Thank youCan be dbms_random.string, dbms_random.value and dbms_random.random can help.
-
How to insert the 20 records in a single table
Hi all,
I want to insert documents 20 only in a table, but suppose I want to enter the folder 21 it will be not inserted.
Suppose that to delete a record and then insert, it will insert.
but always count (*) is not greater than 20.
is there any solution for this
Please help meCreate a trigger Before Insert database on the table to keep the count = 20 in the table. If it exceeds, raise the exception and cancel the insert operation.
-
Insert the same record (same key) in different sessions
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Prod
PL/SQL Release 10.2.0.5.0 - Production
CORE Production 10.2.0.5.0
AMT for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production
Hello! I am inserting the same record in 2 different sessions without validation using TOAD:
in this table, where ID is a key:insert into FIFAPPS.TESTES values(3);
In the first session, it goes well, but for the next session... It of just execution and never ends. My question is what happens when I insert the second session? TX in advance.CREATE TABLE FIFAPPS.TESTES ( ID NUMBER )
You have a unique index or primary key (which will automatically index) on the column?
Oneself up to the entrance in the first insert your index? Since you insert same value in another session, he waits for the lock on index to be released. If the first session agrees, you will get an error (duplicate). If the first session, second session restorations go...
-
How can I get a name of table 1, column A, if column B is a negative number and insert the names of table 2?
What is the formula?
You can do this with a column of "index" in table 1, as this assistance:
The formula in C2, filled to the bottom:
IF (B =<>
That increments a counter each time that it finds a negative number in column B.
In the second table, you can retrieve a list of negative values in this way:
The formula in A2, filled to the bottom:
= INDEX (array 1::A, CORRESPONDENCE (LINE (−1, Table 1::C), 0))
It takes the line number, the formula is activated, subtracts 1 to the header line and look up the result in the column of table 1 C. If it finds a match, it feeds the line number to the INDEX page with retrieves the value of the column of table 1A.
To hide the red triangles of signage wrap the IFERROR formula, like this:
= SIERREUR (INDEX (table 1::A, CORRESPONDENCE (LINE (−1, Table 1::C), 0)),"")
Of course, you can also simply filter on column B without the need to set up a column from another table or index.
SG
-
How to insert the value from the FORM to another database table?
Hi all
I have the following problem.
I have a form that has a tabular layout for the MATURED_FD_DTLtable. (all about 20 line items)
CREATE TABLE MATURED_FD_DTL
(
ACCT_FD_NO VARCHAR2 (17 BYTE) NOT NULL,
CUST_CODE NUMBER (9),
FD_AMT NUMBER (15),
FD_INT_BAL NUMBER (15),
TDS NUMBER (15),
CHQ_NO NUMBER (10),
CREATED_DATE DATE,
CREATED_BY VARCHAR2 (15 BYTE),
PREV_YR_TDS NUMBER (15),
ADD_FD_AMT NUMBER (15),
DESCRIPTION VARCHAR2 (100 BYTE),
P_SAP_CODE NUMBER (10),
P_TYPE VARCHAR2 (1 BYTE)
)
By clicking on 'SAVE' button, all values will be automatically is stored in the MATURED_FD_DTLdatabase table.
But I want to insert these values in another table called TEMP.
I want to insert values of form only, no table select.
How can I do this?
Help me.
Is Oracle Designer 6i.
Answer:
BEGIN
Commit_form;
PREMIER_ENREGISTREMENT;
LOOP
insert into TEMP_MATURED_FD_DTL
SELECT * FROM MATURED_FD_DTL WHERE ACCT_FD_NO =: acct_fd_NO;
WHEN THE OUTPUT: SYSTEM. LAST_RECORD = "TRUE";
NEXT_RECORD;
END LOOP;
PREMIER_ENREGISTREMENT;
COMMIT;
MESSAGE ("RECORD UPDATED AS PAID '");
MESSAGE (' ', no_acknowledge);
END;
-
Hi all
I created a table with two columns method. I am assigned two values to variable recordtype.
When inserting the record in the table, a compiler error is to be there.
ERROR on line 11:Create table emp_info(empno number(5),ename varchar2(30)); DECLARE l_rec emp_info%rowtype; BEGIN l_rec.empno := 101; l_rec.ename := 'KING'; insert into emp_info(empno,ename) values(l_rec); commit; END;
ORA-06550: line 10, column 35:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 10, column 1:
PL/SQL: SQL statement ignored
Can I insert with output Recordtype variables indicating the column names. Can someone help me?Do not list of columns. Do not put brackets in the record variable:
SQL> DECLARE 2 l_rec emp_info%rowtype; 3 BEGIN 4 l_rec.empno := 101; 5 l_rec.ename := 'KING'; 6 insert 7 into emp_info 8 values l_rec; 9 commit; 10 END; 11 / PL/SQL procedure successfully completed. SQL>
SY.
-
Insert the output of a refcursor procedure into a table
Hello
I met a scenario in which I need to put the insert records returned by a procedure using refcursor, at a table.
I followed the instructions in PL/SQL 101: understanding Refcursor (PL/SQL 101: understanding Ref Cursor am unfortunately still not able to do so.)
Here is my sample codes. (Copied here as advised by the new Member)
I check the result using the following statement, which gives results.create or replace PROCEDURE TEST_PROCEDURE1 ( p_cursor OUT SYS_REFCURSOR) AS BEGIN OPEN p_cursor FOR SELECT C1,C2 FROM TEST_USER.test_table4procedure; END;
Now, I want to be able to use the output and insert the data into a table. That's how I came across this thread.variable rc refcursor; exec TEST_USER.TEST_PROCEDURE1 (:rc) print rc;
I created the types and function...
After that, I want to be able to view the records by using the function... so I used the instructions below...create or replace type test_user.type_table1 as object(var1 varchar2(50),var2 varchar2(50)); create or replace type test_user.type_table2 as table of test_user.type_table1; create or replace function test_user.test_function1 (rc in sys_refcursor ) return test_user.type_table2 is v_emptype test_user.type_table2 := test_user.type_table2(); -- Declare a local table structure and initialize it v_cnt number := 0; v_rc sys_refcursor; v_var1 varchar2(20); v_var2 varchar2(20); begin v_rc := rc; loop fetch v_rc into v_var1, v_var2; exit when v_rc%NOTFOUND; v_emptype.extend; v_cnt := v_cnt + 1; v_emptype(v_cnt) := test_user.type_table1(v_var1, v_var2); end loop; close v_rc; return v_emptype; end;
However, it fails with the errorvariable rc refcursor; exec TEST_USER.TEST_PROCEDURE1 (:rc) SELECT * FROM TABLE(test_user.test_function1(:rc));
Error from line 3 in order:
SELECT * FROM TABLE (test_user.test_function1 (:rc))
Error report:
SQL error: Missing a setting IN or OUT to index: 1
Help, please...Your code does not work for a simple reason. SYS_REFCURSOR parameters must be in IN OUT mode. Check if the RC is open when he switched mode:
create or replace type type_table1 as object(var1 varchar2(50),var2 varchar2(50)) / create or replace type type_table2 as table of type_table1 / create or replace PROCEDURE TEST_PROCEDURE1 (p_cursor IN OUT SYS_REFCURSOR) AS BEGIN OPEN p_cursor FOR SELECT ENAME,JOB FROM EMP; END; / create or replace function test_function1 (rc in sys_refcursor ) return type_table2 is v_emptype type_table2 := type_table2(); -- Declare a local table structure and initialize it v_cnt number := 0; v_var1 varchar2(20); v_var2 varchar2(20); begin if rc%isopen then dbms_output.put_line('rc is open'); else dbms_output.put_line('rc is not open'); end if; loop fetch rc into v_var1, v_var2; exit when rc%NOTFOUND; v_emptype.extend; v_cnt := v_cnt + 1; v_emptype(v_cnt) := type_table1(v_var1, v_var2); end loop; close rc; return v_emptype; end; / variable rc refcursor exec TEST_PROCEDURE1(:rc) set serveroutput on SELECT * FROM TABLE(test_function1(:rc)) / SELECT * FROM TABLE(test_function1(:rc)) * ERROR at line 1: ORA-01001: invalid cursor ORA-06512: at "SCOTT.TEST_FUNCTION1", line 15 rc is not open SQL>
Now IN OUT parameter edit mode:
set serveroutput off create or replace function test_function1 (rc in out sys_refcursor ) return type_table2 is v_emptype type_table2 := type_table2(); -- Declare a local table structure and initialize it v_cnt number := 0; v_var1 varchar2(20); v_var2 varchar2(20); begin if rc%isopen then dbms_output.put_line('rc is open'); else dbms_output.put_line('rc is not open'); end if; loop fetch rc into v_var1, v_var2; exit when rc%NOTFOUND; v_emptype.extend; v_cnt := v_cnt + 1; v_emptype(v_cnt) := type_table1(v_var1, v_var2); end loop; close rc; return v_emptype; end; / variable rc refcursor exec TEST_PROCEDURE1(:rc) set serveroutput on declare v_emptype type_table2 := type_table2(); begin v_emptype := test_function1(:rc); end; / rc is open PL/SQL procedure successfully completed. SQL>
Problem is that you can't call the procedure/function with parameters in/out OUT of SQL.
SY.
-
Trying to update a table in a second table when the data are different
Hello;
I have a the same table in two databases. The database are connected with a DB link. I'm trying to update one table based on the data in the second table when the EMP_ID is but the name does not match.
The table will look like:
Table name: EMP
EMP_ID
LAST_NAME
FIRST NAME
MIDDLE_INITIAL
My SQL is:
This refers to a number of update of all lines not the bit I want.update EMP TARGET set (TARGET.LAST_NAME, TARGET.FIRST_NAME,TARGET.MIDDLE_INITIAL) = ( select SOURCE.LAST_NAME, SOURCE.FIRST_NAME, SOURCE.MIDDLE_INITIAL from EMP@OTHER_DB SOURCE where TARGET.PHYSICIAN_ID = SOURCE.PHYSICIAN_ID and TARGET.LAST_NAME <> SOURCE.LAST_NAME);
Any help would be great!Hello
Sky13 wrote:
Hello;
I have a the same table in two databases. The database are connected with a DB link. I'm trying to update one table based on the data in the second table when the EMP_ID matchDo you doctor_id?
but the name does not match.
The table will look like:
Table name: EMP
EMP_ID
LAST_NAME
FIRST NAME
MIDDLE_INITIALMy SQL is:
update EMP TARGET set (TARGET.LAST_NAME, TARGET.FIRST_NAME,TARGET.MIDDLE_INITIAL) = ( select SOURCE.LAST_NAME, SOURCE.FIRST_NAME, SOURCE.MIDDLE_INITIAL from EMP@OTHER_DB SOURCE where TARGET.PHYSICIAN_ID = SOURCE.PHYSICIAN_ID and TARGET.LAST_NAME <> SOURCE.LAST_NAME);
This refers to a number of update of all lines not the bit I want.
Any help would be great!
There is no WHERE clause in the UPDATE statement, so that each row in the target table will be changed.
If you only want to change the lines that have a match in the source table, then add a WHERE clause (perhaps "WHERE EXISTS (...)) with a subquery miuch very similar to the one you already have), or use the MERGER instead of UPDATE.If you want to know, post CREATE TABLE and INSERT statements to recreate the tables they existed before the UPDATE and also post the contents of the table changed after the UPDATE.
Always tell what version of Oracle you are using.Maybe you want something like this:
{code}
MERGE INTO the emp target
WITH THE HELP OF)
SELECT o.emp_id
o.last_name
o.last_name
o.middle_initial
OF emp@other_db o
JOIN emp t ON o.emp_id = t.emp_id
AND o.last_name! = t.last_name
) source
WE (target.emp_id = source.emp_id
WHEN MATCHED THEN UPDATE
SET target.last_name = source.last_name
target.first_name = source.first_name,
target.middle_initial = source.middle_initial,
;
{code}
assuming that emp is unique, at least in other_db.It will work in Oracle 10 (and more). In Oracle 9, MERGER still requires a WHEN MATCHED clause, so add one if you must. No matter what he does; the subquery USE will only return matches.
Published by: Frank Kulash, October 10, 2011 16:45
-
Insert into multiple tables; Enter the id of the first
I apologize if this has been answered elsewhere, but I tried searching and got no results.
I try inserting the contents of a form in two tables. After you insert fields in a table, I want to enter the id of the first table key in the second table with the other fields.
I tried a few different things, including trying to use SCOPE_IDENTITY(), but just kept a lot of mistakes.
The current version of my code works perfectly, * except for the fact that it does not insert the ID *!
Can someone tell me what I am doing wrong? or offer another way to do this?
Thanks for your help, as usual.
Very well! I got it to work. Thank you very much! Here's what I ended up doing (I write just the code for INSERTs). I'll play with cfqueryparam some more later - thanks a lot for the valuable info!
Maybe you are looking for
-
I want to run more backward than the dates indicated restore me. How can I do
How can I run restore several months back. I'm offered only about a month when I try to run the restore [Moved from comments]
-
don't have windows auto recov cd what I can do, I have a prob of System 32
I have a 32 system prob have to extent that it is asking for windows auto recovery cd, but I didn't do it that can only make
-
How to change the menu product w / right click on the name of the file?
How can I remove an option in the menu product with the right click on a file name in the Explorer window? TKS. Carlos
-
Hi, I submitted an application (game) on the 18 and currently it always appears as "pending review". But when I go to the src it shows "approved new value =" and "status = open '... Is this a normal time-out for a BB10 app aproval? I have to do somet
-
Access/permissions of system32 folder
A few programs, I tried installed have stopped due to a same 'mistake' - they cannot access c:/windows/System32 or a file in this folder. After a lot of research, I discovered that the record is marked "read only". I tried to change the permissions i