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 |
|
BargainBasement
Starting Member
2 Posts |
Posted - 2007-10-30 : 04:48:21
|
| Hi Folks, Im new to SQL, and I am trying to do the following:I have a table Documents with DocID, Path and FileName.A second table Keywords has KwdID, KeywordStringA third table DocumentKeywords links the two with DocID,KwdID. Multiple keywords are linked to one document.I want to create a SELECT query that makes a result table that contains Path, FileName and Keywords columns where the Keywords column contains entries like "Keyword1,Keyword2,Keyword3" ie. a comma delimited list of keyword strings which have been built from the keywords that associate with a specific document.I found a nice sample herehttp://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-stringwhich shows how to return just the comma delimited string itself:DECLARE @List varchar(100)SELECT @List = COALESCE(@List + ', ', '') + Keywords.KeywordStringFROM DocumentKeywordsWHERE KwdID = 1SELECT @ListI cannot seem to integrate this into the query so that it calculates the string for each row on the fly. My suspicion is that the capability is there. Can somebody point me in the right direction? Thanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2007-10-30 : 08:51:49
|
| try this code it may help youcreate table result_set ([path] varchar(25),[filename] varchar(25),keywords char(50))declare concate_ext cursor scrollfor select * from (select d.[path],d.[filename],k.keywordstring keyword from [document] d left outer joindocumentkeyword dk on d.Docid = dk.docidleft outer join keyword k on k.kwdid=dk.kwdid) #tempdeclare concate_int cursor scrollfor select * from (select d.[path],d.[filename],k.keywordstring keyword from [document] d left outer joindocumentkeyword dk on d.Docid = dk.docidleft outer join keyword k on k.kwdid=dk.kwdid) #tempdeclare @var1 varchar(25)declare @var2 varchar(25)declare @var3 char(50)open concate_extfetch first from concate_ext into @var1,@var2,@var3 --ABSOLUTE {n | @nvar}while @@fetch_status=0begin open concate_int declare @var11 varchar(25) declare @var22 varchar(25) declare @var33 char(50) declare @flag int set @flag=0 fetch first from concate_int into @var11,@var22,@var33 while @@fetch_Status=0 begin if @flag=0 begin set @flag=1 end else begin if @var11=@var1 and @var22=@var2 begin set @var3=@var3+','+@var33 end end fetch next from concate_int into @var11,@var22,@var33 end insert into result_set values(@var1,@var2,@var3) close concate_intfetch next from concate_ext into @var1,@var2,@var3end close concate_extdeallocate concate_intdeallocate concate_ext --,@count1,@count,@var1,@var2,@var3,@path,@filename,@keyword,@flagRahul Arora MCA 07 BatchNCCE Israna, PanipatHRY, INDIA |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-30 : 09:28:38
|
| Also make sure to read the link I postedMadhivananFailing to plan is Planning to fail |
 |
|
|
BargainBasement
Starting Member
2 Posts |
Posted - 2008-01-24 : 01:20:40
|
| I am sorry, it has been some time since I received replies on this thread, I am just getting back to this project now. I wanted to thank madhivanan for the link, and particularly arorarahul.0688 for the great customized and instructive code sample.I will try these methods ASAP. Thanks again. |
 |
|
|
|
|
|
|
|