Re: Delete locations batch
Has anyone written a batch file to delete a location and it's subsites?
Information Officer
Greater Lincolnshire Nature Partnership
The technical forum of the NBN Trust for use by the Network. It includes discussions and announcements on the NBN Atlas, Recorder 6 and Indicia |
You are not logged in. Please login or register.
Forum → How Do I... & Other Questions → Delete locations batch
Has anyone written a batch file to delete a location and it's subsites?
Sorry, I haven't got one. It will be a fairly complicated things to do and test because of the number of tables which use the Location_Key or which are linked in some way to it.
Yes, that's why I was asking :D
Not sure what's going to be quicker... manually delete ~1500 locations or try and write a batch update to do it....
There must be some SQL within Recorder that does the tests?
I have had a go at this and the results are below. It may not cover all possibilities and needs testing, but it does run. It should not even try to delete Locations which are used in Sample or Survey Events, or any of their parents. This version works by selecting a location, but it could be adapted to run from a csv file containing Location keys.
<?xml version="1.0" ?>
<batchupdate menupath="LC" description="Deletes selected Locations"
title="Delete Locations"
>
<SQL>
<Where keytype="Default">
CREATE TABLE #Location (Location_Key CHAR(16) COLLATE SQL_Latin1_General_CP1_CI_AS PRIMARY KEY,
Parent_Key CHAR(16) COLLATE SQL_Latin1_General_CP1_CI_AS , IsUsed integer )
INSERT INTO #Location (Location_Key,Parent_Key) SELECT L.LOCATION_KEY, L.LOCATION_KEY
FROM
LOCATION L
WHERE
<Condition field= "L.Location_key" operator="equal" type="Location" name="Select Locations" entrycount="-1" />
WHILE 1=1 BEGIN
INSERT INTO #Location
SELECT L.Location_Key, Tinc.Parent_Key, 0
FROM Location L
INNER JOIN #Location Tinc on L.Parent_Key=Tinc.Location_Key
LEFT JOIN #Location Texc ON Texc.Location_Key=L.Location_Key
WHERE Texc.Location_Key IS NULL
IF @@ROWCOUNT=0
BREAK
END
UPDATE #LOCATION SET ISUSED = 1
FROM #LOCATION WHERE EXISTS(SELECT * FROM SAMPLE WHERE SAMPLE.LOCATION_KEY = #LOCATION.LOCATION_KEY)
OR EXISTS(SELECT * FROM SURVEY_EVENT WHERE SURVEY_EVENT.LOCATION_KEY = #LOCATION.LOCATION_KEY)
WHILE 1=1 BEGIN
UPDATE #LOCATION SET ISUSED = 1
FROM #LOCATION INNER JOIN #LOCATION L ON L.PARENT_KEY = #LOCATION.LOCATION_KEY
WHERE L.ISUSED = 1 AND #LOCATIOn.ISUSED <> 1
IF @@ROWCOUNT=0
BREAK
END
DELETE FROM #LOCATION WHERE ISUSED = 1
DELETE FROM LOCATION_USE
FROM LOCATION_USE INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_USE.LOCATION_KEY
DELETE FROM TENURE
FROM TENURE INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = TENURE.LOCATION_KEY
DELETE FROM LOCATION_SOURCES
FROM LOCATION_SOURCES INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_SOURCES.LOCATION_KEY
DELETE FROM LOCATION_RELATION
FROM LOCATION_RELATION INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_RELATION.LOCATION_KEY_1
DELETE FROM LOCATION_RELATION
FROM LOCATION_RELATION INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_RELATION.LOCATION_KEY_2
DELETE FROM LOCATION_DESIGNATION
FROM LOCATION_DESIGNATION INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_DESIGNATION.LOCATION_KEY
DELETE FROM LOCATION_BOUNDARY
FROM LOCATION_BOUNDARY INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_BOUNDARY.LOCATION_KEY
DELETE FROM LOCATION_ADMIN_AREAS
FROM LOCATION_ADMIN_AREAS INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_ADMIN_AREAS.LOCATION_KEY
DELETE FROM LOCATION_NAME
FROM LOCATION_NAME INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_NAMe.LOCATION_KEY
DELETE FROM GRID_SQUARE
FROM GRID_SQUARE INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = GRID_SQUARE.LOCATION_KEY
DELETE FROM LOCATION_DATA
FROM LOCATION_DATA INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION_DATA.LOCATION_KEY
DELETE FROM LOCATION_FEATURE_SOURCES
FROM LOCATION_FEATURE_SOURCES INNER JOIN LOCATION_FEATURE LF ON LF.LOCATION_FEATURE_KEY =
LOCATION_FEATURE_SOURCES.LOCATION_FEATURE_KEY
INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LF.LOCATION_KEY
DELETE FROM MANAGEMENT_AIM
FROM MANAGEMENT_AIM
INNER JOIN LOCATION_FEATURE LF ON LF.LOCATION_FEATURE_KEY = MANAGEMENT_AIM.LOCATION_FEATURE_KEY
INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LF.LOCATION_KEY
DELETE FROM POTENTIAL_THREAT
FROM POTENTIAL_THREAT
INNER JOIN LOCATION_FEATURE LF ON LF.LOCATION_FEATURE_KEY = POTENTIAL_THREAT.LOCATION_FEATURE_KEY
INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LF.LOCATION_KEY
DELETE FROM LOCATION_FEATURE
FROM LOCATION_FEATURE LF
INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LF.LOCATION_KEY
WHILE 1=1 BEGIN
DELETE FROM LOCATION
FROM LOCATION INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LOCATION.LOCATION_KEY
WHERE NOT EXISTS (SELECT * FROM #Location L2 WHERE L2.Parent_Key = #LOCATION.LOCATION_KEY)
IF @@ROWCOUNT=0
BREAK
END
DROP TABLE #LOCATION
</Where>
Excellent Mike, thanks. So far so, good!
Just had a conflict in the LAND_PARCEL table.
Assume the code would follow the same pattern:
DELETE FROM LAND_PARCEL
FROM LAND_PARCEL LP
INNER JOIN #LOCATION ON #LOCATION.LOCATION_KEY = LP.LOCATION_KEY
Yes. Missed that one. Also missed Damage_Occurence which has a link to Location_Feature
Greetings, I'm James Shelti Recently moved from Iowa, originally from Lincoln and have lived there most of my life.
I have an awesome free time,I love to experience all four seasons , specially the summer and the heat.
High Tech has been a part of my life for the last 5 years and most recently primarily on the World Wide Web.
I cannot resist to learn.
As tough as it is right now, more and more people are seeking help from people who have walked the walk and can teach others how to benefit from what they went through. I am here to help those that need help and will ask for help from other people when i need it.
I am a technician in the field of Engineering and Scientific project management software.
I am sharing my favorite sites , new york attractions
new york pass new york pass
Learning about new things is one of my passions.
when possible I volunteer for for valid causes that help the elderly.
Glad to be a part of this site and Will learn something new here.
Forum → How Do I... & Other Questions → Delete locations batch
Powered by PunBB 1.4.6, supported by Informer Technologies, Inc.