使用cursor循环和Insert语句

时间:2017-09-07 10:29:09

标签: sql sql-server-2012

我有一个insert语句,其中包含一个子查询,用于确定用作Identity的列中的下一个值,但遗憾的是nvarchar(20)类型(顺便说一下,不是我的db)。

我需要能够为AssetNo字段生成下一个增量编号。我可以通过强制转换为数字来确定下一个数字,但是当插入多个记录时,它们都会获得下一个值。因此,我假设我需要添加光标,但对这些光标几乎没有经验。

我的代码如下:

BEGIN
    INSERT INTO tbAsset
    (
        AssetUID,
        AssetNo,
        LocationParentID,
        LocationID,
        LocationDescription,
        Quantity,
        UnitTypeID,
        ElementID,
        LifeExpectancy,
        InstalledDate,
        OutsideHour,
        Status,
        BookingResource,
        IsSystem,
        IsPartOfSystem,
        AssetClassID,
        LastUpdate,
        LastUpdateUser,
        SystemConfidence,
        UserConfidence,
        IsOriginalComponent,
        ClientCode,
        ApplicationResource
    )
           SELECT
               NEWID() AS Expr1,
           (
               SELECT
                   MAX(CASE
                           WHEN ISNUMERIC(AssetNo) = 1
                           THEN CAST(AssetNo AS INT)
                           ELSE NULL
                       END) + 1 AS 'AssetNo'
               FROM tbAsset
           ) AS Expr2,
               Template_Structure_Location.Location_Type,
               Template_Structure_Location.Client_Location_Ref,
               Template_Structure_Location.Location,
               Template_Survey.Quantity,
               tbUnitType.UnitTypeID,
               dbo_Item.CLIENT_ITEM_REF,
               dbo_ProcedureX.LIFE_EXPECTANCY,
               NULL AS Expr3,
               '0' AS Expr4,
               '0' AS Expr5,
               '0' AS Expr6,
               '0' AS Expr7,
               '0' AS Expr8,
               '2' AS Expr9,
               GETDATE() AS Expr10,
               'PDS' AS Expr11,
               '100' AS Expr12,
               '100' AS Expr13,
               '0' AS Expr14,
               '0' AS Expr15,
               '0' AS Expr16
           FROM dbo_Item
           INNER JOIN Template_Survey ON dbo_Item.Itemref = Template_Survey.ItemRef
           INNER JOIN Template_Structure_Location ON Template_Survey.TEMPLATE_STRUCTURE_LOCATION_REF = Template_Structure_Location.TEMPLATE_STRUCTURE_LOCATION_REF
           INNER JOIN Template_Structure ON Template_Structure_Location.TEMPLATE_STRUCTURE_REF = Template_Structure.TEMPLATE_STRUCTURE_REF
           INNER JOIN Template_Details ON Template_Structure.TEMPLATE_DETAILS_REF = Template_Details.TEMPLATE_DETAILS_REF
                                          AND Template_Structure.TEMPLATE_DETAILS_REF = Template_Details.TEMPLATE_DETAILS_REF
           INNER JOIN dbo_ProcedureX ON dbo_Item.Itemref = dbo_ProcedureX.Itemref
           INNER JOIN tbUnitType ON dbo_Item.IPF_Element = tbUnitType.UnitDescription
           LEFT OUTER JOIN tbAsset AS tbAsset_1 ON Template_Survey.K2ref = tbAsset_1.AssetNo
           WHERE(Template_Details.Status = N'Signed Off')
                AND (Template_Details.StatusME = N'Signed Off')
                AND (Template_Survey.K2ref IS NULL)
                AND (dbo_ProcedureX.Replacement = 1);
END;

1 个答案:

答案 0 :(得分:0)

让我们把它减少到一个基本的假设:

insert into t2 (ID, Descrip, AssetNo)
select ID,
       Descrip,
       (
       SELECT MAX(AssetNo) + 1 from T1
       )
from T1

现在,让我们尝试解决这个更简单的问题(如果您的DBMS支持CTE和窗口函数):

with CTE as
(
select ID, Descrip, row_number() over(order by ID) as RN
from T1
)
insert into t2 (ID, Descrip, AssetNo)
select ID, Descrip, (select max(AssetNo) from T1) + RN
from CTE