Click here to Skip to main content
12,295,791 members (48,757 online)
Rate this:
 
Please Sign up or sign in to vote.
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
Kunjammu1.1K
Edited 8-Jan-13 22:01pm
v2
Comments
Kunjammu 9-Jan-13 4:01am
   
made some changes in my question
Kunjammu 9-Jan-13 4:09am
   
yes. RECNO is my Pk
Milind_T 9-Jan-13 4:08am
   
Do you have primary key set on the table ?
Kunjammu 9-Jan-13 4:10am
   
yes. RECNO is my Pk
Milind_T 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 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 ?
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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2016
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