2012-03-07 41 views
1

我有問題插入記錄關於身份的特定表。但SQL總是告訴我,我需要打開identity_insert,但我已經爲該表設置了標識列,以便我確信它不會生成多個標識。順便說一句,我的表已經有記錄。這裏是我的代碼身份列不工作

declare @empid int 
declare @trans_name varchar(max) = 'Append' 
declare @lname varchar(max) 
declare @fname varchar(max) 
declare @mname varchar(max) 
declare @emp_id varchar(max) 
declare @gender varchar(max) 
declare @bday datetime 
declare @allowance numeric(18,2) 
declare @emp_sysid numeric(18,0) 
declare @datehired datetime 
declare @status varchar(max) 
declare @positionid numeric(18,0) 

declare cc_cur cursor for 
    select emps.LNAME, emps.MNAME, emps.FNAME, emps.EMP_ID, 
    emps.GENDER, emps.BDAY, emps.ALLOWANCE, emps.SYSID, 
    emps.DATE_HIRED, emps.[STATUS], emps.POSITION_SYSID 
    from SPADA.dbo.M_EMPLOYEE emps 
     where emps.SYSID not in (select ISNULL(B.SPADAEmpID, 0) from SPADA_FIS.dbo.HRIS_Employees B 
           inner join SPADA_FIS.dbo.HRIS_EmployeeStatus C 
            on C.EmployeeID = B.EmployeeID where C.IsCurrent = 1) 

open cc_cur 
fetch next from cc_cur 
into @lname, @mname, @fname, @emp_id, 
    @gender, @bday, @allowance, @emp_sysid, 
    @datehired, @status, @positionid 

begin tran @trans_name 
    while @@FETCH_STATUS = 0 
    begin 
     insert into SPADA_FIS.dbo.HRIS_Employees 
     (LastName, MiddleName, FirstName, OtherName, 
     Gender, BirthDate, Allowance) 
     values 
     (@lname, @mname, @fname, @emp_id, 
     @gender, @bday, @allowance) 

     set @empid = SCOPE_IDENTITY() 

     insert into SPADA_FIS.dbo.HRIS_EmployeeStatus 
     (EmployeeID, EmploymentTypeID, DepartmentID, EmploymentTenureID, 
     Remarks, DateHired, JobOrganizationID, EmployeeStatusID, IsCurrent, 
     PayFrequencyID, TaxExemptionStatus) 
     values 
     (@empid, 1, 6, (select htet.EmploymentTenureID 
           from SPADA_FIS.dbo.HRIS_tblEmploymentTenures htet 
            where htet.Tenure = @status), 
     'Migrated Data', @datehired, (select htjo.JobOrganizationID from SPADA_FIS.dbo.HRIS_tblJobOrganizations htjo 
              where htjo.Position = (select pos.POSITION from SPADA.dbo.M_POSITION pos 
               where pos.SYSID = @positionid) or htjo.Position = 
                (select pos2.[DESCRIPTION] from SPADA.dbo.M_POSITION pos2 
                 where pos2.SYSID = @positionid)), 
     1, 1, 2, 2) 
     fetch next from cc_cur 
     into @lname, @mname, @fname, @emp_id, 
      @gender, @bday, @allowance, @emp_sysid, 
      @datehired, @status, @positionid 
    end 
    close cc_cur 
    deallocate cc_cur 
if @@ERROR <> 0 
rollback tran @trans_name 
commit tran @trans_name 

如果我設置IDENTITY_INSERT對..標識列僅產生1。

值這是我的表結構

USE [SPADA_FIS] 
GO 

/****** Object: Table [dbo].[HRIS_EmployeeStatus] Script Date: 03/07/2012 17:13:03 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

SET ANSI_PADDING ON 
GO 

CREATE TABLE [dbo].[HRIS_EmployeeStatus](
    [EmployeeStatusID] [int] IDENTITY(1,1) NOT NULL, 
    [EmployeeID] [int] NULL, 
    [EmploymentTypeID] [int] NULL, 
    [DepartmentID] [int] NULL, 
    [EmploymentTenureID] [int] NULL, 
    [Remarks] [varchar](max) NULL, 
    [DateHired] [datetime] NULL, 
    [JobOrganizationID] [int] NULL, 
    [SectionID] [int] NULL, 
    [EmploymentStatusID] [int] NULL, 
    [IsCurrent] [bit] NULL, 
    [StartDate] [datetime] NULL, 
    [IsUnionMember] [bit] NULL, 
    [EndDate] [datetime] NULL, 
    [PayFrequencyID] [int] NULL, 
    [TaxExemptionStatus] [bigint] NULL, 
    [IDNumber] [varchar](50) NULL, 
    [BiometricNumber] [varchar](50) NULL, 
    [BankAccountNumber] [varchar](50) NULL, 
    [CreatedBy_UserID] [int] NULL, 
    [CreatedDate] [datetime] NULL, 
    [UpdatedBy_UserID] [int] NULL, 
    [UpdatedDate] [datetime] NULL, 
CONSTRAINT [PK_HRIS_EmployeeStatus] PRIMARY KEY CLUSTERED 
(
    [EmployeeStatusID] 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 

SET ANSI_PADDING OFF 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_EmployeeID] DEFAULT ((0)) FOR [EmployeeID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_EmploymentTypeID] DEFAULT ((0)) FOR [EmploymentTypeID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_DepartmentID] DEFAULT ((0)) FOR [DepartmentID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_EmploymentTenureID] DEFAULT ((0)) FOR [EmploymentTenureID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_DateHired] DEFAULT (getdate()) FOR [DateHired] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_JobOrganizationID] DEFAULT ((0)) FOR [JobOrganizationID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_SectionID] DEFAULT ((0)) FOR [SectionID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_EmploymentStatusID] DEFAULT ((0)) FOR [EmploymentStatusID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_IsCurrent] DEFAULT ((1)) FOR [IsCurrent] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_StartDate] DEFAULT (getdate()) FOR [StartDate] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_IsUnionMember] DEFAULT ((0)) FOR [IsUnionMember] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_EndDate] DEFAULT (getdate()) FOR [EndDate] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_PayFrequencyID] DEFAULT ((0)) FOR [PayFrequencyID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_TaxExemptionStatus] DEFAULT ((0)) FOR [TaxExemptionStatus] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_CreatedBy_UserID] DEFAULT ((0)) FOR [CreatedBy_UserID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_CreatedDate] DEFAULT (getdate()) FOR [CreatedDate] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_UpdatedBy_UserID] DEFAULT ((0)) FOR [UpdatedBy_UserID] 
GO 

ALTER TABLE [dbo].[HRIS_EmployeeStatus] ADD CONSTRAINT [DF_HRIS_EmployeeStatus_UpdatedDate] DEFAULT (getdate()) FOR [UpdatedDate] 
GO 
+0

在插入時沒有提及'@ emp_id',只需命名所有其他列,然後將自動創建ID值。 – Seph 2012-03-07 08:59:41

+0

只有在您想顯式爲標識列提供值時,才需要啓用Identity_insert。通常你想讓它自動分配。你還沒有提供架構來說明你正在嘗試在標識列中插入值的標識列 – kaj 2012-03-07 09:00:23

+1

,這就是爲什麼它正在發生 – Nighil 2012-03-07 09:01:14

回答

3

那是你的問題:你有:

CREATE TABLE [dbo].[HRIS_EmployeeStatus](
    [EmployeeStatusID] [int] IDENTITY(1,1) NOT NULL, 

,並在您INSERT聲明,您要的東西插入到該列:

insert into SPADA_FIS.dbo.HRIS_EmployeeStatus 
     (EmployeeID, EmploymentTypeID, DepartmentID, EmploymentTenureID, 
     Remarks, DateHired, JobOrganizationID, EmployeeStatusID, IsCurrent, 
               ***************** 
     PayFrequencyID, TaxExemptionStatus) 

不要那樣做! IDENTITY列將由SQL Server自身處理。請從INSERT聲明中刪除該列(以及VALUES集合中的值),並且您應該沒問題。

+1

比我早4秒:) – b0rg 2012-03-07 09:33:52

1

看來你是試圖在標記爲'identity'列的列中插入一個值。因此你會得到錯誤。插入新行時,服務器會自動生成標識列值。如果你想明確地插入值,則需要將「identity_insert」屬性設置爲「真」這樣的列

0

啊,好老的人力資源數據庫,幾乎像教科書中......你會因爲它的定義爲

[EmployeeStatusID] [int] IDENTITY(1,1) NOT NULL, 

所以將需要刪除「EmployeeStatusId」從

insert into SPADA_FIS.dbo.HRIS_EmployeeStatus... 

在插入時自動生成。

0

爲已經提供的答案並不明確地爲員工提供StatusId

的值作爲單獨的點你應該砸EmployeeStatusId

默認約束

你不應該能夠創建一個默認約束一個標識列...

相關問題