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 |
tkepongo
Yak Posting Veteran
62 Posts |
Posted - 2011-07-28 : 18:20:53
|
I have three tables. Group table*************************************************************** Group_ID * Group_Name* Clauses * Documents***************************************************************....1......* Engineer.*...5.1,5.2,5.3...n*..MTL01,MTL02,etc.**....2......* IT.......*...1.3,4.2,5.3...n*..MIS01,MIS02,etc.**....3......* Finance. *...5.5,5.1,5.3...n*..EDF02,DFD05,etc.***************************************************************DocToGroup junction table************************* Group_ID * Doc Title**************************....1......*...MTL01..**....1......*...MTL02..**....2......*...MIS01..*************************ClauseToGroup junction table************************* Group_ID * *Clause**************************....1......*...5.1..**....1......*...5.2..**....2......*...4.3..*************************How do I make the Group.Clauses, Group.Documents to automatically concatenate the rows from the junction tables and store it into the fields?I found a code to concatenate the rows to string but I have no idea what to do next. CREATE TABLE #RowsToString (Group_Name varchar(100), RefDoc_Title VARCHAR(100))INSERT INTO #RowsToStringselect * from dbo.jntbl_RefDocToGroupSELECT * FROM #RowsToStringSELECT DISTINCT Group_Name, (SELECT SUBSTRING((SELECT ', ' + RefDoc_TitleFROM #RowsToStringWHERE Group_Name = t.Group_NameFOR XML PATH('')),2,200000)) AS RefDocINTO #RefDocCSVFROM #RowsToString tSELECT * FROM #RefDocCSVdrop table #RefDocCSVDROP TABLE #RowsToString Thanks All! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-29 : 02:51:32
|
[code]UPDATE tSET t.Clauses = STUFF((SELECT ','+ CAST(Clause AS varchar(10)) FROM ClauseToGroup WHERE Group_ID=t.Group_ID FOR XML PATH('')),1,1,''),t.Documents = STUFF((SELECT ','+ [Doc Title] FROM DocToGroup WHERE Group_ID=t.Group_ID FOR XML PATH('')),1,1,'')FROM Group t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|