如何更新architectureid列

时间:2018-10-07 11:39:24

标签: sql-server tsql sql-server-2012

我有一个名为items的表,该表具有我要转换为层次结构ID的父/子关系。

我已按照this教程进行操作。

本教程的所有步骤均有效,最后的更新声明除外。

我收到错误消息:

  

不允许从数据类型architectureid到nvarchar(max)的隐式转换。使用CONVERT函数运行此查询。

但这没有任何意义。我要更新的字段是architectureid,而不是nvarchar(nax)。因此,我看不到涉及nvarchar(max)字段的地方。

drop table #children
CREATE TABLE #Children   
   (  
    ID int,  
    TenantId int,
    ParentID int,
    Num int  
);  
GO
CREATE CLUSTERED INDEX tmpind ON #Children(TenantId, ParentID, ID);  
GO

INSERT #Children (ID,  TenantId,ParentID, Num)  
SELECT ID, TenantId, ParentID, 
  ROW_NUMBER() OVER (PARTITION BY TenantId, ParentID ORDER BY ParentId)   
FROM Items  
GO 

SELECT * FROM #Children ORDER BY TenantId, ParentID, Num  
GO


WITH paths(path, ID, ParentId, TenantId)   
AS (  
-- This section provides the value for the root of the hierarchy  
SELECT hierarchyid::GetRoot() AS OrgNode, ID, ParentId, TenantId   
FROM #Children AS C   
WHERE ParentId IS NULL   

UNION ALL   
-- This section provides values for all nodes except the root  
SELECT   
CAST(p.path.ToString() + CAST(C.Num AS varchar(30)) + '/' AS hierarchyid),   
C.ID , C.ParentId, C.TenantId 
FROM #Children AS C   
JOIN paths AS p   
   ON C.ParentID = P.ID   
)
-- This select statement runs just fine and shows expected data.
--Select i.Id as ItemId, p.path, p.path.ToString() as LogicalNode, p.Id, p.ParentId, p.TenantId from Paths P
--join Items I on p.Id = i.Id
--order by P.TenantId, P.path

--Note that I have tried using the convert function, but it still fails with the same error message.
UPDATE I Set OrgNode = Convert(hierarchyid, P.path)
FROM Items I    
JOIN Paths AS P   
   ON I.ID = P.ID  
GO

编辑 奇怪的是,这个DBFiddle有用。

enter image description here

2 个答案:

答案 0 :(得分:1)

看起来列require('../src/FoxRandomString.js'); obj = FoxRandomString; Tests = { obj: obj, canSetLength: function(){ vals = [true,false,-1,'text',undefined,null,0,2,4,10] for (i = 0; i < vals.length; i++){ if (vals[i] < 4){ if (this.obj.setLength(vals[i]) == 4){ console.log('%cFor input value = '+vals[i]+ ' Success. Output: '+this.obj.setLength(vals[i]),'color: white; background:olive') } else{ console.log('%cFor input value = '+vals[i]+ ' Fail. Output: '+this.obj.setLength(vals[i]),'color:black; background:red') } } else{ if (this.obj.setLength(vals[i]) == vals[i] || isNaN(vals[i])){ console.log('%cFor input value = '+vals[i]+ ' Success. Output: '+this.obj.setLength(vals[i]),'color: white; background:olive') } else{ console.log('%cFor input value = '+vals[i]+ ' Fail. Output: '+this.obj.setLength(vals[i]),'color:black; background:red') } } } } } Tests.canSetLength(); 的类型不是require() is not defined。您可以使用OrgNode

hierachyid

或更改表ToString()并更改列类型。

答案 1 :(得分:1)

看起来您已经解决了问题,但是我建议将转换到architectureid的内容保存到最后。像这样:

WITH paths(path, ID, ParentId, TenantId)   
AS (  
-- This section provides the value for the root of the hierarchy  
SELECT cast('/' as varchar(max)) AS OrgNode, ID, ParentId, TenantId   
FROM #Children AS C   
WHERE ParentId IS NULL   

UNION ALL   
-- This section provides values for all nodes except the root  
SELECT   
CAST(concat(p.path.ToString(), C.Num, '/') AS varchar(max)),   
C.ID , C.ParentId, C.TenantId 
FROM #Children AS C   
JOIN paths AS p   
   ON C.ParentID = P.ID   
)
-- This select statement runs just fine and shows expected data.
--Select i.Id as ItemId, p.path, p.path.ToString() as LogicalNode, p.Id, p.ParentId, p.TenantId from Paths P
--join Items I on p.Id = i.Id
--order by P.TenantId, P.path

--Note that I have tried using the convert function, but it still fails with the same error message.
UPDATE I Set OrgNode = Convert(hierarchyid, P.path)
FROM Items I    
JOIN Paths AS P   
   ON I.ID = P.ID  
GO

请注意,我还为+函数更改了concat()的串联样式,因此您不必费心将C.Num转换为varchar。