Stored procedures 存储过程游标中出错

Stored procedures 存储过程游标中出错,stored-procedures,cursor,Stored Procedures,Cursor,在我的存储过程中,我声明了Have foll。光标。但它在两者之间给出了错误 DECLARE insert_cursor CURSOR FOR select o.orderID, o.paymenttype, o.noOfInstallment, o.installmentPaid, p.package_id_pk, p.price from order_master as o inner join package_master as p ON p.packag

在我的存储过程中,我声明了Have foll。光标。但它在两者之间给出了错误

DECLARE insert_cursor CURSOR FOR 
    select o.orderID, o.paymenttype, o.noOfInstallment, o.installmentPaid,
    p.package_id_pk, p.price
    from order_master as o
    inner join package_master as p ON p.package_id_pk = o.packageID
    where  o.noOfInstallment > 0;

OPEN insert_cursor;
FETCH NEXT FROM insert_cursor
INTO @orderID,@paymenttype,@noOfInstallment,@installmentPaid,@package_id_pk,@price;

WHILE @@FETCH_STATUS = 0
BEGIN
    if(@price = 1)
    begin           
        Declare @paymentID as numeric(18,0), @createdDate datetime
        select @paymentID = paymentID, @createdDate = createdDate from payment_cusotmer where orderID = @orderID
        if(@paymentID is not null)
        begin   
            Declare @pk_free numeric(18,0)          
            set @pk_free = dbo.getUniqueID()

            insert into Order_Installments(ID,orderID,installment_price,duedate,isPaid,createdDate)
            values(@pk_free, @orderID, @price, null, 1,@createdDate)

            update payment_cusotmer set orderInstallment_ID = @pk_free where paymentID = @paymentID
        end         
    end
    else
    begin
        -- 1 = full ; 2 = part payment
        if(@paymenttype = 1)
        begin
        Declare @paymentID_full as numeric(18,0), @createdDate_full datetime,@instDate_full datetime, @amountPaid numeric(18,2),@AmountToPay numeric(18,2), @paidStatus int

        select @paymentID_full = paymentID, @instDate_full = InstDate, @createdDate_full = CreatedDate,
        @paidStatus = status ,@AmountToPay = AmountToPay, @amountPaid=AmountPaid
        from payment_cusotmer where orderID = @orderID

        -- @paidStatus : 1=paid, 2=not paid

        if(@paymentID_full is not null)
        begin   
            Declare @pk_full numeric(18,0)          
            set @pk_full = dbo.getUniqueID()

            insert into Order_Installments(ID,orderID,installment_price,duedate,isPaid,createdDate)
            values(@pk_full, @orderID, @price, @instDate_full, @paidStatus,@createdDate_full)

            update payment_cusotmer set orderInstallment_ID = @pk_full where paymentID = @paymentID
        end 
        end
        else if(@paymenttype = 2)
        begin
            Declare @cnt_invoices int, @cnt_invoices_temp int,@oneTime_payment numeric(18,0), @noOfInstallment_temp int
            select @cnt_invoices = COUNT(PaymentID) from Payment_Cusotmer where  OrderID=@orderID

            Declare @paymentID_part as numeric(18,0), @createdDate_part datetime,@instDate_part datetime, @amountPaid_part numeric(18,2),@AmountToPay_part numeric(18,2), @paidStatus_part int

            set @oneTime_payment = @price/@noOfInstallment;             
            set @cnt_invoices_temp = @cnt_invoices
            while(@cnt_invoices > 0)
            begin
                select @paymentID_part = paymentID, @instDate_part = InstDate, @createdDate_part = CreatedDate,
                @paidStatus_part = status ,@AmountToPay_part = AmountToPay, @amountPaid_part =AmountPaid
                from payment_cusotmer where orderID = @orderID

                -- @paidStatus : 1=paid, 2=not paid

                if(@paymentID_part is not null)
                begin   
                    Declare @pk_part numeric(18,0)
                    set @pk_part = dbo.getUniqueID()

                    insert into Order_Installments(ID,orderID,installment_price,duedate,isPaid,createdDate)
                    values(@pk_part, @orderID, @AmountToPay_part, @instDate_part, @paidStatus_part,@createdDate_part)

                    update payment_cusotmer set orderInstallment_ID = @pk_part where paymentID = @paymentID_part
                end 
                set @cnt_invoices = @cnt_invoices - 1;
            end
            Declare @cnt_remaining_installments int
            set @cnt_remaining_installments = @noOfInstallment - @cnt_invoices;
            if(@cnt_remaining_installments > 0)
            begin
                set @cnt_invoices_temp = @cnt_remaining_installments
                while(@cnt_invoices_temp > 0)
                begin
                    --@oneTime_payment
                    Declare @pk_part_new numeric(18,0)
                    set @pk_part_new = dbo.getUniqueID()

                    insert into Order_Installments(ID,orderID,installment_price,duedate,isPaid,createdDate)
                    values(@pk_part_new, @orderID, @oneTime_payment, null, 0,GETDATE()) 
                    set @cnt_invoices_temp = @cnt_invoices_temp - 1;
                end
            end
        end
    end
    FETCH NEXT FROM insert_cursor
    INTO @orderID,@paymenttype,@noOfInstallment,@installmentPaid,@package_id_pk,@price;
END
CLOSE insert_cursor;
DEALLOCATE insert_cursor; 
错误:

违反主键约束“主键顺序分期付款”。不能 在对象“dbo.Order\u分期付款”中插入重复键

注意:我有一个sql函数
GetuniqueID()
,它将返回
uniqueID

我认为问题不在于上述功能。问题在于局部变量的范围。

SQL函数GetUniqueID()
payment\u cusotmer
是这样吗?问题是您试图在已经存在的
Order\u分期付款
表中插入一个值。因此,可能您的
GetUniqueID()
函数没有真正返回唯一ID,或者您有其他问题。试着找到插入数据的位置,并理解为什么会发生这种情况……@vulkanino:是的,这是对的。@marc_s:请再看一次帖子。我只是添加了getuniqueID()。@Abhi:你认为这真的是一个被返回的唯一ID。。。。如果两个连接同时调用此函数会怎么样??他们将得到相同的ID-非常不唯一!目前唯一可靠的方法是拥有一个
IDENTITY
列,由SQL Server自行处理并保证ID值的唯一性。。。。。
select @RandomMilliSecond= convert(nvarchar,datepart(ms,theDATE)) from v_getdate
select @RandomSecond=convert(nvarchar,datepart(ss,theDATE)) from v_getdate 
select @RandomYear=convert(nvarchar,datepart(yy,theDATE)) from v_getdate 
select @RandomMonth=convert(nvarchar,datepart(mm,theDATE)) from v_getdate 
select @RandomDay=convert(nvarchar,datepart(dd,theDATE)) from v_getdate
select @RandomHr=convert(nvarchar,datepart(hh,theDATE)) from v_getdate 
select @RandomMin=convert(nvarchar,datepart(mi,theDATE)) from v_getdate 
set @TMpReturnValue = @RandomYear + @RandomMonth
set @TMpReturnValue = @TMpReturnValue + @RandomDay + @RandomHr
set @TMpReturnValue = @TMpReturnValue + @RandomMin  + @RandomSecond
set @TMpReturnValue = @TMpReturnValue + @RandomMilliSecond
set @ReturnValue = @TMpReturnValue
return @ReturnValue