-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateMissingCoordinates.sql
More file actions
43 lines (35 loc) · 1.17 KB
/
Copy pathUpdateMissingCoordinates.sql
File metadata and controls
43 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Purpose:
- Apply previously validated coordinates to rows whose latitude or longitude is missing.
Safety:
- Changes data only when @ApplyChanges = 1. The default execution rolls back.
- Validation/geocoding should happen outside this update and must not expose API keys in SQL.
Requirements:
- SQL Server 2012 or later.
Customization:
- Replace dbo.Location and dbo.LocationValidationResult with neutral equivalents in your schema.
*/
SET XACT_ABORT ON;
DECLARE @ApplyChanges BIT = 0;
BEGIN TRANSACTION;
UPDATE target
SET
latitude = validated.latitude,
longitude = validated.longitude
OUTPUT
inserted.location_id,
deleted.latitude AS previous_latitude,
deleted.longitude AS previous_longitude,
inserted.latitude AS proposed_latitude,
inserted.longitude AS proposed_longitude
FROM dbo.Location AS target
JOIN dbo.LocationValidationResult AS validated
ON validated.location_id = target.location_id
WHERE (target.latitude IS NULL OR target.longitude IS NULL)
AND validated.is_valid = 1
AND validated.latitude BETWEEN -90 AND 90
AND validated.longitude BETWEEN -180 AND 180;
IF @ApplyChanges = 1
COMMIT TRANSACTION;
ELSE
ROLLBACK TRANSACTION;