Solved

How to iterate through each record without using cursor

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 37
SQL Update trigger 5 18
convert null in sql server 12 34
Rename a column in the output 3 14
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 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