SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select rows from 1 table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nbalraj
Starting Member

21 Posts

Posted - 07/17/2013 :  11:22:57  Show Profile  Send nbalraj a Yahoo! Message  Reply with Quote
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

India
52325 Posts

Posted - 07/17/2013 :  11:39:32  Show Profile  Reply with Quote

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


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

nbalraj
Starting Member

21 Posts

Posted - 07/17/2013 :  13:07:55  Show Profile  Send nbalraj a Yahoo! Message  Reply with Quote
Thanks and it worked great.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/17/2013 :  13:21:55  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000