将具有相同功能的列复制到另一个表

时间:2015-11-04 17:39:37

标签: sql tsql

我想将身份函数identity(1,1)生成的列复制到另一个表中。但是,在此过程之后,整个新列仅显示1.

有没有办法解决这个问题?谢谢

2 个答案:

答案 0 :(得分:0)

 CREATE TABLE Dim_Route(RtID INT Identity(1,1) Primary Key, Itinerary varchar(50) NOT NULL, ) 
 CREATE TABLE Dim_FlightSchedule(FSID INT Identity(1,1) Primary Key, RTID INT, Constraint fk_Route Foreign KEY (RTID) REFERENCES Dim_Route(RtID), )

 insert Dim_Route (Itinerary)
 values 
 ('B'),
 ('C'),
 ('D'),
 ('E'),
 ('F')

insert Dim_FlightSchedule (RTID)
select RTID from Dim_Route

答案 1 :(得分:0)

这是我的代码:

  

原始表:CREATE TABLE Dim_Route(RtID INT Identity(1,1) Primary Key, Itinerary varchar(50) NOT NULL, )

目的地表:CREATE TABLE Dim_FlightSchedule(FSID INT Identity(1,1) Primary Key, RTID INT, Constraint fk_Route Foreign KEY (RTID) REFERENCES Dim_Route(RtID), )
UPDATE Dim_FlightSchedule SET Dim_FlightSchedule.RTID = Dim_Route.RTID FROM Dim_Route

目标表中的RTID均为1。 我不知道我哪里出错了。谢谢