Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server TSQL-基于位列更新列_Sql Server_Tsql - Fatal编程技术网

Sql server TSQL-基于位列更新列

Sql server TSQL-基于位列更新列,sql-server,tsql,Sql Server,Tsql,我的表中有一个标识列[ID]。我还有一个基于identity列的计算列,如下所示 create table [dbo].[tblMaster] ( ID bigint identity(1,1), GlobalFamilyUniqueID int, DupeIdentifier as cast('EDME' + RIGHT('00000000'+ISNULL(cast([ID] as nvarchar(max)),''),8) as nvarchar(30)), ControlNumber nv

我的表中有一个标识列
[ID]
。我还有一个基于identity列的计算列,如下所示

create table [dbo].[tblMaster] (
ID bigint identity(1,1),
GlobalFamilyUniqueID int,
DupeIdentifier as cast('EDME' + RIGHT('00000000'+ISNULL(cast([ID] as nvarchar(max)),''),8) as nvarchar(30)),
ControlNumber nvarchar(30),
NuixGuid nvarchar(50),
TopLvlGuid nvarchar(50),
ParentGuid nvarchar(50),
CustodianArtifactID int,
IsGlobalFamilyUnique int,
IsCustodianFamilyUnique int,
IsItemUnique int,
ItemUniqueDupID nvarchar(100),
IsChild int,
GroupIdentifier nvarchar(100),
DatasourceID int,
MD5Hash nvarchar(32),
GlobalFamilyDupID nvarchar(100),
CustodianFamilyDupId nvarchar(100),
ExportSessionID nvarchar(100)
)

我想根据
IsGlobalFamilyUnique
列更新
ControlNumber
列。此时,
DupeIdentifier
将有一个值,无论
IsGlobalFamilUnique
1
还是
0
。我需要
ControlNumber
根据
ControlNumber
字段中的最后一个值增加一个值(我创建了另一个名为
GlobalFamilyUnqiueID
的列作为“计数器”列),但只有当
IsGlobalFamilUnique
1
时才需要

这就是我目前的处境

update x
    set 
    GlobalFamilyUniqueID = [ProposedGlobalFamilyUniqueID],
    ControlNumber = [ProposedControlNumber]
from(
    Select top 10 [id],
        DupeIdentifier,
    --  ControlNumber,
        isglobalfamilyunique,
        --GlobalFamilyUniqueID,
        GlobalFamilyUniqueID,
        Row_Number() Over(Order By [id]) [ProposedGlobalFamilyUniqueID],
        ControlNumber,
        'TEST' + RIGHT('00000000'+ISNULL(cast(Row_Number() Over(Order By [id]) as nvarchar(30)),''),8) [ProposedControlNumber]
    From dbo.tblMaster mstr1
    where IsGlobalFamilyUnique = 1 and ControlNumber is null
)x
问题是,当再次运行代码时,
ProposedGlobalFamilyUniqueID
将再次从
1
开始。它应该从
dbo.tblMaster
中的
ProposedGlobalFamilyUniqueID
的最后一个值开始。我假设这是因为我将查询限制在
ControlNumber为null的地方,但我不知道如何解决这个问题

这是第一次运行时代码成功运行的示例:

最终结果应该是连续的
ControlNumber
值,其中
IsGlobalFamilyUnique=1

示例数据

 declare @test table (
    ID bigint identity(1,1),
    GlobalFamilyUniqueID int,
    DupeIdentifier as cast('EDME' + RIGHT('00000000'+ISNULL(cast([ID] as nvarchar(max)),''),8) as nvarchar(30)),
    ControlNumber nvarchar(30),
    MD5Hash nvarchar(32),
    IsGlobalFamilyUnique bit
)

insert into @test (MD5Hash, IsGlobalFamilyUnique)values
--1
('ABC', 1),
--2
('DEF', 1),
--3
('GHI', 1),
--4
('JKL', 1),
--5
('ABC', 0),
--6
('XXX', 1)
结果应该是,;

将ControlNumber字段保留为数字,尽量不要将其设为varchar,因为它将无法计算

首先,在尝试脚本之前删除所有现有的控件号值

那么,试试这个:

 DECLARE @max as bigint

 SELECT @max = Max(id)

 FROM tblMaster 


  Select [id],
        DupeIdentifier,
    --  ControlNumber,
        isglobalfamilyunique,
        --GlobalFamilyUniqueID,
        GlobalFamilyUniqueID,
        @max + Row_Number() Over(Order By [id]) [ProposedGlobalFamilyUniqueID],
        ControlNumber      
    From dbo.tblMaster mstr1
    where IsGlobalFamilyUnique = 1 and ControlNumber is null


   update x
    set 
    GlobalFamilyUniqueID = [ProposedGlobalFamilyUniqueID],
    ControlNumber =   x.Proposed_Control_Number

from(
    Select [id],
        DupeIdentifier,
    --  ControlNumber,
        isglobalfamilyunique,
        --GlobalFamilyUniqueID,
        GlobalFamilyUniqueID,
        Row_Number() Over(Order By [id]) [ProposedGlobalFamilyUniqueID],
        ControlNumber,
          @max + Row_Number() Over(Order By [id]) as Proposed_Control_Number   
    From dbo.tblMaster mstr1
    Where IsGlobalFamilyUnique = 1 and ControlNumber is null
)x

您能提供一些样本数据并期望结果吗?@D-Shih刚刚编辑以提供样本数据!