DECLARE @Aux int = 0
DECLARE @Conta int = 0
WHILE @Aux < 100
BEGIN
SELECT @Conta = COUNT(1) FROM ra.HierarchicalStructure WHERE HierarchicalStructureId LIKE '%.' + CONVERT(varchar, @Aux) AND HierarchyLevel = 'BUN'
IF (@Conta > 1)
BEGIN
print @Aux
END
SET @Aux = @Aux + 1
END
--DECLARE @HierarchicalStructureId varchar(18) = '21.32'
--SELECT COUNT(1) AS Estruturas_Ativas FROM ra.HierarchicalStructure WITH(NOLOCK) WHERE HierarchicalStructureId LIKE @HierarchicalStructureId + '%' AND Status != 'I'
--SELECT COUNT(1) AS Artigos FROM ra.Item WITH(NOLOCK) WHERE HierarchicalStructureId IN (SELECT HierarchicalStructureId FROM ra.HierarchicalStructure WITH(NOLOCK) WHERE HierarchicalStructureId LIKE @HierarchicalStructureId + '%' AND HierarchyLevel = 'UNB' AND Status = 'I')
--SELECT COUNT(1) AS Validades FROM vd.NumDaysValidaty WITH(NOLOCK) WHERE HierarchicalStructureId IN (SELECT HierarchicalStructureId FROM ra.HierarchicalStructure WITH(NOLOCK) WHERE HierarchicalStructureId LIKE @HierarchicalStructureId + '%' AND HierarchyLevel = 'UNB' AND Status = 'I')
----DELETE vd.NumDaysValidaty WHERE HierarchicalStructureId IN (SELECT HierarchicalStructureId FROM ra.HierarchicalStructure WITH(NOLOCK) WHERE HierarchicalStructureId LIKE @HierarchicalStructureId + '%' AND HierarchyLevel = 'UNB' AND Status = 'I')
----DELETE ra.HierarchicalStructure WHERE HierarchicalStructureId LIKE @HierarchicalStructureId + '%' AND Status = 'I'
--DECLARE @Aux int = 32
--SELECT * FROM ra.HierarchicalStructure WHERE HierarchicalStructureId LIKE '%.' + CONVERT(varchar, @Aux) AND HierarchyLevel = 'BUN'