question

Chandrasekaran avatar image
Chandrasekaran asked

Is it possible to update a row using Create View?

Hi,

I have a table and it has a two column. I want to add those two columns and put it in to third column Total. This is can we do this by using "CREATE VIEW" Method instead spf or normal update?

sql-server-2008
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Squirrel 1 avatar image
Squirrel 1 answered

yes. You can create a view for this.

create view NewView            
as            
select col1, col2, Total = col1 + col2            
from   atable            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnFx avatar image
JohnFx answered

This sounds like a bad idea in progress.

Have you considered just adding a calculated column to the underlying table?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Squirrel 1 avatar image
Squirrel 1 answered

if you want to update the total column of the same table,

update atable            
set    total = col1 + col2            

if total is always a sum of col1 and col2, you should seriously consider John's suggestion of using a computed column

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.