有没有办法做到这一点,不重复?

时间:2017-07-16 01:12:48

标签: sql sql-server sql-server-2008

我按代码(PROYECT_A A00001)

处理项目

有3个表格(SEGMENT SE00001,LABLE LA00001,STRONG ST00001)与项目相关,这些表格可能会有重复的代码,但它们只属于某个项目

有没有办法不重复这些字段?

CREATE TABLE [dbo].[PROYECT_A](
    [PROYECT_A] [int] IDENTITY(1,1) NOT NULL,
    [CODE_A]  AS ('A'+right('00000'+CONVERT([varchar],[PROYECT_A],(0)),(5))),
    [STATUS] [datetime] NOT NULL,
    [CREATION_DATE] [datetime] NOT NULL,
 CONSTRAINT [PK_RR] PRIMARY KEY CLUSTERED 
(
    [CODE_A] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

CREATE TABLE [dbo].[SEGMENT](
    [ID_SEGMENT] [int] IDENTITY(1,1) NOT NULL,
    [CODE_SE]  AS ('SE'+right('00000'+CONVERT([varchar],[ID_SEGMENT],(0)),(5))),
    [NAME] [varchar](20),
    [POWW] [varchar](20),
    [SQWE] [varchar](20),
    [DATESTART] [datetime] NOT NULL,
    [DATESTART] [datetime] NOT NULL
    [CODE_A] [varchar](20)
GO


CREATE TABLE [dbo].[LABLE](
    [ID_LABLE] [int] IDENTITY(1,1) NOT NULL,
    [CODE_LA]  AS ('LA'+right('00000'+CONVERT([varchar],[ID_LABLE],(0)),(5))),
    [NAME] [varchar](20),
    [POWW] [varchar](20),
    [SQWE] [varchar](20),
    [DATESTART] [datetime] NOT NULL,
    [DATESTART] [datetime] NOT NULL
    [CODE_A] [varchar](20)
GO

CREATE TABLE [dbo].[STRONG](
    [ID_STRONG] [int] IDENTITY(1,1) NOT NULL,
    [CODE_ST]  AS ('ST'+right('00000'+CONVERT([varchar],[ID_STRONG],(0)),(5))),
    [NAME] [varchar](20),
    [POWW] [varchar](20),
    [SQWE] [varchar](20),
    [DATESTART] [datetime] NOT NULL,
    [DATESTART] [datetime] NOT NULL
    [CODE_A] [varchar](20)
GO

1 个答案:

答案 0 :(得分:1)

创建一个包含Type列的表格,以区分记录是否属于SEGMENTLABLESTRONG

CREATE TABLE [dbo].[SEGMENT](
    [ID_SEGMENT] [int] IDENTITY(1,1) NOT NULL,
    [Type] as Varchar(15) -- Here `SEGMENT`,`LABLE` or `STRONG`
    [CODE_SE]  AS (case type 
                     when 'Segment' then 'SE' 
                     when 'LABLE' then 'LA' 
                     else 'ST' 
                   end +right('00000'+CONVERT([varchar],[ID_SEGMENT],(0)),(5))),
    [NAME] [varchar](20),
    [POWW] [varchar](20),
    [SQWE] [varchar](20),
    [DATESTART] [datetime] NOT NULL,
    [DATESTART] [datetime] NOT NULL
    [CODE_A] [varchar](20)
    )