sql server put a column into a nvarchar
HI Guys,
I'm trying to take a ms sql server 2008 column and merge it into a nvarchar. So instead of being a column it will be come a file. Does anyone know how to do that in a stored procedure?
Say you have a table with a column "Names"
john
mary
jack
joe
How do you merge all these cells into a single string so now it would be:
"john mary jack joe"
thanks.
Re: sql server put a column into a nvarchar
you can use a cursor to achieve this...but nvarchar datatype has a limitation of 4000 char..
Code:
DECLARE @Name as nvarchar(64)
DECLARE @RowToCol as nvarchar(4000)
SET @RowToCol = ''
DECLARE Employee_Cursor CURSOR FOR
SELECT Emp_Name FROM Employee
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Employee_Cursor INTO @Name
SET @RowToCol = @RowToCol + ' ' + @Name
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
SELECT @RowToCol