我正在創建sql hirercky表如何擺脫超時錯誤,當使用mssql層次結構檢查constrint
這是我的代碼;
約束函數代碼
alter Function Accounts.Types_Sub_Check_fn (@ID uniqueidentifier, @Sub Uniqueidentifier) returns int
begin
--declare @id uniqueidentifier = '8c7d4151-246c-476c-adf6-964ca9afdd3c' declare @sub uniqueidentifier = '47c2b6da-25fc-4921-adfa-b1f635bddde6'
declare @a int
declare @b int =(iif(@[email protected],2,0))
;with cte(id, lvl) as
(
select f.sub,
1
from Accounts.Types as f
where f.id = @id
union all
select f.sub,
lvl + 1
from Accounts.Types as f
inner join cte as c
on f.id = c.id
)
select @a = (select count (*)
from cte
where id [email protected]) + @b
option (maxrecursion 0)
return @a
end
go
表代碼
create Table Accounts.Types
(
ID uniqueidentifier not null CONSTRAINT DF_Accounts_Types_ID DEFAULT newid() CONSTRAINT PK_Accounts_Types_ID PRIMARY KEY NONCLUSTERED (ID) ,
Name varchar(200) not null CONSTRAINT UQ_Accounts_Types_NAME UNIQUE (NAME),
Sub uniqueidentifier CONSTRAINT FK_Accounts_Types_Sub Foreign key references Accounts.Types ,
Ctype uniqueidentifier CONSTRAINT FK_Accounts_Types_Ctype Foreign key references Accounts.Types ,
insert_time datetime not null CONSTRAINT DF_Accounts_Types_Insert_Time DEFAULT getdate() ,
insert_user uniqueidentifier CONSTRAINT DF_Accounts_Types_Insert_User DEFAULT'9EC66F53-9233-4A6C-8933-F8417D2BB5A9' ,
ts timestamp,
INDEX IX_Accounts_Types_NAME#ASC CLUSTERED (Name ASC),
Constraint Check_Accounts_Types_Sub check (Accounts.Types_Sub_Check_fn(ID,Sub)<=1)
)
go
如果試圖插入itseft父此功能將得到2,結果(在子列)
它會給1,如果它已經是一個孩子,試圖插入作爲其父母
CHECK約束創建檢查父(分列)對任何ID不應該是其子或大孩子, 和本身不能是其母公司
當我嘗試插入數據,不符合檢查約束,它卡住,並給出一個超時錯誤,
如:
insert into Accounts.Types (ID, Name, Sub)
values ('607936b9-6f95-4989-8ebe-87a08807f43e','LLL','607936b9-6f95-4989-8ebe-87a08807f43e')
這會給超時
任何人都可以幫助我,我需要擺脫超時錯誤;只有約束錯誤
它看起來像通過構建一個CSV字符串,然後對它進行LIKE檢查父/子/自我關係。您可能可以直接使用遞歸CTE檢查關係,而無需構建CSV字符串。 –
您是否排除了通過'hierarchyid'數據類型使用支持建模層次結構的構建? –
如果您在「樹」中創建循環,則CTE將運行一段時間。 [This](http://stackoverflow.com/a/15081353/92546)答案顯示了一種方法來終止遞歸,如果檢測到循環。 – HABO