79478350

Date: 2025-03-01 22:23:40
Score: 1.5
Natty:
Report link

This is working for me and it is based on modified version of this:

Is there a way to loop through a table variable in TSQL without using a cursor?

As you can see, no more Cursors! HTH someone!

DECLARE @target_state CHAR(4) 
DECLARE @geom_state geography
DECLARE @distance FLOAT = 482803; -- A distance in metres   482803 = 300 miles
declare @RowNum int, @State_ID int
select State_ID=MAX(State_ID) FROM [dbo].[gis_US_States_GeoJSON]     --start with the highest ID
Select @RowNum = Count(*) From [dbo].[gis_US_States_GeoJSON]       --get total number of records
WHILE @RowNum > 0                          --loop until no more records
BEGIN   
    select  @target_state = STUSPS, @geom_state = State_Boundaries_geog
    from [dbo].[gis_US_States_GeoJSON] where State_ID= @RowNum 
    INSERT INTO dbo.gis_shell300_imls (MID)(SELECT m.MID 
       FROM dbo.gis_MuseumFile2018_File1_Nulls m , dbo.gis_2022_gaz_zcta_national z
      WHERE
       @geom_state.STContains(z.ZCTA_Centroid_Geography)  = 1)
    set @RowNum = @RowNum - 1    --decrease count
END
Reasons:
  • Blacklisted phrase (1): Is there a way
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: IrfanClemson