CREATE TABLE [dbo].[Film](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Heading] [int] NULL,
[HeadingGroup] AS (isnull([Heading],[ID])),
[HeadingName] [varchar](25) NULL,
[HeadingClose] [bit] NULL,
[SeriesName] [varchar](15) NULL,
[Series] [smallint] NULL,
[SeriesDigit] [smallint] NULL,
[Season] [smallint] NULL,
[Episode] [smallint] NULL,
[HeadingNumber] AS (isnull(isnull([SeriesName]+' ','серия ')+CONVERT([varchar],[Series]),'')+isnull((('s'+format([Season],'d2'))+'e')+format([Episode],'d2'),'')),
[PartName] [varchar](15) NULL,
[Part] [smallint] NULL,
[PartDisplay] AS (isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),'')),
[HeadingKey] AS (((left(CONVERT([varchar],[Heading]),(0))+isnull('c'+format([Series],'d2'),''))+isnull((('s'+format([Season],'d2'))+'e')+format([Episode],'d2'),''))+isnull('p'+right('00'+CONVERT([varchar],[Part]),(2)),'')),
[SeasonCount] [smallint] NULL,
[Serias] [smallint] NULL,
[Article] [varchar](20) NULL,
[Name] [varchar](200) NULL,
[NameTranslat] [varchar](200) NULL,
[Year] [smallint] NULL,
[Premiere] [smalldatetime] NULL,
[PremiereDisplay] AS (isnull(CONVERT([varchar],[Premiere],(104)),CONVERT([varchar],isnull([Year],(1985))))),
[Country] [varchar](50) NULL,
[Addition] [varchar](200) NULL,
[TitleArticleLess] AS ((([Name]+isnull((' ('+[NameTranslat])+')',''))+isnull((' ['+[Addition])+']',''))+isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),'')),
[TitleBase] AS ((((isnull([Article]+' ','')+[Name])+isnull((' ('+[NameTranslat])+')',''))+isnull((' ['+[Addition])+']',''))+isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),'')) PERSISTED,
[TitleYear] AS (((((isnull([Article]+' ','')+[Name])+isnull((' ('+[NameTranslat])+')',''))+isnull((' ['+[Addition])+']',''))+isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),''))+isnull(' · '+CONVERT([varchar],[Year],(104)),'')),
[TitleCountry] AS (((((isnull([Article]+' ','')+[Name])+isnull((' ('+[NameTranslat])+')',''))+isnull((' ['+[Addition])+']',''))+isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),''))+isnull(' · '+[Country],'')),
[TitleYearCountry] AS ((((((isnull([Article]+' ','')+[Name])+isnull((' ('+[NameTranslat])+')',''))+isnull((' ['+[Addition])+']',''))+isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),''))+isnull(' · '+CONVERT([varchar],[Year],(104)),''))+isnull(' · '+[Country],'')),
[ShowTitleArticleLess] AS (isnull(([Name]+isnull((' ('+[NameTranslat])+')',''))+isnull((' ['+[Addition])+']',''),'')),
[ShowPart] AS (isnull(((', '+isnull([PartName],'часть'))+' ')+CONVERT([varchar],[Part]),'')),
[Cover] [image] NULL,
[Note] [text] NULL,
[NotWatch] [bit] NULL,
[LastWatch] [datetime] NULL,
[LastWatchDaysAgo] AS (datediff(day,[LastWatch],getdate())),
[DateWatch] [datetime] NULL,
[WatchDisplay] AS ((isnull('+ '+CONVERT([varchar],[DateWatch],(104)),'')+isnull(case when [DateWatch] IS NULL then CONVERT([varchar],[LastWatch],(104)) else (' ('+CONVERT([varchar],[LastWatch],(104)))+')' end,''))+case when NOT [NotWatch] IS NULL then ' x' else '' end),
[PauseWatch] [datetime] NULL,
[Range] AS (((((isnull(CONVERT([varchar](4),[Year]),'1895')+format(isnull([Series],(0)),'d2'))+format(isnull([Season],(0)),'d2'))+format(isnull([Episode],(0)),'d2'))+format(isnull([Part],(0)),'d2'))+isnull(upper([Name]),'')),
[SearchValue] AS (replace(rtrim([Name]),' ','+')),
[Icon] AS (CONVERT([bit],isnull(datalength([Note]),(0)),(0))+CONVERT([bit],isnull(datalength([Cover]),(0)),(0))*(2)),
[AttributeIcon] [bit] NULL,
[Key] AS ('F'+CONVERT([varchar],[ID])),
CONSTRAINT [PK_Film] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Film] ADD CONSTRAINT [DF_Film_AttributeIcon] DEFAULT ((0)) FOR [AttributeIcon]
GO
ALTER TABLE [dbo].[Film] WITH CHECK ADD CONSTRAINT [FK_Film_Film] FOREIGN KEY([Heading])
REFERENCES [dbo].[Film] ([ID])
GO
ALTER TABLE [dbo].[Film] CHECK CONSTRAINT [FK_Film_Film]
GO