Just like Multi-table insert(using INSERT ALL) is there multi-table Update facility in Oracle? In which version?
AFAIK there is no such facility in Oracle - you can't UPDATE multiple tables within one statement - you can use mutiple tables within an UPDATE statement but only one table can be updated.
What you could do is either wrap multiple UPDATE statements into one transaction and/or use an INSTEAD OF trigger which in turn contains multiple UPDATE statements...
Related
Hello Everyone I wanted to know if it is possible to Insert new values into multiple tables(in my case 22) in a single query. Once they are inserted I want to delete these 22(newly inserted rows) and move the deleted rows into a separate table ?
It depends on your definition of "a single query". You cannot do it in a single SQL statement like SELECT, UPDATE, DELETE, MERGE. But you can create a single query with multiple SQL statements using BigQuery Scripting (currently in Beta).
https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting
Note that these multiple updates are not transactional, BigQuery uses single-statement transactions.
I have created an on update trigger.
If I am updating 5 records in the table in a single statement, how many times will the trigger execute? Does it change if I have multiple statements updating the records in a single transaction?
Does it execute 5 times, or only once after all the transactions are complete?
It all depends on the type of trigger you are using.
a row level trigger will fire for each and every row that is affected by the DML statement (note this is also true for INSERT statements that are based on a SELECT or are using a multi-row syntax to insert more than one row at a time)
a statement level trigger will fire once for the whole statement.
Oracle, PostgreSQL and DB2 support both, row level and statement level triggers. Microsoft SQL Server only supports statement level triggers and MySQL only supports row level triggers.
With SQL 2008:
If you are doing 1 update that updates 5 rows, the trigger should be executed only once.
That's why you have to use the tables "INSERTED" and "DELETED" to be able to detect all the modified rows.
If you are doing 5 updates that update 1 row, the trigger will be executed 5 times.
Considering you are using SQL Server, the trigger will only fire once every Update.
If this is not what you want, you could consider using different update statements to make sure the trigger fires everytime.
You can look at this turorial on SQL triggers. It covers everything.
Note that if you are using Oracle the trigger can be based on rows. Not in SQL Server.
I need a sql statement, to insert a new row in one database table and update an existing row in another database table based on some conditions.
Is there a way to do this? To insert a row in one table and update a row in another database table in one sql statement?
Thanks in advance!
Yes, they are called Transactions, and are implemented with START TRANSACTION and COMMIT/ROLLBACK with something like:
START TRANSACTION;
INSERT INTO ...
UPDATE table2 SET name='TOTO' WHERE type=1;
COMMIT;
EDIT
This is not in fact one SQL query, but the operation is done atomically - and I think that is what you need.
A single SQL statement allows you to update one table, not several; if that statement is a MERGE then you can specify insert/update/delete actions but still targeting just the same one target table.
If you just want consistency, use transactions; until a transaction is committed, changes within it are not visible to the outside world.
If you want that a single update (which you cannot control) resulted in a coordinated insert, use an on update trigger in the table being updated. The trigger would insert appropriate row(s) into other table(s).
You can use Trigger to update second table on insert of first table
Yes, it's possible with stored procedures.
Watch this: Stored procedures
How do i update two or more two tables without the help of where clause.
Thanks...
You can do this by using an updateable view and instead of trigger.
For this you need to create a view on your base tables and add an "instead of" trigger to this view
Later you can update the view directly.
For more information check the following links
how to update multiple tables in oracle DB?
Update multiple table column values using single query
In SQL Server you cannot update two tables in one update statement even in an updateable view.
Will it be possible to insert into two tables with same insert command?
No you cannot perform multiple inserts into two tables in one query.
No you can't.
If you want to ensure the atomicity of an operation that requires data to be inserted into 2 tables, you should protect it in a transaction. You either use the SQL statements BEGIN TRAN and COMMIT TRAN, or you use a transaction boundary in whatever language you're using to develop the db access layer. E.g. something like Connection.StartTransaction and Connection.Commit (or Connection.Rollback on an error).
You can call a stored procedure with inserts into two tables.
Maybe in a future release of MySQL you could create a View containing the 2 tables and insert into that.
But with MySQL 5.1.41 you'll get the error:
"Can not modify more than one base table through a join view"
But inserting into 2 tables with 1 query is a weird thing to do, and I don't recommend it.
For more on updatable views check out the MySQL reference.