Reputation: 7701
I am updating a table with spatial types and there are three or more updates that I might have to perform to make sure that the geometry type is successfully loaded into a geography type. What I want to do is to us the first update and commit as many rows as possible, then use the second update where the first one failed and lastly, use the third update where the second one failed.
I successfully managed to do did this manually and updated all the geography types using the geometry types. These are the statements that I am using:
Update statement #1:
Update Postcode set geog = geography::STGeomFromWKB(geom.STAsBinary(), 4326)
FROM Postcode
WHERE geog is null
Update statement #2:
Update Postcode set geog = geography::STGeomFromWKB(geom.STUnion(geom.STStartPoint()).STAsBinary(), 4326)
FROM Postcode
WHERE geog is null
Update Statement #3:
Update Postcode set geog = geography::STGeomFromWKB(geom.STBuffer(0.00001).STBuffer(-0.00001).STAsBinary(), 4326)
FROM Postcode
WHERE geog is null
Is there a way to write this so that it lets me update rows wherever error is not encountered and then move on to the next update statement in case of an error?
I hope it makes sense and any help will be greatly appreciated.
Upvotes: 1
Views: 2291
Reputation: 5654
If there is a way to test each assignment without actually throwing an error, then a preferred approach is to use CASE statement to switch among them.
update postcode set geog = case when <test> then <A>
when <testB> then <B>
else <C>
end
Otherwise if an error cannot be avoided, then nested try/catch blocks can be used to ensure execution. However when an error occurs within a given statement, that whole statement may be rolled back (not 100% sure here). You sounded like you would be happy if you could just run all three statements so not sure if this is an issue.
BEGIN TRY
-- A
END TRY
BEGIN CATCH
BEGIN TRY
-- B
END TRY
BEGIN CATCH
-- C
END CATCH
END CATCH
Upvotes: 1