Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm a newbie. I'm working out with Views. I have created a view. However, I get an error message when I try to update it.

SQL
CREATE VIEW vw_demo
AS
SELECT o.OrderID, o.CustomerID, o.Shipcountry,d.Unitprice, d.quantity
FROM Orders o INNER JOIN [Order Details] d
ON (o.OrderID = d.OrderID AND o.ShipCountry = 'France')


Updating view
SQL
update vw_demo
SET CustomerID = 'Averroes'
WHERE OrderID =10248


I get the following error message.
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.


Where am I going wrong? Please help!

Thanks in advance!
Posted

1. Increase the column length of CustomerID column of "Orders" table. Currently the column length is less than 8. Make it 8 or above it will work, I hope.

2.Another issue, to make an update able view, keep the following options in mind:

The SELECT statement must only refer to one database table.
The SELECT statement must not use GROUP BY or HAVING clause.
The SELECT statement must not use DISTINCT in the column list of the SELECT clause.
The SELECT statement must not refer to read-only views.
The SELECT statement must not contain any expression (aggregates, functions, computed columns…

The above points taken from the link below:
http://www.mysqltutorial.org/create-sql-updatable-views.aspx[^]
 
Share this answer
 
You cannot perform delete / update or inserts on a view.
A view provides only a static representation of data.

To perform updates, write update clauses in a stored procedure.
 
Share this answer
 
Comments
Newbie271 6-Feb-13 5:29am    
Hi Abhinav!

Thanks for your reply.

My books says you can modify data in the view if the modification affects only one table at a time. So, I just followed the example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900