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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select rows from 1 table

Author  Topic 

nbalraj
Starting Member

21 Posts

Posted - 2013-07-17 : 11:22:57
Hi,

I have a table called tblBooks with the below fields

bookid (pk), booktitle, bookdescription
example record
1, 'SQL for Queries', 'Great book for queries'

I also have another table called tblBooksuggestions with the below fields

suggestionid (pk), bookid(fk),suggestiondesc
example records
1, 1, 'I recommend this book'
2, 1, 'It is for beginners'


The challenge i'm facing or the required output, for any given bookid the query need to return: booktitle, suggestiondesc (all in one field)

'SQL for Queries', 'I recommend this book <br> It is for beginners'

Thanks for any help.





visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-17 : 11:39:32
[code]
SELECT b.booktitle,
STUFF((SELECT CHAR(13) + CHAR(10) + suggestiondesc
FROM tblBookSuggestions
WHERE bookid = b.bookid
ORDER BY suggestionid
FOR XML PATH('')),1,2,'')
FROM tblBooks b
WHERE bookid = @bookid
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

nbalraj
Starting Member

21 Posts

Posted - 2013-07-17 : 13:07:55
Thanks and it worked great.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-17 : 13:21:55
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -