Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 8-Jan-13 22:01pm
v2
Comments
[no name] 9-Jan-13 3:59am    
please elobrate your question
Kunjammu 9-Jan-13 4:01am    
made some changes in my question
[no name] 9-Jan-13 4:08am    
IN UR TABLE ANY PK COL IS THERE
Kunjammu 9-Jan-13 4:09am    
yes. RECNO is my Pk
MT_ 9-Jan-13 4:08am    
Do you have primary key set on the table ?

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
 
Share this answer
 
v2
Comments
Kunjammu 9-Jan-13 4:09am    
can u plz give me an example
Christian Graus 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 ?
SQL
UPDATE TABLE_NAME SET
COL_NAME=(SELECT COL_NAME FROM TABLE_NAME WHERE RECNO= 1)
 
Share this answer
 
v3
Check following query

SQL
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
 
Share this answer
 
v2
Comments
Kunjammu 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900