开发者

Help with creating a computed column (sql server 2008)

Greetings.

Need some assistance with creating a computed column on this table:

USE [ScienceWorksSummary]

GO

/****** Object:  Table [dbo].[Authors_Articles]    Script Date: 04/07/2011 10:38:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Authors_Articles](
    [IdAuthor] [int] NOT NULL,
    [IdArticle] [int] NOT NULL,
    [Part] [float] NULL,
 CONSTRAINT [PK_Authors_Articles] PRIMARY KEY CLUSTERED 
(
    [IdAuthor]开发者_运维技巧 ASC,
    [IdArticle] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

The point of a computed column (Part) is to get a part of work of each author, that wrote a same article. So, for example, if idAuthor = 1, idArticle = 1 and idAuthor = 2, idArticle = 1, then Part must be 0.5 for each of them.

Thanks for your help!


CREATE TABLE [dbo].[Authors_Articles](
    [IdAuthor] [int] NOT NULL,
    [IdArticle] [int] NOT NULL,
 CONSTRAINT [PK_Authors_Articles] PRIMARY KEY CLUSTERED 
(
    [IdAuthor] ASC,
    [IdArticle] ASC
))
GO
create function dbo.calcPart(@IdArticle int) returns float as
begin
return (Select 1.0/count(*) from [Authors_Articles] where IdArticle = @IdArticle)
end
GO
alter table Authors_Articles add Part as dbo.calcPart(IdArticle)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜