读书人

数据库的计算列有关问题

发布时间: 2012-03-09 21:42:54 作者: rapoo

数据库的计算列问题
create database test
go

use test
create table t
(
a int primary key,
b int not null,
c int not null,
d as b/c
)
上面是个例子不考虑c为0,本意是弄计算列让d为b除c的结果,而上面的b/c是取模,该怎么做?d并没有声明类型,数据库是根据b和c来判断d的类型的吧。测试b c 为float型时可以。怎么做可以达到d 为 b除c的结果呢(b c为整数),

[解决办法]
d as b*1.0/c
[解决办法]

SQL code
create table t(  a int primary key,  b int not null,  c int not null,  d as CAST(b AS DECIMAL(18,2))/c)INSERT INTO t(a,b,c)SELECT 1,2,3 UNION ALLSELECT 3,6,7 UNION ALLSELECT 6,7,8SELECT * FROM t/*a           b           c           d----------- ----------- ----------- ---------------------------------------1           2           3           0.66666666666663           6           7           0.85714285714286           7           8           0.8750000000000*/ 

读书人网 >SQL Server

热点推荐