Home Forums SQL Server 2005 T-SQL (SS2K5) inserting horizontal records from table1 as vertical records in table2 RE: inserting horizontal records from table1 as vertical records in table2

  • used INSERT INTO table2(frmdt,todt)

    SELECT ca.frmdt,ca.todt

    FROM table1 t

    CROSS APPLY(SELECT FDT1, TODT1

    UNION ALL

    SELECT FDT2, TODT2

    UNION ALL

    SELECT FDT3, TODT3) ca(frmdt,todt)

    the records are inserted as needed

    thanks very for the same

    but a slight issue with other fields that is being inserted

    insert into table2(year,frmdt,todt,No,Consno)

    SELECT t.YEAR,ca.frmdt,ca.todt @No,@Consno

    FROM table1 t

    CROSS APPLY(SELECT FDT1, TODT1

    UNION ALL

    SELECT FDT2, TODT2

    UNION ALL

    SELECT FDT3, TODT3) ca(frmdt,todt)

    @No is a count taken from table3

    if @no is 2

    then the No column from the table2 will have data as

    1

    2

    1

    2

    1

    2

    @Consno is a consecutive no

    i am taking max(Consno)+1

    so each record inserted will have

    1

    2

    3

    4....... consecutive nos

    due to union all same no gets inserted in all records