如何在SQL中的临时表中插入自动增量值

时间:2017-02-06 11:23:47

标签: sql stored-procedures row identity sql-server-2016

我有一个存储过程,我从用户界面接收序列号。存储过程将从表中选择记录并将它们插入到临时表中,该表的结构与原始表相同,但附加列除外。

此列将保存值"序号+ 1"。对于连续插入,此值将自动递增。我该怎么做?我希望保留自动递增值的列名是" REV_SEQ"

  ALTER PROCEDURE [PCC_APP_OWN].[Doc_GetBlChargePatternData] 
      @RevSeq INT NULL = 0,
      @TotalCount INT OUTPUT
  AS
  BEGIN
      DECLARE @DscgPort VARCHAR(7);
      DECLARE @tempRowId INT; 
      DECLARE @tempRevSeq INT; 

      SET @tempRevSeq = @RevSeq + 1;

      DECLARE @TempMenuResult AS TABLE (REV_SEQ INT,
                                        CHRG_PTRN_DTL_ID INT,
                                        UPDT_STAMP DATETIME,
                                        UPDT_USER VARCHAR(12),
                                        ORGL_STAMP DATETIME,
                                        ORGL_USER VARCHAR(12),
                                        DEL_FLG VARCHAR(1),
                                        CHRG_PTRN_HDR_ID INT,
                                        SEQ INT,
                                        CHRG_CODE VARCHAR(3),
                                        PORT_CODE VARCHAR(7),   
                                        BASIS VARCHAR(2),
                                        UM VARCHAR(2),
                                        RATE DECIMAL(8,2),
                                        CURRENCY VARCHAR(3),
                                        PER VARCHAR(2),
                                        PREPAID_COLLECT VARCHAR(1),
                                        INV_OFC VARCHAR(6)
                                       );   

    SELECT @DscgPort = DSCH_PORT 
    FROM BL 
    WHERE BL_NO_UID = 1 AND CA_SEQ = 0;

    INSERT INTO @TempMenuResult (REV_SEQ, CHRG_PTRN_DTL_ID, UPDT_STAMP, UPDT_USER, ORGL_STAMP, ORGL_USER, DEL_FLG, CHRG_PTRN_HDR_ID, SEQ, CHRG_CODE, PORT_CODE, BASIS, UM, RATE, CURRENCY, PER, PREPAID_COLLECT, INV_OFC)
        SELECT 
            IDENTITY(INT, @RevSeq, 1) AS id, * 
        FROM 
            CHRG_PTRN_DTL CHG_1 
        WHERE 
            CHG_1.CHRG_PTRN_HDR_ID = @ChargePtrnHdrId 
            AND CHG_1.PORT_CODE = @DscgPort 
            AND CHG_1.DEL_FLG = 'N'

        UNION

        SELECT 
            IDENTITY(INT, @RevSeq, 1) AS id, * 
        FROM 
            CHRG_PTRN_DTL CHG_2 
        WHERE 
            CHG_2.PORT_CODE IS NULL 
            AND CHG_2.DEL_FLG = 'N';

    SELECT @TotalCount = COUNT(1) OVER () FROM @TempMenuResult;
    SELECT * FROM @TempMenuResult ORDER BY REV_SEQ;
END

2 个答案:

答案 0 :(得分:1)

我认为通过将列声明为identity来更容易实现此目的:

DECLARE @TempMenuResult AS TABLE (REV_SEQ INT IDENTITY, . . .);

. . .

INSERT INTO @TempMenuResult (CHRG_PTRN_DTL_ID, UPDT_STAMP, UPDT_USER, ORGL_STAMP, ORGL_USER, DEL_FLG, CHRG_PTRN_HDR_ID, SEQ, CHRG_CODE, PORT_CODE, BASIS, UM, RATE, CURRENCY, PER, PREPAID_COLLECT, INV_OFC)
    SELECT *
    . . .;

然后你有这个陈述:

SELECT @TotalCount = COUNT(1) OVER () FROM @TempMenuResult;

这可能不会做你想要的。你想要简单的聚合,而不是窗口函数:

SELECT @TotalCount = COUNT(*) FROM @TempMenuResult;

答案 1 :(得分:0)

表变量无法实现动态身份种子。请改用Temp表。修改您的表创建如下。

    DECLARE @sql varchar(8000);
    SET @sql = 'DROP TABLE ##TempMenuResult; CREATE TABLE ##TempMenuResult (REV_SEQ INT IDENTITY('+ CAST(@tempRevSeq AS varchar(15)) + ', 1) NOT NULL ,
                                            CHRG_PTRN_DTL_ID INT,
                                            UPDT_STAMP DATETIME,
                                            UPDT_USER VARCHAR(12),
                                            ORGL_STAMP DATETIME,
                                            ORGL_USER VARCHAR(12),
                                            DEL_FLG VARCHAR(1),
                                            CHRG_PTRN_HDR_ID INT,
                                            SEQ INT,
                                            CHRG_CODE VARCHAR(3),
                                            PORT_CODE VARCHAR(7),   
                                            BASIS VARCHAR(2),
                                            UM VARCHAR(2),
                                            RATE DECIMAL(8,2),
                                            CURRENCY VARCHAR(3),
                                            PER VARCHAR(2),
                                            PREPAID_COLLECT VARCHAR(1),
                                            INV_OFC VARCHAR(6)
                                           );'
    EXEC (@sql);
    --Your code here to further process ##TempMenuResult

编辑:我没有您的数据库,但可以修改SP

ALTER PROCEDURE [PCC_APP_OWN].[Doc_GetBlChargePatternData] 
      @RevSeq INT NULL = 0,
      @TotalCount INT OUTPUT
  AS
  BEGIN
      DECLARE @DscgPort VARCHAR(7);
      DECLARE @tempRowId INT; 
      DECLARE @tempRevSeq INT; 

      SET @tempRevSeq = @RevSeq + 1;

      DECLARE @sql varchar(8000);
        SET @sql = 'DROP TABLE ##TempMenuResult; CREATE TABLE ##TempMenuResult (REV_SEQ INT IDENTITY('+ CAST(@tempRevSeq AS varchar(15)) + ', 1) NOT NULL ,
                                            CHRG_PTRN_DTL_ID INT,
                                            UPDT_STAMP DATETIME,
                                            UPDT_USER VARCHAR(12),
                                            ORGL_STAMP DATETIME,
                                            ORGL_USER VARCHAR(12),
                                            DEL_FLG VARCHAR(1),
                                            CHRG_PTRN_HDR_ID INT,
                                            SEQ INT,
                                            CHRG_CODE VARCHAR(3),
                                            PORT_CODE VARCHAR(7),   
                                            BASIS VARCHAR(2),
                                            UM VARCHAR(2),
                                            RATE DECIMAL(8,2),
                                            CURRENCY VARCHAR(3),
                                            PER VARCHAR(2),
                                            PREPAID_COLLECT VARCHAR(1),
                                            INV_OFC VARCHAR(6)
                                           );'
    EXEC (@sql);

    SELECT @DscgPort = DSCH_PORT 
    FROM BL 
    WHERE BL_NO_UID = 1 AND CA_SEQ = 0;

    INSERT INTO #TempMenuResult (CHRG_PTRN_DTL_ID, UPDT_STAMP, UPDT_USER, ORGL_STAMP, ORGL_USER, DEL_FLG, CHRG_PTRN_HDR_ID, SEQ, CHRG_CODE, PORT_CODE, BASIS, UM, RATE, CURRENCY, PER, PREPAID_COLLECT, INV_OFC)
        SELECT 
            * 
        FROM 
            CHRG_PTRN_DTL CHG_1 
        WHERE 
            CHG_1.CHRG_PTRN_HDR_ID = @ChargePtrnHdrId 
            AND CHG_1.PORT_CODE = @DscgPort 
            AND CHG_1.DEL_FLG = 'N'

        UNION

        SELECT 
            * 
        FROM 
            CHRG_PTRN_DTL CHG_2 
        WHERE 
            CHG_2.PORT_CODE IS NULL 
            AND CHG_2.DEL_FLG = 'N';

    SELECT @TotalCount = COUNT(1) OVER () FROM #TempMenuResult;
    SELECT * FROM #TempMenuResult ORDER BY REV_SEQ;
END
编辑:如果您不想使用动态SQL,请使用DBCC CHECKIDENT来重置。

ALTER PROCEDURE [PCC_APP_OWN].[Doc_GetBlChargePatternData] 
      @RevSeq INT NULL = 0,
      @TotalCount INT OUTPUT
  AS
  BEGIN
      DECLARE @DscgPort VARCHAR(7);
      DECLARE @tempRowId INT; 
      DECLARE @tempRevSeq INT; 

      SET @tempRevSeq = @RevSeq + 1;


      DROP TABLE ##TempMenuResult; 
      CREATE TABLE ##TempMenuResult (REV_SEQ INT IDENTITY(1, 1) NOT NULL ,
                                            CHRG_PTRN_DTL_ID INT,
                                            UPDT_STAMP DATETIME,
                                            UPDT_USER VARCHAR(12),
                                            ORGL_STAMP DATETIME,
                                            ORGL_USER VARCHAR(12),
                                            DEL_FLG VARCHAR(1),
                                            CHRG_PTRN_HDR_ID INT,
                                            SEQ INT,
                                            CHRG_CODE VARCHAR(3),
                                            PORT_CODE VARCHAR(7),   
                                            BASIS VARCHAR(2),
                                            UM VARCHAR(2),
                                            RATE DECIMAL(8,2),
                                            CURRENCY VARCHAR(3),
                                            PER VARCHAR(2),
                                            PREPAID_COLLECT VARCHAR(1),
                                            INV_OFC VARCHAR(6)
                                           );

    DBCC CHECKIDENT (##TempMenuResult, reseed, @tempRevSeq)

    SELECT @DscgPort = DSCH_PORT 
    FROM BL 
    WHERE BL_NO_UID = 1 AND CA_SEQ = 0;

    INSERT INTO #TempMenuResult (CHRG_PTRN_DTL_ID, UPDT_STAMP, UPDT_USER, ORGL_STAMP, ORGL_USER, DEL_FLG, CHRG_PTRN_HDR_ID, SEQ, CHRG_CODE, PORT_CODE, BASIS, UM, RATE, CURRENCY, PER, PREPAID_COLLECT, INV_OFC)
        SELECT 
            * 
        FROM 
            CHRG_PTRN_DTL CHG_1 
        WHERE 
            CHG_1.CHRG_PTRN_HDR_ID = @ChargePtrnHdrId 
            AND CHG_1.PORT_CODE = @DscgPort 
            AND CHG_1.DEL_FLG = 'N'

        UNION

        SELECT 
            * 
        FROM 
            CHRG_PTRN_DTL CHG_2 
        WHERE 
            CHG_2.PORT_CODE IS NULL 
            AND CHG_2.DEL_FLG = 'N';

    SELECT @TotalCount = COUNT(1) OVER () FROM #TempMenuResult;
    SELECT * FROM #TempMenuResult ORDER BY REV_SEQ;
END