Solved

How to iterate through each record without using cursor

Posted on 2016-11-21
2
41 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 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41896126
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
ID: 41896210
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 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