Solved

How to iterate through each record without using cursor

Posted on 2016-11-21
2
19 Views
Last Modified: 2016-11-21
Hi guys,

I will be sent an array of postcodes from the API. I need to iterate through each row and bring back the latitude and longitude from the table and then create a string from the latitude and longitude to create a polygon.

I will be capturing the array in my table type.

Is it possible to iterate through each record without using cursors?
Or is there a way of doing this by set based operation.

Regards,
0
Comment
Question by:shah36
2 Comments
 
LVL 16

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
Basically what you wanted to achieve?  You can use below--, It is easy compared to Cursors.

DECLARE @Id AS VARCHAR(MAX)=''
 
IF OBJECT_ID('tempdb..#TempBills') IS NOT NULL
    DROP TABLE #TempBills
 
SELECT * INTO #TempBills FROM YourTable
 
WHILE EXISTS (SELECT TOP 1 col FROM #TempBills)
BEGIN
 
     SELECT TOP 1 @Id = col FROM #TempBills
 
     -- Your operations 
 
     DELETE FROM #TempBills WHERE col = @Id
 
END

Open in new window

0
 

Author Closing Comment

by:shah36
Comment Utility
Thank you very much. Using your logic i have sorted it out like the follow:

DECLARE @PostCodes AS dbo.[AreaPostCodes];

INSERT  INTO @PostCodes
        SELECT TOP ( 100 )
                Incode ,
                OutCode
        FROM    dbo.AddressStringSearch;

--SELECT * FROM @PostCodes

DECLARE @Lat AS VARCHAR(50) ,
    @Long VARCHAR(50) ,
    @Incode VARCHAR(4) ,
    @OutCode VARCHAR(4) ,
    @String AS VARCHAR(MAX) = '';

IF OBJECT_ID('tempdb..#InCodes') IS NOT NULL
    DROP TABLE #Incodes;

SELECT  *
INTO    #Incodes
FROM    @PostCodes;

WHILE EXISTS ( SELECT TOP 1
                        inCode ,
                        OutCode
               FROM     #Incodes )
    BEGIN

        SELECT  @Lat = ( SELECT TOP 1
                                Latitude
                         FROM   dbo.AddressStringSearch
                         WHERE  Incode = ( SELECT TOP 1
                                                    inCode
                                           FROM     #Incodes
                                         )
                                AND OutCode = ( SELECT TOP 1
                                                        OutCode
                                                FROM    #Incodes
                                              )
                       );
        SELECT  @Long = ( SELECT TOP 1
                                    Longitude
                          FROM      dbo.AddressStringSearch
                          WHERE     Incode = ( SELECT TOP 1
                                                        inCode
                                               FROM     #Incodes
                                             )
                                    AND OutCode = ( SELECT TOP 1
                                                            OutCode
                                                    FROM    #Incodes
                                                  )
                        );

        SELECT  @Incode = inCode
        FROM    #Incodes;
        SELECT  @OutCode = OutCode
        FROM    #Incodes;

        SET @String = @String + ' ' + @Lat + ' ' + @Long + ',';

        DELETE  FROM #Incodes
        WHERE   inCode = @Incode
                AND OutCode = @OutCode;

    END;

SELECT  LTRIM(LEFT(@String, DATALENGTH(@String) - 1));
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now