How to insert data in Oracle table with virtual columns? - vb.net

I have a .Net application that loads a datatable from an oracle Database. The table contains a virtual column.
When I'm inserting a new row in datatable, it says :
Insert INSERT operation disallowed on virtual columns
I do understand this error but I don't know how to skip the virtual columns when saving data back to the database.
Here is my code :
Dim Command As OracleCommand
Dim TempDataAdapter As OracleDataAdapter
Dim DataSet = new DataSet
Dim Name = "MyTable"
Dim TempDataAdapter As OracleDataAdapter
Dim DataTable as DataTable
'The connection is defined somewhere else...
Command = New OracleCommand("MyTable", Me.Connection)
Command.CommandType = CommandType.Text
TempDataAdapter = New OracleDataAdapter(Command)
'Fill the table from the database
TempDataAdapter.FillSchema(DataSet, SchemaType.Source, Name)
DataTable = DataTable = DataSet.Tables(0)
And the code for saving data back to database :
TempDataAdapter.Update(DataTable)
After the creation of the datatable, I tried deleting the virtual column from the datatable :
DataTable.Columns.Remove(DataTable.Columns("MyVirtualColumn"))
But when saving data back to the database, it returns the same error.
Can anyone help please ?
Cheers,

If you are not trying to populate or query the virtual column, can you create a view on that table that excludes the virtual column and work with that view.

When you insert data into table with virtual columns then you must list columns, i. e. you must not use
Insert into table_x values (x, y, z)
but
Insert into table_x (col1, col2, col3) values (x, y, z)
Write your .net statements accordingly. If you try to update with DataTable then you can use a view which excludes the virtual columns:
CREATE OR REPLACE VIEW V_MyTable AS
SELECT col1, col2 -- skip virtual_col3
FROM MyTable;
In case you like to select virtual columns but skip them for INSERT (or UPDATE) you can create a view with a INSTEAD OF Trigger. Would be like this:
CREATE OR REPLACE VIEW V_MyTable AS
SELECT col1, col2, virtual_col3
FROM MyTable;
CREATE OR REPLACE TRIGGER IO_MyTable
INSTEAD OF INSERT OR UPDATE ON V_MyTable
FOR EACH ROW
BEGIN
IF UPDATING THEN
UPDATE MyTable SET
col1 = :NEW.col1,
col2 = :NEW.col2
WHERE primary_key_col = :OLD.primary_key_col;
ELSIF INSERTING THEN
INSERT INTO MyTable (col1, col2) VALUES (:NEW.col1, :NEW.col2);
END IF;
END;

Related

how to create a new table with the same schema as an existing table in dolphindb?

how to create a new table with the same schema as an existing table in dolphindb?
I have established a table in dolphindb, and now I want to establish another empty table with the same schema. what should I do?
The field definition of the original table can be obtained through “schema().colDefs”:
pt = loadTable("dfs://GPS_data", "pt")
shema = pt.schema().colDefs
col1 = exec name from shema
col2 = exec typeString from shema
t = table(10:0, col1, col2)
Or obtain it through an invalid where condition:
t=select * from pt where Send_time=1970.01.01T00:00:00.000

How can I save query results into a new column and save it in the table with SQL Oracle Live?

This is the query:
SELECT ROUND(column_1,0) as new_column_to_save
FROM table_to_save_to
Thank you
if you wanna insert it into a new table :
INSERT INTO NEWTableName ( colName)
SELECT ROUND(column_1,0) as new_column_to_save
FROM table_to_save_to
If you want to update a column within the same table
UPDATE table_to_save_to
SET new_column_to_save = ROUND(column_1,0)
FROM table_to_save_to

PL/SQL bulk INSERT into a table with an unknown structure

Can you issue a FORALL bulk INSERT into a table with an unknown structure? That means, can you build dynamically the INSERT command in the FORALL construct without knowing the number of fields at compile time?
Number and name of fields is retrieved at runtime and stored in a collection:
TYPE RowType is TABLE OF VARCHAR2(50) INDEX BY VARCHAR2(50);
TYPE TableType is TABLE OF RowType;
my_table TableType;
So at runtime my_table could be filled this way for example:
my_table(1)('FIELD1') = 'VALUE1A';
my_table(1)('FIELD2') = 'VALUE2A';
my_table(1)('FIELD3') = 'VALUE3A';
my_table(2)('FIELD1') = 'VALUE1B';
my_table(2)('FIELD2') = 'VALUE2B';
my_table(2)('FIELD3') = 'VALUE3B';
my_table(3)('FIELD1') = 'VALUE1C';
my_table(3)('FIELD2') = 'VALUE2C';
my_table(3)('FIELD3') = 'VALUE3C';
The insert statements that should be bulk executed therefore are:
INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1A,VALUE2A,VALUE3A);
INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1B,VALUE2B,VALUE3B);
INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1C,VALUE2C,VALUE3C);
EDIT: Do you even read the questions or you just read a couple of words in the title? The linked question asks how to bind a variable, this question asks how to bulk issue dynamic statements. Yes, there are the words 'insert' and 'table' in both questions.
No, you can't dynamically build and execute a FORALL...INSERT... statement dynamically. You can, however, build up an INSERT statement dynamically of the form:
INSERT ALL
INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1A,VALUE2A,VALUE3A)
INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1B,VALUE2B,VALUE3B)
INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1C,VALUE2C,VALUE3C)
Or if the data you want to insert into your table resides in another table you might find an INSERT...(subquery) statement like
INSERT INTO TABLENAME
SELECT FIELD1, FIELD2, FIELD3
FROM OTHER_TABLE
WHERE something <> something_else
or you might be able to use a MERGE statement similar to
MERGE INTO TABLENAME t
USING (SELECT FIELD1, FIELD2, FIELD3 FROM OTHER_TABLE) o
ON (t.FIELD1 = o.FIELD1)
WHEN NOT FOUND THEN
INSERT (FIELD1, FIELD2, FIELD3) VALUES (o.FIELD1, o.FIELD2, o.FIELD3)
which will do a mass insert based on the data specified in the USING clause and the match criteria in the ON predicate.
So there may be ways to do what you want but without knowing the specifics of the source of your data and how you're manipulating that data prior to inserting it into your database it's tough to say whether or not any of them would apply.
Best of luck.

How to get the value after updating the table using triggers in sql

How to get the value after updating the table using triggers in sql
Having a scenario like this:
CREATE TABLE #tmp (id INT, txt VARCHAR(10))
INSERT INTO #tmp ( id, txt )
VALUES ( 1, 'abc')
You can return the values using OUTPUT (https://msdn.microsoft.com/en-us/library/ms177564.aspx) during an UPDATE like this:
UPDATE #tmp SET
txt = 'xyz'
OUTPUT INSERTED.id, INSERTED.txt
You can return any column from your table - INSERTED will contain new data while DELETED will contain previous data.
In the Update SQL it self you can use OUTPUT.
In the triggers on MS SQL servers there are to temp tabels called INSERED and DELETED see her for more info: https://msdn.microsoft.com/en-us/library/ms191300.aspx
Hope it helps.

Can SQL server instead of update force insert a new row

Is it possible in SQL Server 2008+ to force an UPDATE statement on table to be transformed into INSERT statement, thus creating new row with the old and updated columns?
Yes this is a typical scenario for an INSTEAD OF UPDATE TRIGGER.
Create the following trigger on your table and it will insert a row for each update made on your table. you can have a bit more logic inside your trigger but this is just a basic definition just to give you some idea.
Inside your INSTEAD of UPDATE trigger you will have access to two system tables Inserted and deleted.
Inserted table will hold new values for the row that was being updated by the Update statement.
Deleted table will hold Old values for the row that was being updated by the Update statement.
Demo Trigger
CREATE TRIGGER tr_Table_Instead_Of_Update
ON TABLE_NAME
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO TABLE_NAME(Col1, Col2 , Col3)
SELECT Col1, Col2 , Col3
FROM inserted
END