Advertisement
JanuszKowalski123

10. hierarch 11 ok

May 9th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. USE [2024_MZ202_ZSBD_7237]
  2. GO
  3. DROP TABLE [dbo].[Renewable_Energy_Devices];
  4.  
  5. CREATE TABLE Renewable_Energy_Devices (
  6. device_id NVARCHAR(26) PRIMARY KEY,
  7. device_status INT CHECK (device_status >= 0 AND device_status <= 6),
  8. device_power DECIMAL(18,0),
  9. company_id INT,
  10. device_location GEOGRAPHY,
  11. device_location_geom GEOMETRY,
  12. device_hierarchy HIERARCHYID,
  13. device_description XML
  14. );
  15.  
  16. DECLARE @latitude DECIMAL(9, 6), @longitude DECIMAL(9, 6);
  17. DECLARE @point GEOMETRY;
  18. DECLARE @device_hierarchy HIERARCHYID;
  19. DECLARE @device_description XML;
  20.  
  21. DECLARE @i INT = 1;
  22. WHILE @i <= 50
  23. BEGIN
  24. SET @latitude = RAND() * 180 - 90;
  25. SET @longitude = RAND() * 360 - 180;
  26. SET @point = GEOMETRY::Point(@latitude, @longitude, 4326);
  27.  
  28. -- Tworzenie hierarchii z użyciem losowej wartości dodanej do korzenia
  29. IF @i = 1
  30. SET @device_hierarchy = HIERARCHYID::GetRoot().GetDescendant(NULL, NULL);
  31. ELSE
  32. SET @device_hierarchy = @device_hierarchy.GetDescendant(NULL, NULL);
  33.  
  34. SET @device_description = '<description>Device_' + CAST(@i AS NVARCHAR(5)) + '</description>';
  35.  
  36. INSERT INTO Renewable_Energy_Devices (device_id, device_status, device_power, company_id, device_location, device_location_geom, device_hierarchy, device_description)
  37. VALUES (
  38. 'Device_' + CAST(@i AS NVARCHAR(5)),
  39. CAST(RAND() * 6 AS INT),
  40. CAST(RAND() * 1000 AS DECIMAL(18, 0)),
  41. CAST(RAND() * 10 AS INT) + 1,
  42. GEOGRAPHY::Point(@latitude, @longitude, 4326),
  43. @point,
  44. @device_hierarchy,
  45. @device_description
  46. );
  47.  
  48. SET @i = @i + 1;
  49. END;
  50.  
  51. -- Wyświetlenie zaktualizowanych rekordów
  52. SELECT * FROM Renewable_Energy_Devices;
  53.  
  54.  
  55.  
  56.  
  57.  
  58. WITH DeviceHierarchyCTE AS (
  59. SELECT
  60. device_id,
  61. device_hierarchy,
  62. CAST('/' + device_id + '/' AS NVARCHAR(MAX)) AS hierarchy_path,
  63. CAST('/' + device_id + '/' AS NVARCHAR(MAX)) AS device_path,
  64. 1 AS hierarchy_level
  65. FROM
  66. Renewable_Energy_Devices
  67. WHERE
  68. device_hierarchy = HIERARCHYID::GetRoot()
  69.  
  70. UNION ALL
  71.  
  72. SELECT
  73. red.device_id,
  74. red.device_hierarchy,
  75. dhc.hierarchy_path + red.device_id + '/' AS hierarchy_path,
  76. dhc.device_path + red.device_id + '/' AS device_path,
  77. dhc.hierarchy_level + 1
  78. FROM
  79. Renewable_Energy_Devices AS red
  80. INNER JOIN
  81. DeviceHierarchyCTE AS dhc ON dhc.device_hierarchy.IsDescendantOf(red.device_hierarchy) = 1
  82. )
  83. SELECT
  84. device_id,
  85. hierarchy_path,
  86. device_path,
  87. hierarchy_level
  88. FROM
  89. DeviceHierarchyCTE
  90. ;
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement