mssql sqlserver 根据表中两列(相加、相乘、相减)生成新列的方法分享


摘要:
下文分享sqlserver表中,新列采用其它两列生成的方法分享,如下所示:
实验环境:sqlserver 2008 R2


例:

     create table [maomao365]
(sort varchar(30) not null,qty int not null,qty2 int not null )
go

insert into [maomao365]
(sort,qty,qty2)values
('A',2,8),('B',3,6),('C',5,4),
('F',4,3),('G',5,7),('C',6,3) 
go

---新增里qtyAll 为qty和qty2相加
alter table [maomao365]
add qtyAll as qty+qty2 
go
---新增里qtyJian 为qty和qty2相减
alter table [maomao365]
add qtyJian as qty-qty2 
go

---新增里qtyCheng 为qty和qty2相乘
alter table [maomao365]
add qtyCheng as qty*qty2 
go


---新增里qtyChu 为qty和qty2相除
alter table [maomao365]
add qtyChu as qty/qty2 
go

select * from [maomao365]

go
truncate table [maomao365]
drop     table [maomao365]