INSERT error in Oracle - sql

I am working with oracle (with Toad) but when I try to execute this query
INSERT INTO KEYUSER(NAME) VALUES(UNAME) WHERE ID = 1;
I get this error:
SQL command not properly ended. Found 'WHERE' expecting ;-or- LOG -or- RETURN RETURNING
What is wrong? Regards.

You can never use where clause in insert statement. It does not make any sense to have it while you are simply trying to insert a row.

General Insert statement is like below
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
So in your case you have to remove where clause from your statement and query will be like below
INSERT INTO KEYUSER(NAME) VALUES('UNAME');

Presumably, you intend an UPDATE rather than INSERT:
UPDATE KEYUSER
SET NAME = UNAME
WHERE ID = 1;
Sometimes new users of SQL get confused between INSERT and UPDATE. INSERT inserts new rows into a table. UPDATE changes values in existing rows. The confusion is become some people interpret INSERT as "inserting new values into a row".

Related

INSERT query not working

what is the problem with this query???
INSERT INTO query_tbl (winner) VALUES ("yes") WHERE id = 5;
INSERT does not take a WHERE clause, because it creates a new row* unconditionally.
If you want to update an existing row, use
UPDATE query_tbl SET winner='yes' WHERE id = 5
If you want to insert a new row, insert both winner and id:
INSERT INTO query_tbl(id, winner) VALUES (5, 'yes')
Note the use of single quotes around string literal 'yes'.
* SELECT query may be executed as part of insertion sequence; in this case, a WHERE clause is associated with SELECT, not with INSERT.
If you INSERT a new record then try this:
INSERT INTO query_tbl (winner) VALUES ("yes");
or UPDATE an exiting record (I mean change a value of a record), then:
UPDATE query_tbl SET winner='yes' WHERE id = 5;
Good Luck!

ORACLE TRIGGER INSERT INTO ... (SELECT * ...)

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

Can I access values from where clauses in SQLite triggers?

I would like to trigger an update to a table based on the where clause of an insert to a different table. For example:
CREATE TRIGGER update_key_table
BEFORE INSERT ON value_table
BEGIN
INSERT OR IGNORE INTO key_table (key_name) VALUES ('new_key_name');
END;
This would update key_table with the value 'new_key_name' when the following query is run:
INSERT INTO value_table (key_id, value)
SELECT key_table.key_id, 'new_value'
FROM key_table
WHERE key_table.key_name = 'new_key_name';
However, I have not been able to find any way to get access to the 'new_key_name' value from the triggering query's WHERE clause.
I understand that I can just run the following two queries in sequence, it would just be inconvenient in this particular application:
INSERT OR IGNORE INTO key_table (key_name) VALUES ('new_key_name');
INSERT INTO value_table (key_id, value)
SELECT key_table.key_id, 'new_value'
FROM key_table
WHERE key_table.key_name = 'new_key_name';
An INSERT trigger can access only the values in the new record to be inserted.
You have to execute the two queries separately.
Alternatively, if you can modify all your applications, you could create a view with all three columns (key_id, value, and key_name), and create an INSTEAD OF trigger that executes both the 'real' INSERTs.

UPSERT in SQLite

I don't want to use REPLACE INTO because it's basically a DELETE and INSERT and it's complicated to use the data from the old columns.
INSERT OR IGNORE is a bit of a hack because all errors are ignored, so this is not an option.
I've read a blog article which uses the following:
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
IF ##ROWCOUNT=0
INSERT INTO Table1 VALUES (...)
I like this approach really much, but I don't know how I can implement this IF-clause with the ##ROWCOUNT in SQLite, this is what I got:
SELECT CASE (SELECT
CASE
WHEN EXISTS(SELECT 1 FROM t WHERE id=2)
THEN 1
ELSE 0
END)
WHEN 1
UPDATE t set a='pdf' WHERE id=2;
ELSE
INSERT INTO t (a) VALUES ('pdf');
END
the SELECT CASE seems to be the only way to use a CASE-clause in SQLite because everything else throws an syntax error. But it's also not possible to use a UPDATE- or INSERT-statement in the SELECT CASE, so this throws an error.
I've tried the following
UPDATE t set a='pdf' WHERE id=2;
CASE WHEN (changes()=0) THEN
INSERT INTO t (a) VALUES ('pdf');
END
but this doesn't work, because the CASE-clause throws an syntax error.
can someone provide an example using ##ROWCOUNT for an UPSERT in SQLite?
SQLite has no built-in UPSERT-like statement that doesn't delete the old record.
You should check the number of changes in your program, and execute the INSERT conditionally.
However, if you really want to do this in SQL, it's possible; but you have to use the INSERT ... SELECT ... form so that you are able to insert zero records, if needed:
BEGIN;
UPDATE t SET a = 'pdf' WHERE id = 2;
INSERT INTO t(id, a) SELECT 2, 'pdf' WHERE changes() = 0;
COMMIT;
You should use sqlite API in this case and write "IF logic" in your application.
sqlite3_prepare16_v2(stmt1, "UPDATE t SET a=? WHERE id=?");
sqlite3_prepare16_v2(stmt2, "INSERT INTO t(id, a) VALUES(?, ?)");
for(...) // iterate by rows to be updated/inserted
{
//set parameter values for stmt1
sqlite3_step(stmt1);
if( !sqlite3_changes(dbh) )
{
//set parameter values for stmt2
sqlite3_step(stmt2);
}
}

DB2 SQL - Cannot successfully insert into table after trigger has been made

I have been running into problems inserting values into a table after having a trigger attached to it.
The trigger is the following:
CREATE TRIGGER trig
AFTER INSERT ON Follows
REFERENCING NEW as N
FOR EACH row
WHEN ((Select email from Celebrity) <> N.followed_email)
UPDATE followSuggestion SET followSuggestion.Suggested_follower = N.followed_email, followSuggestion.Suggested_followee = N.follower_email
;
the insert code is the following:
INSERT INTO follows
VALUES('Michael_Phelps#uss.net','Michael_Phelps#uss.net');
And the error is as follows
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES
INTO statement is more than one row. SQLSTATE=21000
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO statement is more than one row.
Thank you in advanced!
I think this is the problem:
(Select email from Celebrity) <> N.followed_email
The subquery will probably return more than one row so the scalar operator <> will not work.
You will have to rephrase that condition to something like:
WHEN ((Select COUNT(email) from Celebrity WHERE email = N.followed_email) = 0) ...