Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to create procedure for following table ..
let I have the first table::::::

Name no
ABC 123
XYZ 365
XYZ 123

I just want to update that table as follows:

Name no

ABC 123
XYZ 365
ABC 123

Note:no 123 should have its first name at every entry of 123
Posted

1 solution

You have a couple of problems to solve here, but I'll suggest some pointers

First, you have to get a list of all the numbers in the 'no' column, perhaps using a DISTINCT[^] query, perhaps into a temporary table[^]

Second, for each of the 'no' you get in the first query, you have to find all the 'Name' variants, perhaps walking through the temp table with a CURSOR[^] may be useful

Perhaps a select to get the first name that has the same 'no', using LIMIT[^] - only you know the definition of 'first', because that implies order, that we are unaware of

Then an UPDATE[^] of the original source table, using the 'name' you gleaned form the second query, where the 'no' is from the temp cursor row

All the clues are in there :)
 
Share this answer
 
v2
Comments
project virus 10-Jul-12 1:12am    
sounds good i will definitely try it..
project virus 10-Jul-12 1:22am    
do i have to insert values in that temporary table?
barneyman 10-Jul-12 1:32am    
try something like
CREATE TEMPORARY TABLE distinctNumbers AS (SELECT distinct [no] FROM yourTable)
project virus 10-Jul-12 1:25am    
which cursor I have to use Fetch?close?open?declare??
barneyman 10-Jul-12 1:34am    
you declare a cursor, then open it, then fetch from it in a loop, then close it

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