插入不适用于光标

时间:2017-09-04 12:46:18

标签: sql sql-server

我遇到问题,我的Insert声明不起作用。我创建了临时表,然后插入语句基于此临时表中的两列工作。

数据存在于临时表中,但插入不起作用,没有出现错误。提前致谢

DECLARE JPCUSTOMER_CURSOR CURSOR FOR 
    SELECT
        cu.CustomerNo, jp.ID 
    FROM
        CUSTOMERNO# cu, SalesmanNo# sa, JourneyPlan JP
    WHERE
        cu.OCCURRENCE = sa.OCCURRENCE
        AND jp.AssignedTO = sa.SalesmanNo

OPEN JPCUSTOMER_CURSOR

FETCH NEXT FROM JPCUSTOMER_CURSOR INTO @CUST_ID,@JP_ID

WHILE (@@FETCH_STATUS = 0)
BEGIN  
    INSERT INTO [JPCustomer] ([CustomerID ], [JPID], [Frequency], [StartWeek], 
                              [sat], [sun], [mon], [tue], [wed], [thu], [fri],
                              [VisitOrder], [ModifiedOn], [ModifiedBy],
                              [CreatedOn], [Createdby],
                              [RecordSource], [IsPotential])
    VALUES (@CUST_ID, @JP_ID, 4, 1, 
            1, 1, 1, 1, 1, 1, 1, 
            NULL, NULL, NULL,  
            NULL, NULL, 0, 0)
END   

CLOSE JPCUSTOMER_CURSOR;  
DEALLOCATE JPCUSTOMER_CURSOR;  

2 个答案:

答案 0 :(得分:2)

您不需要光标来处理您正在做的事情。这会快得多:

INSERT INTO [JPCustomer] ([CustomerID ],
                           [JPID],
                           [Frequency],
                           [StartWeek],
                           [sat],
                           [sun],
                           [mon],
                           [tue],
                           [wed],
                           [thu],
                           [fri],
                           [VisitOrder],
                           [ModifiedOn],
                           [ModifiedBy],
                           [CreatedOn],
                           [Createdby],
                           [RecordSource],
                           [IsPotential])
select cu.CustomerNo,
       jp.ID,
        4,
        1,
        1,
        1,
        1,
        1,
        1,
        1,
        1,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        0,
        0 
from CUSTOMERNO# cu
join SalesmanNo# sa on cu.OCCURRENCE = sa.OCCURRENCE
join JourneyPlan JP on jp.AssignedTO = sa.SalesmanNo

答案 1 :(得分:1)

您不需要也不应该使用光标。只是一个INSERT ... SELECT。 EG

with q as
(
    select cu.CustomerID CUST_ID, jp.ID JPID
    from CUSTOMERNO# cu
    join SalesmanNo# sa
      on cu.OCCURRENCE = sa.OCCURRENCE
    join JourneyPlan JP
      on jp.AssignedTO = sa.SalesmanNo

)
INSERT INTO [JPCustomer] ([CustomerID],
                           [JPID],
                           [Frequency],
                           [StartWeek],
                           [sat],
                           [sun],
                           [mon],
                           [tue],
                           [wed],
                           [thu],
                           [fri],
                           [VisitOrder],
                           [ModifiedOn],
                           [ModifiedBy],
                           [CreatedOn],
                           [Createdby],
                           [RecordSource],
                           [IsPotential])
SELECT  CustomerID,
        JPID,
        4,
        1,
        1,
        1,
        1,
        1,
        1,
        1,
        1,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        0,
        0
from q;