Insert into view - plsql

In Oracle I create a view using a 'union all' as below
create view TESTVIEW as
select column1 from TABLE1
union all
select column1 from TABLE2;
If I want to insert into this view I get
SQL Error: ORA-01732: data manipulation operation not legal on this view
Is there any way around this if I know I want to insert in TABLE1?

Yes, you should be able to create an INSTEAD OF INSERT trigger on the view.
You'll need to write the trigger to insert the row in the base table, and it will run "instead of" the original insert.

Please refer http://docs.oracle.com/cd/E17952_01/refman-5.1-en/view-updatability.html to understand which views are directly updatable. For views which are not directly updatable use "Instead of" trigger as suggested in the above post.

Related

icCube join table with ETL

I have a Customers table which contains the salesRepEmployeeNumber which is in the Employees table.
How do I do something like
SELECT *
FROM Customers
JOIN Employees
ON Customers.salesRepEmployeeNumber = Employees.employeeNumber
with icCube ETL ?
As pointed in another answer, you can add a table based in an SQL statement that would do the job. In case your original datasource is not able to do a join :
We've not yet an join transformation, added this in our todo list. On the meantime, what you can do is.
Create an Union Table with your two tables. This will create a new table with the columns of both tables. Put the small one, first as we're going to cache it later on.
Create a Javascript view, you might need to activate Javascript in your icCube.xml configuration. In this one you can cache the first table and use a bit of js to do the join. You can trigger the table change on a field being empty. Don't forget to put 'Table Row Ordering' to Keep Table Order.
hope it helps
No need to use the ETL.
With the designer, add a table with the + sign in the menu above DataSource. The next panel gives you the choice between reading data from an existing table or an sql query.

is there a command for "AS" in postgres?

I'm wondering if there is a command like AS for postgres. Does anyone know if postges has this ability? I've tried to google it but it's a very difficult question to google :P I want to make a select statement and store it as a new table name. I want to say something like:
select subj, user as 'new' from table_name;
Yes. It's a reserved SQL key word in PostgreSQL. See Table C-1 at the linked documentation page.
It's typically used with column labels.
The AS keyword is optional, but only if the new column name does not
match any PostgreSQL keyword (see Appendix C).
CREATE TABLE new_table AS SELECT subj, user FROM table_name
So, your new table will be ready.
"I want to make a select statement and store it as a new table name" --
CREATE VIEW view_name AS
SELECT subj, user AS "new" FROM table_name;
If you don't want to leave any changes in the schema, you can do something like this-
select new.* from (
select foo, bar from old
) as new
develop your query: SELECT field AS new_field FROM table WHERE ...;
If it runs ok, copy it
Go to VIEW and do that: CREATE OR REPLACE VIEW new_view AS <QUERY>;
Save the view and use it as table.
enjoy.
In my way, you can do alias for the field and create view to store it. which ll act like table.
select subj, user as new from table_name;
By using the above query can get subj and new as a field.
And create view.
create or replace view_name as
select subj, user as new from table_name;
And just call
select view_name;

SQLite Reorder Table?

Can you reorder SQLite table columns via a query?
I would prefer a query method, but if that is not possible is there any other way?
Yes, you control the order of columns by the order you name them in the query. Both these queries return the same rows, but the columns are in a different order.
select first_column_name, second_column_name
from mytable;
select second_column_name, first_column_name
from mytable;
If you want it to appear that the columns in the base table have been permanently changed, you can use a view.
create view mytable_reordered
select second_column_name, first_column_name
from mytable;
If you wanted to make that change transparent to application programs, you'd first rename the table, then create the view, giving it the old name of the table. Finally, you'd jump through whatever hoops your dbms requires in order to make that view updatable. In SQLite, I think that means writing code to implement some INSTEAD OF triggers.

pl sql: trigger for insert data from another table

There is the table OLD and a similar one, NEW. I want to insert in the existing process that fills the table OLD a trigger event that for each new inserted row, this event will insert the newly inserted row to table NEW, as well. Inside the body of trigger, i need to include the query BELOW which aggregates values of OLD before inserted in NEW:
insert into NEW
select (select a.id,a.name,a.address,b.jitter,a.packet,a.compo,b.rtd,a.dur from OLD a,
select address,packet,compo, avg(jitter) as jitter, avg(rtd) as rtd from OLD
group by address,packet,compo ) b
where a.address=b.address and a.packet=b.packet and a.compo=b.compo;
can you correct any possible mistakes or suggest other trigger syntax on the statement below?
CREATE OR REPLACE TRIGGER insertion
after update on OLD
for each row
begin
MY select query above
end;
In a for each row trigger you cannot query the table itself. You will get a mutating table error message if you do.
I recommend only to use triggers for the most basic functionality such as handing out ID numbers and very basic checks.
If you do use triggers for more complex tasks you may very easily end up with a system that's very hard to debug and maintain because of all kinds of actions that appear out of knowhere.
Look at this question for another approach: getting rid of Insert trigger
Oracle Streams might also be a good solution. In the apply handler, you can include your own custom PL/SQL code. This procedure will be called after the COMMIT, so you can avoid mutating table errors.
However, Streams requires a large amount of setup to make it work. It might be overkill for what you are doing.

asp.net InsertCommand to return latest insert ID

I'm unable to retrieve the latest inserted id from my SQL Server 2000 db using a typed dataset in asp.NET
I have created a tableadapter and I ticked the "Refresh datatable" and "Generate Insert, Update and Delete statements". This auto-generates the Fill and GetData methods, and the Insert, Update, Select and Delete statements.
I have tried every possible solution in this thread
http://forums.asp.net/t/990365.aspx
but I'm still unsuccesfull, it always returns 1(=number of affected rows).
I do not want to create a seperate insert method as the auto-generated insertCommand perfectly suits my needs.
As suggested in the thread above, I have tried to update the InsertCommand SQL syntax to add SELECT SCOPY_IDENTITY() or something similar, I have tried to add a parameter of type ReturnValue, but all I get is the number of affected rows.
Does anyone has a different take on this?
Thanks in advance!
Stijn
I decided to give up, I can't afford to waste any more time on this.
I use the Insert statement after which I do a select MAX(id) query to hget the insert ID
If anyone should have a solution, I'll be glad to read it here
Thanks
Stijn
I successfully found a way to get the incremental id after insert using my table adapter.
My approach is a little different, I'm using a Store procedure to make the insert, so my insert command has all the values but the ID, I made the sp return the ID just calling:
SET #ID=SCOPE_IDENTITY()
and then
COMMIT TRAN
and last line will be
RETURN #ID
Then I searched my table adapter parameters for InsertCommand and set the #RETURNVALUE to the column of the incremental ID of the table, so when it's executed automatically put the return value on the id field.
Hope this help
You need to tell your table's table-adapter to refresh the
data-table after update/insert operation.
This is how you can do that.
Open the properties of TableAdapter -> Default Select Query -> Advnaced options. and Check the option of Refresh the data table. Save the adapter now. Now when you call update on table-adapter, the data-table will be updated [refreshed] after the update/insert operation and will reflect the latest values from database table. if the primary-key or any coloumn is set to auto-increment, the data-table will have those latest value post recent update.
Now you can Call the update as TableAdapterObj.Update(ds.dataTable);
Read latest values from the DataTable(ds.dataTable) coloumns and assign respective values into the child table before update/insert. This will work exactly the way you want.
alt text http://ruchitsurati.net/files/tds1.png

Resources