如何在SQL中自动增加NULL值

时间:2017-12-12 02:05:25

标签: sql-server sql-server-2016

我的下表中 ID 字段的值为NULL。我想基于当前表格中的最大 ID ,创建一个将所有NULL值替换为自动递增数字的查询。

| ID          | OBJECT  |
|------------ |-------- |
| 5637144579  | Obj 1   |
| NULL        | Obj 2   |
| 5637144584  | Obj 3   |
| NULL        | Obj 4   |

所以最终结果应该是

| ID          | OBJECT  |
|------------ |-------- |
| 5637144579  | Obj 1   |
| 5637144585  | Obj 2   |
| 5637144584  | Obj 3   |
| 5637144586  | Obj 4   |

请注意,每次更新后,NULL都会根据之前的最大 ID 值替换为自动增量数字。

现在,我有

SELECT
  ...
  ((select MAX(ID) from [dbo].MYTABLE) + 1 ) ID
  ...

但这会导致5637144585 ID 值的NULL

5 个答案:

答案 0 :(得分:1)

试试这个答案:

file_id = url.split("id=")[1]

<强>输出:

DECLARE @MYTABLE TABLE(ID BIGINT, OBJECT VARCHAR(10))

INSERT INTO @MYTABLE VALUES (5637144579,'OBJ 1')
INSERT INTO @MYTABLE VALUES (NULL,'OBJ 2')
INSERT INTO @MYTABLE VALUES (5637144584,'OBJ 3')
INSERT INTO @MYTABLE VALUES (NULL,'OBJ 4')

UPDATE A SET A.ID=B.RN
FROM @MYTABLE A,(
    SELECT *
        ,ROW_NUMBER() OVER(ORDER BY OBJECT)+(SELECT MAX(ID) FROM @MYTABLE)RN
    FROM @MYTABLE
    WHERE ID IS NULL)B
WHERE A.OBJECT=B.OBJECT

SELECT * FROM @MYTABLE

答案 1 :(得分:1)

解决此问题的一种方法是使用sum作为空值ID值的运行计数,并将其添加到之前的max(ID)值。

示例数据:

create table #sample
    (
        ID bigint
        , [object] char(5)
    )

insert into #sample
values (5637144579, 'Obj 1')
, (NULL, 'Obj 2')
, (5637144584, 'Obj 3')
, (NULL,  'Obj 4') 

<强>答案:

declare @max_id bigint = (select max(s.id) as id_bgn from #sample as s)

; with prelim as
    (
        select s.id
        , s.[object]
        , iif(s.id is null, 1, 0) as id_is_null
        from #sample as s       
    )
select isnull(p.id, @max_id + sum(p.id_is_null) over (order by p.[object] asc)) as id
, p.[object]
from prelim as p

答案 2 :(得分:0)

我认为这是更简单的解决方案

构建表

DECLARE @MYTABLE TABLE(ID BIGINT, OBJECT VARCHAR(10))

INSERT INTO @MYTABLE VALUES (5637144579,'OBJ 1')
INSERT INTO @MYTABLE VALUES (NULL,'OBJ 2')
INSERT INTO @MYTABLE VALUES (5637144584,'OBJ 3')
INSERT INTO @MYTABLE VALUES (NULL,'OBJ 4')

<强>答案

DECLARE @N BIGINT

SELECT @N = Max(ID) FROM @MYTABLE

UPDATE @MYTABLE
SET @N = @N+1,
ID = @N
WHERE ID IS NULL

SELECT * from @MYTABLE

答案 3 :(得分:0)

如果您只想选择,您可以这样做:

SELECT ID, Obj FROM test
   WHERE ID IS NOT NULL
UNION
SELECT (SELECT Max(ID) FROM TEST) + ROW_NUMBER() OVER(ORDER BY OBJ) AS ID, OBJ
    FROM TEST WHERE ID is NULL;

答案 4 :(得分:0)

DECLARE @MYTABLE TABLE(ID BIGINT, OBJECT VARCHAR(10))

INSERT INTO @MYTABLE VALUES (5637144579,'OBJ 1')
                            ,(NULL,'OBJ 2')
                            ,(5637144584,'OBJ 3')
                            ,(NULL,'OBJ 4');

;With CTE AS(SELECT (SELECT MAX(ID) from @MYTABLE)+ROW_NUMBER()OVER(ORDER BY ID) AS id,
                    Object 
              FROM @MYTABLE WHERE id IS NULL)

SELECT ISNULL(T1.id,CTE.id) as ID,
        T1.Object
 FROM @MYTABLE T1  FULL JOIN CTE
   ON (CTE.Object=T1.Object)

    --- ------------
    ID         Object
    ---------------------
    5637144579  OBJ 1
    5637144585  OBJ 2
    5637144584  OBJ 3
    5637144586  OBJ 4