开发者

How to make a list of IDs of a table so I cycle through the IDs and use in a different table

I created a distance table/chart in the chart I want the ability to ada a city. I created a stored procedure in mssql which goes something like this

ALTER PROCEDURE dbo.sup_InsertCity
    
    (
    @cityName nvarChar(50)
    )
    
AS
    Insert into citiesTest (cityName)
    values (@cityName)
    

    declare @counter int
    declare @cityNumber int
    declare @cityID int
    SELECT @cityNumber = COUNT(idCities) FROM citiesTest 
    SELECT @cityID = @@Identity    
    set @counter = 0
    while @count开发者_C百科er < @cityNumber
    begin
      set @counter = @counter + 1
     /*
     
     
     */
        insert into DistanceTest (CityTop, CityRight, Distance, time)
        values (@cityID, @counter, 0, 0)
        insert into DistanceTest (CityTop, CityRight, Distance, time)
        values ( @counter, @cityID, 0, 0)
    end

RETURN

Now the problem is that this would only work if one didn't delete city's making a gap in the cities id. Instead of doing a course I would like to go thrould a collection of the id's in the other table to assign as the second city.


Would something like this work for you:

Declare @CityId int

Insert CitiesTest( cityName )
Values( @cityName )

Set @CityId = SCOPE_IDENTITY()

Insert DistanceTest( CityTop, CityRight, Distance, Time )
Select @CityId, CityId, 0, 0
From CitiesTest
Where CityId <> @CityId
Union All
Select CityId, @CityId, 0, 0
From CitiesTest
Where CityId <> @CityId
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜