3

Trigger with Insert into (select * ...)

I'm trying it.

INSERT INTO T_ USERS SELECT * FROM USERS WHERE ID = :new.ID;

not working...

this work.

INSERT INTO T_USERS(ID) VALUES(:new.ID);

Trigger

create or replace trigger "TRI_USER"
AFTER
insert on "USER"
for each row
begin
INSERT INTO T_USER SELECT * FROM USER WHERE ID = :new.ID;
end;​
5
  • 2
    Structure of tables T_USER and USER is the same? Commented Jan 23, 2017 at 19:45
  • 5
    "not working" is not a valid Oracle error message Commented Jan 23, 2017 at 19:48
  • 2
    Using insert without specifying a column list is considered bad coding style. As is using select * Commented Jan 23, 2017 at 19:49
  • @MegaTron yes. the same. Commented Jan 23, 2017 at 19:55
  • Ok, then see my updated answer Commented Jan 23, 2017 at 19:59

3 Answers 3

1

this work.

INSERT INTO T_USERS(ID) VALUES(:new.ID);

So if it fits to you then try this:

INSERT INTO T_USER(ID) SELECT ID FROM USER WHERE ID = :new.ID;

If you want to select one or more rows from another table, you have to use this syntax:

insert into <table>(<col1>,<col2>,...,<coln>)
select <col1>,<col2>,...,<coln>
from ...;
Sign up to request clarification or add additional context in comments.

2 Comments

@Edulynch What do you mean by "not working", any errors? See my update
Seems to be the only way. ty.
1

Perhaps you could post the actual error you are experiencing?

Also, I suggest that you rethink your approach. Triggers that contain DML introduce all sorts of issues. Keep in mind that Oracle Database may need to restart a trigger, and could therefore execute your DML multiple times for a particular row.

Instead, put all your related DML statements together in a PL/SQL procedure and invoke that.

1 Comment

I want to know how to do the "insert" with the "select * ..." which worked for me in mysql. (trigger)
0

Its not about your trigger but because of INSERT statement

here insert statement works as below

INSERT INTO <TABLE>(COL1,COL2,COL3) VALUES (VAL1,VAL2,VAL3);  --> If trying to populate value 1 by one.

INSERT INTO <TABLE>(COL1,COL2,COL3) --> If trying to insert mult vales at a time
SELECT VAL1,VAL2,VAL3 FROM <TABLE2>;

The number of values should match with number of columsn mentioned.

Hope this helps you to understand

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.