Mssql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alla
    New Member
    • Aug 2007
    • 1

    #1

    Mssql

    I have to copy text field from one record to another on the same table.

    Can you help me?
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Moved to MS SQL forum.

    Comment

    • bwestover
      New Member
      • Jul 2007
      • 39

      #3
      Originally posted by Alla
      I have to copy text field from one record to another on the same table.

      Can you help me?
      I dont know if this is the best way, but it works for me. I use a subquery in the FROM statement.

      Code:
      --Create our test table
      Create table table1
      (ID int, TextField varchar(10))
      
      --Insert a few test rows with a key and a text data field
      Insert into table1
      Select 1, 'Test 1'
      Union All
      Select 2, 'Test 2'
      Union All
      Select 3, 'Test 3'
      Union All
      Select 4, 'Test 4'
      
      --Display our table 
      Select * From table1
      
      
      /*
      Results
      ID          TextField
      ----------- ----------
      1           Test 1
      2           Test 2
      3           Test 3
      4           Test 4
      
      (4 row(s) affected)
      */
      
      (4 row(s) affected)
      
      --Update the contents of record 3 with the text data from record 1
      Update table1
      Set TextField = A.TextField
      From (Select * From table1 Where ID = 1) A
      Where table1.ID = 3
      
      --Display our table again with the new updated record 3
      Select * From table1
      
      /*Results
      ID          TextField
      ----------- ----------
      1           Test 1
      2           Test 2
      3           Test 1
      4           Test 4
      
      (4 row(s) affected)
      */

      Comment

      Working...