Archive for May, 2009
Comma-Delimited Output (SQL Server)
12 years ago
by Solomon
in SQL Server
Using Query:
use Northwind
GO
declare @CustIDs varchar(8000)
select top 10
@CustIDs = isnull(@CustIDs + ',', '') + CustomerID
from Customers
select @CustIDs
GO
Using Cursor:
declare cCustomerIDs cursor for
select [CustomerID] from [dbo].[Customers] order by [CustomerID]
declare @CustomerIDs varchar(8000)
declare @CustomerID varchar(10)
open cCustomerIDs
fetch next from cCustomerIDs into @CustomerID
while @@FETCH_STATUS = 0
begin
set @CustomerIDs = isnull(@CustomerIDs + ',', '') + @CustomerID
fetch next from cCustomerIDs into @CustomerID
end
close cCustomerIDs
deallocate cCustomerIDs
select @CustomerIDs as CustomerIDs
GO