Question : Append data in rows to different columns in different table

Hello experts:
Using SQL2000.  I have a table (TblMemCPTCodes) that needs to be udpated with fields that are in a different table (tblCPTCodes).

The TblCPTCodes holds the CPT for each member.  The table has 2 fields MemID and CPT.  This table could holde more than one CPT code per member id.  Example of data:

MemID          CPT
347            98412
347            90841
347            91658
241            95555
241            98546

The TblMemberCPTCodes holds the member id and other info and it has 3 columns for the cpt to be entered.  Currently the CPT in this table is null.  Example of what table with data looks like:

MemID        Provider        CPT1            CPT2           CPT3
347               5555
241               1234
I need to update TblMemberCPTCodes with the CPT codes in TblCPTCodes.  The final table should look like this:

MemID        Provider        CPT1            CPT2           CPT3
347              5555           98412          90841         91658
241              95555         98546

I have attached the code I have created so far but I can't figure out how to use this code to update TblMemberCPTCodes.

Thanks in advance for your help.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
declare @CPTCode varchar(50)
declare @Sql nvarchar(4000)

declare c cursor FAST_FORWARD for
select distinct CPT from TblCPTCodes order by CPT

open c
fetch next from c into @CPTCode

set @Sql = 'select MembID '
while @@Fetch_Status = 0
begin
set @Sql = @Sql + ', Min(Case when CPT = ''' + @CPTCode + ''' then
CPT end) as ''' + @CPTCode + ''' '
fetch next from c into @CPTCode
end
close c
deallocate c
set @Sql = @Sql + ' from TblCPTCodes group by MemID'
print @sql
exec (@sql)

Answer : Append data in rows to different columns in different table

typo on line 24, the name of the table should be #temCodes
Random Solutions  
 
programming4us programming4us