0

我正在创建一个带有自动生成employee id前缀的简单表

sale_900000,sale_900001,sale_900002,sale_900003 

它是一个自引用表。

当我将数据插入表中时,会出现如下错误:

*消息 547,级别 16,状态 0,过程 tr_generate_emp_id,第 42 行
INSERT 语句与 FOREIGN KEY SAME TABLE 约束“Registration_Registration”冲突。冲突发生在数据库“test”、表“dbo.Registration”、列“empid”中。*

对于自动员工 ID,我instead of insert在桌子上使用触发器

这是我的桌子

CREATE TABLE [dbo].[Registration](
    [empid] [varchar](40) NOT NULL,
    [id] [int] IDENTITY(900000,1) NOT NULL,
    [First_Name] [varchar](40) NULL,
    [Last_Name] [varchar](40) NULL,
    [Address] [varchar](40) NULL,
    [E_Mail] [varchar](40) NULL,
    [Country] [varchar](40) NULL,
    [Mobile_No] [varchar](40) NULL,
    [Designation] [varchar](40) NULL,
    [managerID] [varchar](40) NULL,
 CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED 
(
    [empid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[Registration]  WITH CHECK ADD  CONSTRAINT [Registration_Registration] FOREIGN KEY([managerID])
REFERENCES [dbo].[Registration] ([empid])

这是我的触发器

ALTER TRIGGER [dbo].[tr_generate_emp_id] ON [dbo].[Registration]
    INSTEAD OF INSERT
AS
    BEGIN
        DECLARE @id INT
        DECLARE @id1 INT
        DECLARE @id_s VARCHAR(50)

        DECLARE @empid VARCHAR(50)
        DECLARE @First_Name VARCHAR(50)
        DECLARE @Last_Name VARCHAR(50)
        DECLARE @Address VARCHAR(50)
        DECLARE @E_Mail VARCHAR(50)
        DECLARE @Country VARCHAR(50)
        DECLARE @Mobile_No VARCHAR(50)
        DECLARE @Designation VARCHAR(50)
        DECLARE @managerID VARCHAR(50)

            SELECT  @id = Id
            FROM   dbo.Registration
             IF @id IS NULL
             SET @id = 899999

              SET @id1 = @id + 1
           SELECt  @First_Name = First_Name ,
                    @Last_Name = Last_Name ,
                    @Address = Address ,
                    @E_Mail = E_Mail ,
                    @Country = Country ,
                    @Mobile_No = Mobile_No ,
                    @Designation = Designation ,
                    @managerID = managerID 

            FROM    INSERTED

              SET @id_s = CONVERT(VARCHAR(50), @id1)
              SET @empid = 'Sale_' + @id_s

             INSERT INTO dbo.Registration
                      ( empid ,
                        First_Name ,
                        Last_Name ,
                        Address ,
                        E_Mail ,
                        Country ,
                        Mobile_No ,
                        Designation ,
                        managerID 

                      )
              VALUES  ( @empid , -- empid - varchar(40)
                        @First_Name , -- First_Name - varchar(40)
                        @Last_Name  , -- Last_Name - varchar(40)
                        @Address , -- Address - varchar(40)
                        @E_Mail , -- E_Mail - varchar(40)
                        @Country, -- Country - varchar(40)
                        @Mobile_No , -- Mobile_No - varchar(40)
                        @Designation  , -- Designation - varchar(40)
                        @managerID -- managerID - varchar(40)
                      )
    END

我正在插入

INSERT INTO dbo.Registration
        ( empid ,
          First_Name ,
          Last_Name ,
          Address ,
          E_Mail ,
          Country ,
          Mobile_No ,
          Designation ,
          managerID
        )
VALUES  ( '' , -- empid - varchar(40)
          'admin' , -- First_Name - varchar(40)
          'jon' , -- Last_Name - varchar(40)
          's-24' , -- Address - varchar(40)
          'abc@gmail.com' , -- E_Mail - varchar(40)
          'india' , -- Country - varchar(40)
          '098735322211' , -- Mobile_No - varchar(40)
          'manager' , -- Designation - varchar(40)
          ''  -- managerID - varchar(40)
        )

和错误

Msg 547, Level 16, State 0, Procedure tr_generate_emp_id, Line 42
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "Registration_Registration". The conflict occurred in database "test", table "dbo.Registration", column 'empid'.
The statement has been terminated.
4

4 回答 4

1

主要问题是您首先要这样做。我刚刚通读了你的触发器。只是将 empid 更改为计算字段...

CREATE TABLE [dbo].[Registration](
    [empid] AS ('Sale_' + Convert(Varchar(50),id)) PERSISTED,
    [id] [int] IDENTITY(900000,1) NOT NULL,
    [First_Name] [varchar](40) NULL,
    [Last_Name] [varchar](40) NULL,
    [Address] [varchar](40) NULL,
    [E_Mail] [varchar](40) NULL,
    [Country] [varchar](40) NULL,
    [Mobile_No] [varchar](40) NULL,
    [Designation] [varchar](40) NULL,
    [managerID] [varchar](40) NULL,
 CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED 
(
    [empid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
于 2013-03-20T15:50:34.197 回答
0

在你的触发器中,我认为这条线

SELECT  @id = Id

旨在阅读

SELECT  @id = MAX(Id)

我很惊讶您没有因此收到 PK 违规错误。FK 违规错误只能是由于尝试插入与 empid 值不匹配的 MangerId 值(据我从代码中看到的)。因此,请检查您提供的 managerId 值。

于 2013-03-20T15:52:31.233 回答
0
  1. 检查您的巢等级。

http://msdn.microsoft.com/en-us/library/ms182737(v=sql.100).aspx

我会将这个值放在 (dbo.) AuditTable 中,这与您的主表无关。trigger_nestlevel()

  1. 从基于行的逻辑更改为基于集的逻辑。
于 2013-03-20T15:52:42.047 回答
0

我重新设置了您的触发器,但我认为@Love2Learn 可能为您提供更好的解决方案。

ALTER TRIGGER [dbo].[tr_generate_emp_id] ON [dbo].[Registration]
    INSTEAD OF INSERT
AS
    BEGIN
    DECLARE @id INT

        -- Get the last id used.
        SELECT  @id = MAX(Id)
        FROM   dbo.Registration

        IF @id IS NULL
            SET @id = 899999

         WITH MyInserted AS (SELECT
                        ROW_NUMBER() OVER(ORDER BY E_Mail) AS rownum-- Email is just a random order by here.  Could be anything you want.
                        ,*  
                        FROM inserted)
         INSERT INTO dbo.Registration
                  ( empid ,
                    First_Name ,
                    Last_Name ,
                    Address ,
                    E_Mail ,
                    Country ,
                    Mobile_No ,
                    Designation ,
                    managerID 

                  )
         SELECT -- add the last id used with the rownum (ranges from 1 to num of rows)
                'Sale_'+CONVERT(VARCHAR(50), @id+rownum) -- empid - varchar(40)
                First_Name, -- First_Name - varchar(40)
                Last_Name, -- Last_Name - varchar(40)
                Address,  -- Address - varchar(40)
                E_Mail, -- E_Mail - varchar(40)
                Country, -- Country - varchar(40)
                Mobile_No, -- Mobile_No - varchar(40)
                Designation, -- Designation - varchar(40)
                ManagerID  -- managerID - varchar(40)
        FROM MyInserted
END
于 2013-03-20T15:57:44.503 回答