存储过程游标中的错误

时间:2012-02-28 09:22:39

标签: stored-procedures cursor

在我的存储过程中,我声明了有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; 

错误:

  

违反PRIMARY KEY约束' PK_Order_Installments'。不能   在对象' dbo.Order_Installments'。

中插入重复键

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

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

SQL函数GetUniqueID()

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

0 个答案:

没有答案