469,936 Members | 2,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

Mssql

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

Can you help me?
Aug 1 '07 #1
2 775
mwasif
802 Expert 512MB
Moved to MS SQL forum.
Aug 1 '07 #2
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.

Expand|Select|Wrap|Line Numbers
  1. --Create our test table
  2. Create table table1
  3. (ID int, TextField varchar(10))
  4.  
  5. --Insert a few test rows with a key and a text data field
  6. Insert into table1
  7. Select 1, 'Test 1'
  8. Union All
  9. Select 2, 'Test 2'
  10. Union All
  11. Select 3, 'Test 3'
  12. Union All
  13. Select 4, 'Test 4'
  14.  
  15. --Display our table 
  16. Select * From table1
  17.  
  18.  
  19. /*
  20. Results
  21. ID          TextField
  22. ----------- ----------
  23. 1           Test 1
  24. 2           Test 2
  25. 3           Test 3
  26. 4           Test 4
  27.  
  28. (4 row(s) affected)
  29. */
  30.  
  31. (4 row(s) affected)
  32.  
  33. --Update the contents of record 3 with the text data from record 1
  34. Update table1
  35. Set TextField = A.TextField
  36. From (Select * From table1 Where ID = 1) A
  37. Where table1.ID = 3
  38.  
  39. --Display our table again with the new updated record 3
  40. Select * From table1
  41.  
  42. /*Results
  43. ID          TextField
  44. ----------- ----------
  45. 1           Test 1
  46. 2           Test 2
  47. 3           Test 1
  48. 4           Test 4
  49.  
  50. (4 row(s) affected)
  51. */
  52.  
Aug 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by badz | last post: by
7 posts views Thread by mj | last post: by
14 posts views Thread by Kukurydz | last post: by
11 posts views Thread by ralphie | last post: by
8 posts views Thread by php-taz | last post: by
14 posts views Thread by guswebb | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.