I have two tables CompanyTab and OrderTab .CompanyTab table contain one record for each client while OrderTab table contain multiple orders for clients.
I have data in both table like
CompanyTable
ID Name
1 name1
2 name2
OrderTable
OrderId CompanyTabID
1 1
2 1
3 1
4 1
In my query I want to show all orders in single row.
ID Name Orders
1 name1 1,2,3,4
2 name2 null
Is anybody can help on it.
Thanks
Arvind

Show multiple order records in single row
LtCondor
Brad Alexander
First, this is a bad thing to do:
declare @orders nvarchar(3000)
select @orders = isnull(@orders + ' ', '') + OrderField
from orders where companyID = @companyID
Though I have done it many times myself. If you need to order the output things can go haywire. Check this article: http://www.aspfaq.com/show.asp id=2529
If you are using 2005, the solution using FOR XML works great and doesn't suffer any inconsistencies
Tim5827
_x3m
Do this
Create function dbo.fn_get_Orders(@CompanyID int) as
begin
declare @orders nvarchar(3000)
select @orders = isnull(@orders + ' ', '') + OrderField
from orders where companyID = @companyID
return @Orders
end
This will return the @Orders value with a space delimited string of all the order numbers or whatever else. Then you can simply use it in a select statement
eg
select companyID, CompanyName, dbo.fn_get_orders(companyid) from Company
Amb