在SQL Server 2008中创建计算列 [英] Creating a computed column in SQL Server 2008

查看:85
本文介绍了在SQL Server 2008中创建计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server 2008数据库。该数据库具有一个称为 Book的表。 图书具有以下属性:

I have a SQL Server 2008 database. This database has a Table called "Book". "Book" has the following properties:


  • ID(int)

  • 标题(nvarchar(256 ))

  • 发布日期(日期时间)

我需要创建一个计算列 AgeInMinutes 。我对计算列不是很熟悉。我理解这个概念,但不确定如何去做。在SQL Server Management Studio的列属性区域中,我在表设计器部分中看到一个名为(公式)的属性。我想我需要在这里输入我的计算。但是,我不确定该放在哪里。有人可以帮我吗?

I need to create a computed column called "AgeInMinutes". I'm not very familiar with computed columns. I understand the concept, but I'm not sure how to do it. In SQL Server Management studio, in the "Column Properties" area, I see a property called "(Formula)" in the Table Designer section. I assume I need to enter my calculation here. However, I'm not sure what to put here. Can somebody please help me?

谢谢!

推荐答案

您可以将创建表中的列定义为:

You can define the column in your CREATE TABLE as:

AgeInMinutes为(DATEDIFF(minute ,PublishDate,GETDATE())

或者,只需

ALTER TABLE Book
ADD AgeInMinutes as (DATEDIFF(minute, PublishDate, GETDATE())

这篇关于在SQL Server 2008中创建计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆