I am trying to insert a row in a table using VIEW as
INSERT INTO FIELDI18N(LANGUAGE_ID) VALUES (1);
but it gives me following error:
Error starting at line 5 in command:
INSERT INTO FIELDI18N(LANGUAGE_ID) VALUES (1)
Error at Command Line:5 Column:22
Error report:
SQL Error: ORA-01733: virtual column not allowed here
01733. 00000 - "virtual column not allowed here"
*Cause:
*Action:
Any Clue ?
Added the View Definition:
CREATE OR REPLACE VIEW FIELDI18N("FIELDID", "NAME", "TYPE", "DESCRIPTION", "LANGUAGE_ID")
AS
(SELECT field.fieldid,
field.type,
NVL(i18n.name, field.name) name,
NVL(i18n.description, field.description) description,
i18n.language_id
FROM fields field
JOIN i18n_fields i18n
ON (field.fieldid = i18n.fieldid)
);
LANGUAGE_ID is probably a calculated field, or in any case the database cannot infer what change is to be made to the tables underlying the view based on the change you are requiring. Have to see the view definition code to know.
I believe that in order to do an insert or update using a view that all of the tables in the view must be joined via a primary key. This is to prevent duplicates caused by the view which cannot be updated.
Is there a very good reason you are not just inserting into the underlying table? If you can, just insert into the table directly and avoid this complication.
What are you expecting Oracle to do? Are you expecting it to insert a new record into i18n_fields?
If you really want to do it like this you will need to create an INSTEAD OF trigger because Oracle can't figure out which of the underlying tables it should insert into when your run your insert statement.
Related
I have created a SQL view that retrieves the result set I desire but when I try to add a new set of values it fails. When searching around on the net it appears that VIEW has a few limitations and some types of joins may not be allowed, I have tried to create the view a few different ways but with no success yet, would anyone happen to know a way I could rewrite this create view query that would allow me to insert new values into it?
The view:
CREATE VIEW NATIONAL_ITEMS
AS
SELECT item.itemno, item.itemdesc, item.itemprice
FROM item
FULL OUTER JOIN invoiceitem ON item.itemno = invoiceitem.itemno
WHERE item.itemdesc LIKE '%National%'
AND invoiceitem.invoiceno IS NULL
WITH CHECK OPTION;
My insert statement that does not work:
INSERT INTO NATIONAL_ITEMS
VALUES ('123-456', 'National TV', 100);
I get this error:
Error starting at line : 1 in command -
INSERT INTO NATIONAL_ITEMS VALUES ('123-456', 'National TV', 100)
Error at Command Line : 1 Column : 1
Error report -
SQL Error: ORA-01733: virtual column not allowed here
01733. 00000 - "virtual column not allowed here"
Any help would be greatly appreciated, thank you.
You have two options for the view in order to insert in the view.
First, Your View must contain all the keys present in the table on which you are creating the view i.e there must be no DISTINCT OR GROUP BY clause.
If you performing JOIN on the tables same is applicable for all join tables, all keys from the table must be present in the view and there must be no DISTINCT OR GROUP BY clause.
The table which has 1:1 row relationship with the rows in the view called Key preserving table
Second, you can create instead of trigger on the view. The trigger will be fired instead of INSERT, UPDATE, OR DELETE and in the trigger, you can handle the DML statement.
Since your table is not key preserving then you can make use of INSTEAD OF TRIGGER.
CREATE OR REPLACE TRIGGER NATIONAL_ITEMS_TRG
INSTEAD OF INSERT
ON NATIONAL_ITEMS
FOR EACH ROW
BEGIN
INSERT INTO ITEM(itemno, itemdesc, itemprice) VALUES (:NEW.itemno, :NEW.itemdesc, :NEW.itemprice);
END;
here is my code :
frontgreat=> CREATE TABLE contact_titlemessagesuggestion;
ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE contact_titlemessagesuggestion;
i don't understand why it's not working and why it's an syntax error.
frontgreat=> DROP TABLE contact_titlemessagesuggestion;
ERROR: table "contact_titlemessagesuggestion" does not exist
have no syntax error and work fine.
Regards
You can normally not create a table without any columns. Therefore one often either has to list the columns, or use for example a query that provides both data (but meta-data as well) to construct the columns in the table.
For example:
CREATE TABLE contact_titlemessagesuggestion (
pk INT
);
I am trying to run a simple insert statement in db2 but getting invalid statement:
my sql :
insert into schema1.table1 select * from schema2.table1
Error:
SQLCODE = -20186, ERROR: A CLAUSE SPECIFIED FOR THE DYNAMIC SQL; STATEMENT BEING PROCESSED IS NOT VALID
I had gone through ibm knowledge center and found this page.
I understand db2 allows 3 forms of insert:
There are three forms of this statement:
The INSERT via VALUES form is used to insert a single row into the table or view using the values provided or referenced.
The INSERT via SELECT form is used to insert one or more rows into the table or view using values from other tables, or views, or both.
The INSERT via FOR n ROWS form is used to insert multiple rows into the table or view using values provided or referenced.
My question is why db2 is looking for "for n rows" clause in my query.
Note: this query is being sent through SSGODBC.
Might want to try and explicitly state the columns.
INSERT INTO Schema1.table1 (col1,col2,...) SELECT col1,col2,... FROM Schema2.table2
New to sql statements etc and I have an issue with what i am doing using squirrelSQL on linux machine
I Created a table and used the following sql statements:-
INSERT INTO FIRSTTABLE VALUES
(11,'TEN','STEVE'),(21,'TWENTY','JO'),(31,'THIRTY','KIDS')
ALTER TABLE FIRSTTABLE
ADD SURNAME VARCHAR(15);
this works fine however when i attempt to insert data/values into the the surname row i keep experiencing errors, the SQL statement i am using is:-
INSERT INTO FIRSTTABLE (SURNAME)
VALUES ('THOMAS'),('THOMAS'),('THOMAS'),('THOMAS');
This particular statement returns the following error:-
Error: Column 'ID' cannot accept a NULL value.
SQLState: 23502
ErrorCode: 30000
I only wish to add data/values into the surname column,after creating a new column with the alter table statement, i have tried many different combinations including using a SELECT statement prior to the INSERT statement above which also gives errors any guidance will be greatly appreciated,
You are inserting into Surname, without assigning a value to the other fields. You are getting this error message because ID is blank, and should not.
Understand that INSERT creates new rows. If you wish to modify existing rows, use UPDATE
In this case you could use UPDATE FIRSTTABLE SET SURNAME='THOMAS';
Omitting the WHERE clause affects all the fields in the table.
Hope it helps, and good luck in your learning process!
The approach is wrong, you need to:
UPDATE FIRSTTABLE SET SURNAME='THOMAS' WHERE ID IN (11, 21, 31)
Inserting will add a new row to the table. So you need to update a row using
UPDATE FIRSTTABLE SET SURNAME="THOMAS" WHERE ID=11
At work we have a SQL Server database. I don't know the db that well. I have created a new column in the table for some new functionality....straight away I have started seeing errors
My statement was this:
ALTER TABLE users
ADD locked varchar(50) NULL
GO
The error is:
Insert Error: Column name or number of supplied values does not match table definition
I have read that the error message appears when during an INSERT operation either the number of supplied column names or the number of supplied values does not match the table definition.
But I have checked so many times and i have changed the PHP code to include this columns data yet I still receive the error.
I have run the SQL query directly on the db and still get the error.
Funny enough the query which gets the error is an Update.
UPDATE "users"
SET "users"."date_last_login" = GETDATE()
WHERE id = 1
Have you considered it could be a trigger causing it?
This is the error message you would get.
If its an Update action causing it check trigger actions that Updates on that table run.
Do it with:
#sp_helptrigger Users, 'UPDATE';
This will show triggers occuring with ‘update’ actions.
If there is a trigger, grab the triggers name and run the below (but replace TriggerNameHere with real trigger):
#sp_helptext TriggerNameHere;
This will give you any SQL that the trigger runs and could be the INSERT the error message is referring to.
Hope this helps
Aside from TRIGGERS,
the reason for that is because you are using implicit type of INSERT statement. Let's say your previous number of columns on the table is 3. You have this syntax of INSERT statement,
INSERT INTO tableName VALUES ('val1','val2','val3')
which executes normally fine. But then you have altered the table to add another column. All of your INSERT queries are inserting only three values on the table which doesn't matches to the total number of columns.
In order to fix the problem, you have to update all INSERT statements to insert 4 values on the table,
INSERT INTO tableName VALUES ('val1','val2','val3', 'val4')
and it will normally work fine.
I'll advise you to use the EXPLICIT type of INSERT wherein you have to specify the columns you want to insert values with. Eg,
INSERT INTO tableName (col1, col2, col3) VALUES ('val1','val2','val3')
in this ways, even if you have altered your tables by adding additional columns, your INSERT statement won't be affected unless the column doesn't have a default value and which is non-nullable.