You must Sign In to post a response.
  • Category: ASP.NET

    copy a column from one datatable to another

    hi i hv two datatable in one i hv stuname,rollno,other fields and in second i hv
    stu_rollno and rank,....i want to copy column rank of datatable2..to datatable1..which have all other fields of student...how to do this....
    plz help
  • #345006
    I think u should join both the table according to their ids.copy is not necessary.if u want than simply copy the whole column and pase to other table
    Thanks & Regards
    ----------------------------
    Zakboys
    (Will DO,Will Fight,Will Win)

  • #345027
    Are you saying that you want to add a new column to a table and populate it
    or populate a column that's already there but is empty? To do that you'll
    need to use an update statement and connect the record in the source table
    to the record in the target table via a primary key. It's not as easy as
    copying/pasting a column in Excel, for example.

    In a SQL Server script this would be something like:

    Alter Table Target Add NewColumn Integer
    Go
    Update Target
    Set Target.NewColumn =
    (Select Source.OldColumn From Source
    Where Source.PrimaryKey = Target.PrimaryKey)

  • #345234
    Hi

    what is the cardinality between these two tables? are there 1-1??

    If YES then you need to perform couple of steps to transfer.

    1) Adding RANK Column to First Table.
    2) Write Update Stmt to update this RANK based on First & Second Table join in Student_No.


    Alter Table First_Table Add RANK Integer
    Go
    Update First_Table
    Set Rank =
    (Select Rank From Second_Table
    Where First_Table.Student_No = Second_Table.Student_No

    Thanks -- Vijaya Kadiyala
    http://www.DotNetVJ.com
    Microsoft MVP


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.