Advertisement
JanuszKowalski123

10. b

May 9th, 2024 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.92 KB | None | 0 0
  1. CREATE TABLE Renewable_Energy_Devices (
  2.     device_id NVARCHAR(26) PRIMARY KEY,
  3.     device_status INT CHECK (device_status >= 0 AND device_status <= 6),
  4.     device_power DECIMAL(18,0),
  5.     company_id INT,
  6.     device_location GEOGRAPHY, -- Nowa kolumna przechowująca współrzędne geograficzne urządzenia
  7.     device_location_geom GEOMETRY, -- Nowa kolumna przechowująca współrzędne geometryczne urządzenia
  8.     device_hierarchy HIERARCHYID, -- Nowa kolumna przechowująca hierarchię urządzeń
  9.     device_description XML -- Nowa kolumna opisująca urządzenia
  10. );
  11.  
  12. -- Tworzenie zmiennych dla współrzędnych geograficznych i geometrycznych
  13. DECLARE @latitude DECIMAL(9, 6), @longitude DECIMAL(9, 6);
  14. DECLARE @point GEOMETRY;
  15. DECLARE @hierarchy HIERARCHYID;
  16. DECLARE @device_description XML;
  17.  
  18. -- Pętla wypełniająca tabelę
  19. DECLARE @i INT = 1;
  20. WHILE @i <= 50
  21. BEGIN
  22.     -- Generowanie losowych danych dla każdej kolumny
  23.     SET @latitude = RAND() * 180 - 90;
  24.     SET @longitude = RAND() * 360 - 180;
  25.     SET @point = GEOMETRY::Point(@latitude, @longitude, 4326);
  26.     SET @hierarchy = HIERARCHYID::GetRoot();
  27.     SET @device_description = '<description>Device ' + CAST(@i AS NVARCHAR(5)) + '</description>';
  28.  
  29.     -- Wstawianie danych do tabeli
  30.     INSERT INTO Renewable_Energy_Devices (device_id, device_status, device_power, company_id, device_location, device_location_geom, device_hierarchy, device_description)
  31.     VALUES (
  32.         'Device_' + CAST(@i AS NVARCHAR(5)), -- device_id
  33.         CAST(RAND() * 6 AS INT), -- device_status
  34.         CAST(RAND() * 1000 AS DECIMAL(18, 0)), -- device_power
  35.         CAST(RAND() * 10 AS INT) + 1, -- company_id
  36.         GEOGRAPHY::Point(@latitude, @longitude, 4326), -- device_location
  37.         @point, -- device_location_geom
  38.         @hierarchy.GetDescendant(NULL, NULL), -- device_hierarchy
  39.         @device_description -- device_description
  40.     );
  41.  
  42.     SET @i = @i + 1;
  43. END;
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement