Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to iterate through each record without using cursor

Posted on 2016-11-21
2
Medium Priority
?
59 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 35

Accepted Solution

by:
Pawan Kumar earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

972 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