Solved

How to iterate through each record without using cursor

Posted on 2016-11-21
2
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

630 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