Hi all,
I have a temp guy who is doing some work for us (seriously, I never wrote the query).
Now this is the scenario (hold tight). So we have a stored procedure as follows...
ALTER PROCEDURE createSegmentPoints AS
DECLARE @.fLat INTEGER
DECLARE @.fLon INTEGER
DECLARE @.segId INTEGER
-- declare cursor to return the from lat and lon for all segments that do not have address point 109.
DECLARE c1 CURSOR FOR (SELECT From_Latitude, From_Longitude, id AS segment_id FROM Segments WHERE SegmentType != 109)
OPEN c1
FETCH NEXT FROM c1 INTO @.fLat, @.fLon, @.segId
WHILE @.@.FETCH_STATUS = 0
BEGIN
-- insert into table the segId, from lat, from lon and returned segment id from function.
INSERT INTO test VALUES (@.segId,@.fLat,@.fLon,dbo.points_test(@.fLat,@.fLon))
FETCH NEXT FROM c1 INTO @.fLat, @.fLon, @.segId
END
CLOSE c1
DEALLOCATE c1
As you can see here I am using a Cursor, which in turn calls a functionwith each row that is processed in the recordset. The function that iscalled is as follows...
ALTER FUNCTION points_test(@.x INTEGER, @.y INTEGER)
RETURNS INTEGER
AS
BEGIN
-- function to find the closed segment point with address point 109 to the segment specified in procedure.
DECLARE @.tempDistance FLOAT(4)
SET @.tempDistance = 1000000
DECLARE @.id, @.seg, lat, lon INTEGER
DECLARE @.distance, @.xd, @.yd FLOAT
DECLARE c1 CURSOR FOR (SELECT from_latitude, from_longitude, id FROM segments WHERE segmenttype = 109)
OPEN c1
FETCH NEXT FROM c1 INTO @.lat, @.lon, @.id
WHILE @.@.FETCH_STATUS = 0
BEGIN
-- calucations to get distance.
SET @.xd = (@.lat-@.x)
SET @.yd = (@.lon-@.y)
SET @.distance = SQRT((@.xd*@.xd) + (@.yd*@.yd))
-- test if you have shortest distance.
IF (@.distance < @.tempDistance)
BEGIN
SET @.tempDistance = @.distance
SET @.seg = @.id
END
FETCH NEXT FROM c1 INTO @.lat,@.lon, @.id
END
CLOSE c1;
DEALLOCATE c1;
RETURN @.seg
END
(This function works out an equation to get the shortest distance fromtwo parameters passed to the function calculated with data from eachrow returned within the cursor)
As you can see here, this function contains ANOTHER cursor!! Ouch. Thefact that their is an SQL query in a function is a killer, but havinganother embedded cursor there is also a killer - this has virtuallykiller the application.
So, how best is it for me to correct this. Should I turn the functioninto a stored procudure? But even if I do this, the nested cursor stillremains. I was thinking maybe to have the SQRT equations within theSELECT expression and then wrapped in a MIN() to maybe get the lowestvalue.
Any ideas would be of great help.
Thanks
Tryst
(1) you could get rid of the cursor in the stored proc by doing something like this:
INSERT INTO test ( col1, col2, col3, col4)
SELECT
From_Latitude,
From_Longitude,
id,
dbo.points_testFrom_LatitudeFrom_Longitude)
FROM
Segments
WHERE
SegmentType != 109
(2) The cursor in the function can be replaced with a table variable. Read up books on line for table varibles (or google) and you can find sample code.
|||OK. Will try your suggested tips.
Thanks
Tryst
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment