首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

数据库的计算列有关问题

2012-03-09 
数据库的计算列问题createdatabase testgouse testcreate table t(a int primary key,b int not null,c in

数据库的计算列问题
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*/ 

热点排行