代码增加SQL Server 2017中插入语句中的标识列的问题

时间:2018-09-21 16:33:59

标签: sql sql-server sql-server-2017

我读了一篇关于在具有标识列的表中插入行的文章,但是它不起作用。这是我缺少的代码?当我运行它时,出现以下错误消息:

Cannot insert the value NULL into column 'PP_ID', table 
Med.dbo.tblPledgePayments column does not allow nulls. INSERT fails.
The statement has been terminated.

如果将标识列添加到插入语句(PP_ID),则表具有十五(15)行,而不是添加下一个逻辑数十六(16),而是添加了从一(1)开始的行。我该如何克服?

DECLARE @TxtHowMuch as int,
    @TxtHowMany as int,
    @TxtPledgeStartDt as datetime,
    @TxtFrequency as nvarchar(50), -- Yearly, Monthly, Qurterly
    @PaymentNumber as int,
    @GID as int,
    @cnt INT = 0                   --Constant


--These values will be brought in from Access
SET @TxtHowMuch = 100000
SET @TxtHowMany = 10
SET @TxtPledgeStartDt = '2018-01-01'
SET @TxtFrequency = 'Yearly'
SET @PaymentNumber = 1
SET @GID = 200

While @cnt < @TxtHowMany
BEGIN
INSERT INTO [dbo].[tblPledgePayments] 
(PP_schedule, GiftID, PP_DueDate, PP_Amount)

Select 
 @PaymentNumber, @GID,

Case
    WHEN @TxtFrequency = 'Yearly'    THEN DateAdd(YEAR,    @cnt, @TxtPledgeStartDt)
    WHEN @TxtFrequency = 'Monthly'   THEN DateAdd(MONTH,   @cnt, @TxtPledgeStartDt)
    WHEN @TxtFrequency = 'Quarterly' THEN DateAdd(QUARTER, @cnt, @TxtPledgeStartDt)
END 

,@TxtHowMuch / @TxtHowMany   

SET @cnt = @cnt + 1;
SET @PaymentNumber = @PaymentNumber + 1;

END;

0 个答案:

没有答案