我现在有一个table
PatientID ,First_Name,Last_Name,SS_No,AddressName,Case_Manager
我需要把他们导入到另二个表中
table1包括(PatientID,First_Name,Last_Name,SS_No,AddressID)
table2包括(AddressID,AddressName,Case_Manager)table,table1表中PatientID类型为IDENTITY,Table2中AddressID为IDENTITY
导入原则是这样的,先把table表中的AddressName,Case_manager导入到table2中,取得AddressID,
然后把取回的AddressId和PatientID,First_Name,Last_Name,SS_No导入到Table1中,请问这该怎么导入,是不是需要利用循环和游标???我想通过一条一条的导入,因为实际需要导入的表比这个要大很多,而且母表好多字段都是空的,请问大家有没有一条一条导入的方式!!!

解决方案 »

  1.   

    用游标啊,或都用程序控制,每导入一行table2后,获取@@identity,就是那个AddressID,再导table1
      

  2.   


    create table tablea(PatientID  int ,First_Name int ,Last_Name int ,SS_No int ,AddressName int ,Case_Manager int )create table table1(PatientID int identity(1,1)  ,First_Name int ,Last_Name int ,SS_No int ,AddressID int )
    create table table2(AddressID int identity(1,1),AddressName int ,Case_Manager int)insert into table2(AddressName ,Case_Manager) 
           select distinct   AddressName ,Case_Manager 
           from tableainsert into table1 
         select a.First_Name  ,a.Last_Name  ,a.SS_No,b.AddressID 
         from tablea a, table2 b 
         where a.AddressName=b.AddressName
      

  3.   

    declare @PatientID int,
    @First_Name varchar(8000),
    @Last_Name varchar(8000),
    @SS_No varchar(8000),
    @AddressName varchar(8000),
    @Case_Manager  varchar(8000),
    @AddressID intdeclare tb cursor local
    for
    select PatientID ,First_Name,Last_Name,SS_No,AddressName,Case_Manager
    from [table]open tb
    fetch tb into @PatientID , @First_Name, @Last_Name, @SS_No, @AddressName, @Case_Manager
    while @@fetch_status=0
    begin
    insert table2(AddressName,Case_Manager)
    values(@AddressName, @Case_Manager)
    set @AddressID = scope_identity()

    insert table1(First_Name,Last_Name,SS_No,AddressID)
    values(@First_Name, @Last_Name, @SS_No, @AddressID) fetch tb into @PatientID , @First_Name, @Last_Name, @SS_No, @AddressName, @Case_Manager
    end
    close tb
    deallocate tb