Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Saeed
Starting Member
39 Posts |
Posted - 2002-12-16 : 19:46:44
|
| I have a table with 2 columnsthere is a one to many relationships betwen the first column and the 2nd oneHow do I get to do a spr to return back the related C2s that belong to a C1 Has anyone got any already existing code that might do this.?MyTable1C1 C2==== =====100 10001100 10002100 899101 2002103 999103 3238i.e:execute @MyString =sprGetMeAll 100woudl return '10001,10002,899'so i could form a new tableMyTable2C1(int) S1 (Varchar)=== ====100 '10001,10002,899101 '2002'103 '999,3238' |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
|
|
skillile
Posting Yak Master
208 Posts |
Posted - 2002-12-16 : 22:15:43
|
| This should work as well.DECLARE @X TABLE (c1 varchar(10), c2 varchar(10), list varchar(100))DECLARE @list varchar(8000), @y intINSERT INTO @x VALUES (100, 10001,NULL)INSERT INTO @x VALUES (100, 10002,NULL)INSERT INTO @x VALUES (100, 899,NULL)INSERT INTO @x VALUES (103, 999,NULL)INSERT INTO @x VALUES (103, 3238,NULL)SET @y = (SELECT MIN(c1) FROM @x)WHILE @y IS NOT NULLBEGINSELECT @list = '' + @list + a.c2 + ','FROM @x aWHERE EXISTS ( SELECT 1 FROM @x b WHERE a.c1 = b.c1 AND a.c1 = @y ) SET @list = LEFT(@list,LEN(@list)-1) UPDATE @x SET list = @list WHERE c1 = @y SET @list ='' SET @y = (SELECT MIN(c1) FROM @x WHERE c1 > @y)ENDSELECT c1, list FROM @xGROUP BY c1, list |
 |
|
|
Saeed
Starting Member
39 Posts |
Posted - 2002-12-16 : 22:55:02
|
quote: I have a table with 2 columnsthere is a one to many relationships betwen the first column and the 2nd oneHow do I get to do a spr to return back the related C2s that belong to a C1 Has anyone got any already existing code that might do this.?MyTable1C1 C2==== =====100 10001100 10002100 899101 2002103 999103 3238i.e:execute @MyString =sprGetMeAll 100woudl return '10001,10002,899'so i could form a new tableMyTable2C1(int) S1 (Varchar)=== ====100 '10001,10002,899101 '2002'103 '999,3238'
for those seeking the same solution..This spr does the jobCREATE PROCEDURE sprSlowGetJobs@MCCN int ,@MYSTR varchar(1000) outputasDECLARE @JOBN intDECLARE @CNTR intBeginSET NOCOUNT ON declare con_cursor cursor for select JOBN from MaterialPlan where MCCN = @MCCN Set @MYSTR='' Set @CNTR=0 open con_cursor FETCH NEXT FROM CON_CURSOR into @JOBN WHILE @@FETCH_STATUS =0 BEgin set @CNTR=@CNTR+1 if @CNTR >1 begin set @mystr=@mystr+','+convert(varchar(11),@JobN) end else begin set @mystr=+convert(varchar(11),@JobN) end FETCH NEXT FROM CON_CURSOR into @JOBN END close con_cursor deallocate con_cursor ENDRETURNGO |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-12-16 : 22:58:31
|
| Why would you want to use a cursor to do this? And without an ORDER BY clause, there's nothing to guarantee that the string gets built in the correct order. |
 |
|
|
skillile
Posting Yak Master
208 Posts |
Posted - 2002-12-16 : 23:48:32
|
| And , I think the above code I wrote gives the result needed.slow down to move faster... |
 |
|
|
|
|
|
|
|