我在图表中创建了一个距离表/图表,我希望能够加入一个城市。 我在mssql中创建了一个类似于
的存储过程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 @counter < @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
现在的问题是,只有在没有删除城市的城市ID中存在差距时,这才有效。我不想做一个课程,而是想把另一个表中的id集合作为第二个城市。
答案 0 :(得分:2)
这样的事情对你有用:
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