Hi
i have a view that contain multiple tables from my database and i want to view it on datagridview and update it's data
some people says you can update joined tables using instead of triggers
how is that ?is there any example ?
thanks in advance.
This is quite an advanced thing, and you need to understand more about databases to understand why you cannot update all columns of a view..
Suppose we have CUstomer and Orders. When we join on ID we get:
CustID, Name, CustID, OrderID, Product
1, John Smith, 1, 1, Sugar
1, John Smith, 1, 2, Lemons
1, John Smith, 1, 3, Potatoes
SELECT * FROM cust NATURAL INNER JOIN order
Maybe SQL Server supports Natural, maybe it doesnt.. ITs just a fast way of joining two tables where the columns you want to join on are the same name.
Now you want to update this:
UPDATE
(SELECT * FROM cust NATURAL INNER JOIN order)
SET
name = name + product
How will the database do that? How will it set John Smith to be:
John Smithsugar
John Smithlemons
John Smithpotatoes
all at the same time? You cant! A cell in a row CANNOT have 3 different values all at the same time
So, NONE of the columns from the Customer table in this query are updatable, because they are ALL repeated. Their KEYS have LOST the unique quality.
My advice to you would be:
Dont use JOINs for queries you intend to write back to the db.
If you must use a join, then you can only update ONE table, the one that still has a unique key present.
That one table should be how your updating commands are written.
In this case, it would be ORDERS..
Your select might look like:
SELECT orders.*, customer.Name FROM oders NATURAL INNER JOIN customers
But your update query will look like:
UPDATE orders SET blah = blah...
DONT even try to update customer here.. Update it elsewhere, and mark the column with "John Smith" in your grid view as Read only.