Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
Hi,
I have a table with five rows. i want to update the last four rows with the values of first record. First row contain the master record. How can i do that. plz help me
 
Thanks in advance
kunjammu
Posted 8-Jan-13 21:57pm
Edited 8-Jan-13 22:01pm
v2
Comments
Kunjammu at 9-Jan-13 4:01am
   
made some changes in my question
Kunjammu at 9-Jan-13 4:09am
   
yes. RECNO is my Pk
Milind_T at 9-Jan-13 4:08am
   
Do you have primary key set on the table ?
Kunjammu at 9-Jan-13 4:10am
   
yes. RECNO is my Pk
Milind_T at 9-Jan-13 4:15am
   
Good, check the solution below. That should help.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Sorry, I misread the question. You can do this:
 
update table1 set val = ( select top 1 val from table1 order by id)
 
You'd have to do that for each column. You can do it all in one, if you like.
 
update table1 set val = ( select top 1 val from table1 order by id),
val2 = (select top 1 val2 from table1 order by id), etc
  Permalink  
v2
Comments
Kunjammu at 9-Jan-13 4:09am
   
can u plz give me an example
Christian Graus at 9-Jan-13 4:12am
   
Oh, I see. Update table set val = (select top 1 val from table) will do it. I misread your question. Make sure top 1 is selecting the first record, perhaps order by the id to do that ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

 
UPDATE TABLE_NAME SET
COL_NAME=(SELECT COL_NAME FROM TABLE_NAME WHERE RECNO= 1)
 
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check following query
 
UPDATE myTable
SET
column1 = firstrow.column1,
column1 = firstrow.column1,
column1 = firstrow.column1
FROM
    (
    SELECT
    	column1,column2,column3
    FROM MyTable
    WHERE
    	RECNO=1
    ) firstrow
where RECNO<> 1
 
That should serve the purpose
 
Hope that helps
Milind
  Permalink  
v2
Comments
Kunjammu at 9-Jan-13 4:44am
   
thank u...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 239
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,972
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100