Click here to Skip to main content
15,798,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello....
i have two table table1 and table2, table1 have two coloumn id,name and there are three records in table1
id name
7 abc
96 pqr
129 xyz
table1 ids save in table2s mast_id like as 7+96+129
also table2 have two coloumn id,mast_id and one record init
id mast_id
1 7+96+129

whan i retrieve the table2 row it will seprate the mast_id like 7,96 and 129.
Posted
Comments
Perry Bruins 11-Apr-11 5:45am    
I have absolutely no clue what you want to achieve here, please rephrase your question!

try this ------
SQL
declare @Rowresult varchar(80)
select @Rowresult=mastId from table2 where id=1
// @Rowresult='7+96+129'
DECLARE @csv VARCHAR(255)
   SET @csv =@Rowresult;
   with s(start) as(
    SELECT distinct charindex('+','+'+@csv+'+',p)
    FROM
   (
    select * from
   (
     select row_number() over (order by m1.number) p from master..spt_values m1,master..spt_values m2
    ) z
   where p <=len(@csv)+2) x
    ),
    chunks(chunk) as
             (
    select
    substring(@csv,start,(select min(start) from s as s2 where s2.start>s.start)-start-1)
   from s
   where start<len(@csv)+2
     )
    select * from chunks
 
Share this answer
 
Looks like you're concatenating key values to a single column. This should never be done. Instead re-model your tables to contain a foreign key - primary key -like reference.

How to implement that depends if you're talking about data tables in C#, tables in a database or something else. But the basic idea (if I understand the question correctly) would be to have the key from table 1 present in table 2.
 
Share this answer
 

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