How to copy a row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sethumadhavan
    New Member
    • May 2007
    • 4

    How to copy a row

    Hi

    i want to copy a row and insert that same values in different/same table..

    for example

    Empno LunchAllovance
    1001 50
    1002 50

    here lunch allovance is 50 for all the employees. i want to copy that and paste as a new record for a new employee. The employee number should be changed.... can anyone help me in this please

    Thanks
  • pradeep kaltari
    Recognized Expert New Member
    • May 2007
    • 102

    #2
    Originally posted by sethumadhavan
    Hi

    i want to copy a row and insert that same values in different/same table..

    for example

    Empno LunchAllovance
    1001 50
    1002 50

    here lunch allovance is 50 for all the employees. i want to copy that and paste as a new record for a new employee. The employee number should be changed.... can anyone help me in this please

    Thanks
    Hi,
    1: If the lunchAllowance is same for all the employees then you can set a default value to this column and exclude this column from the insert statement.
    2: You can insert all the other column(s) in the table except the LunchAllowance. The insert statement should be followed by an UPDATE statement which looks somehing like the following:
    Code:
    UPDATE EMPLOYEE 
    SET LunchAllowance= SELECT DISTINCT(LunchAllowance)
                                      FROM EMPLOYEE
    WHERE Empno=<empno_recently_inserted>
    I hope this answers your question.

    Regards,
    Pradeep

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Code:
      INSERT INTO targetTable (field1, field2) (SELECT field1, field2 FROM sourceTable WHERE condition=check)

      Comment

      • sethumadhavan
        New Member
        • May 2007
        • 4

        #4
        Originally posted by Motoma
        Code:
        INSERT INTO targetTable (field1, field2) (SELECT field1, field2 FROM sourceTable WHERE condition=check)


        Thanks for the reply!!!

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          You are welcome! I hope things work out for you; don't hesitate to come back and ask more questions!


          Originally posted by sethumadhavan
          Thanks for the reply!!!

          Comment

          Working...