其中一个单元格中有三个以上的数据是位格式

There are more than three data in one of the cells that are bit formats

我需要在 SQL 中有最多 Select 3 个公司类型 并使用 check constrain。 这个怎么弄,或者我也接受其他的建议,对于下面的table:

CREATE TABLE Dbo.[CompanyType](
    [TypeID] [bigint] IDENTITY(1,1) NOT NULL,
    [Manufacturer] [bit] NULL,
    [Trading] [bit] NULL,
    [BuyingOffice] [bit] NULL,
    [Agent] [bit] NULL,
    [Wholesaler] [bit] NULL,
    [Commission] [bit] NULL,
    [Association] [bit] NOT NULL,
    [BusinessService] [bit] NOT NULL,
    [Other] [bit] NOT NULL,
    [Photo] [image] NULL,
    [CreateDate] datetime
 CONSTRAINT [PK_CompanyType] PRIMARY KEY CLUSTERED 
(
    [TypeID] 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.[CompanyType] ADD  CONSTRAINT [DF_Company_PersianTax]  DEFAULT ((9)) FOR [Taxpercent]
GO

ALTER TABLE dbo.[CompanyType] ADD  CONSTRAINT [DF_Company_CreateDate]  DEFAULT (getdate()) FOR [CreateDate]
GO
  1. 使用检查约束。

How to use check constrain for this model??

我试过:

  1. 同时我用SP插入。在那里检查三个或更多真实数据不是更好吗?在输入数据时作为参数检查
--parameter validation
if(@Manufacturer+@Trading+@BuyingOffice+@Agent+@Wholesaler+@Commission+@Association+@BusinessService+@Other)<= 3

Begin
  Return
End
else 

insert statement (...)
  1. 像下面的查询一样使用触发器!检查已插入,列总和 <= 3 rolback
CREATE TRIGGER usp_checksum3
   ON  dbo.companytype
   AFTER insert
AS 
BEGIN

    SET NOCOUNT ON;

     if     (Select [Manufacturer]+[Trading]+[BuyingOffice]+[Agent]+[Wholesaler]+[Commission]+[Association]+[BusinessService]+[Other] 
     From inserted) <= 3    

  Return

END

感谢您的提示

考虑使用以下检查约束:

CREATE TABLE Dbo.[CompanyType](
    [TypeID] [bigint] IDENTITY(1,1) NOT NULL,
    [Manufacturer] [bit] NULL,
    [Trading] [bit] NULL,
    [BuyingOffice] [bit] NULL,
    [Agent] [bit] NULL,
    [Wholesaler] [bit] NULL,
    [Commission] [bit] NULL,
    [Association] [bit] NOT NULL,
    [BusinessService] [bit] NOT NULL,
    [Other] [bit] NOT NULL,
    [Photo] [image] NULL,
    [CreateDate] datetime
    CONSTRAINT [PK_CompanyType] PRIMARY KEY CLUSTERED ([TypeID] 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],
    CONSTRAINT [PK_Company_Types] CHECK (
        CAST([Manufacturer] AS INT) 
        + CAST([Trading] AS INT) 
        + CAST([BuyingOffice] AS INT) 
        + CAST([Agent] AS INT)
        + CAST([Wholesaler] AS INT) 
        + CAST([Commission] AS INT)
        + CAST([Association] AS INT)
        + CAST([BusinessService] AS INT)
        + CAST([Other] AS INT)
    <= 3)

您不需要在存储过程中实现额外的逻辑。检查约束保证完整性,同样适用于从 SP 或外部执行的插入。

如果要修改现有的table:

ALTER TABLE Dbo.[CompanyType] 
ADD CONSTRAINT [Chk_CompanyType] CHECK (
    CAST([Manufacturer] AS INT) 
    + CAST([Trading] AS INT) 
    + CAST([BuyingOffice] AS INT) 
    + CAST([Agent] AS INT)
    + CAST([Wholesaler] AS INT) 
    + CAST([Commission] AS INT)
    + CAST([Association] AS INT)
    + CAST([BusinessService] AS INT)
    + CAST([Other] AS INT)
<= 3)