From: Andy B. on
I need to take the table variable @NonexistingHeadlines and build a string
out of all of the values in the column HeadlineTitle. Is there a way to do
it?



From: Tom on
On Mar 30, 3:53 am, "Andy B." <a_bo...(a)sbcglobal.net> wrote:
> I need to take the table variable @NonexistingHeadlines and build a string
> out of all of the values in the column HeadlineTitle. Is there a way to do
> it?

Yes

select Column1 + Column2 + Column3

You will have to cast non character columns to a chararcter data type.
From: Plamen Ratchev on
You can use FOR XML PATH:

DECLARE @s NVARCHAR(MAX);

SET @s = STUFF((SELECT ', ' + HeadlineTitle
FROM @NonexistingHeadlines
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '');

--
Plamen Ratchev
http://www.SQLStudio.com
From: Andy B. on
Will this get all of the rows for the column HeadlineTitle?

create table @NonexistingHeadlines(
HeadlineID int,
HeadlineTitle nvarchar(200),
primary key(HeadlineID),
unique key(HeadlineTitle))
go
insert into NonExistingHeadlines (HeadlineID,HeadlineTitle)
values(1,'test 1'),
(2,'test 2'),
(3,'test 3')
go

I need a string that looks something like this:
"The following Headlines were added to the database:

1. test 1
2. test 2
3. test 3"



"Plamen Ratchev" <Plamen(a)SQLStudio.com> wrote in message
news:5Judnf9Iit0BZizWnZ2dnUVZ_tUAAAAA(a)speakeasy.net...
> You can use FOR XML PATH:
>
> DECLARE @s NVARCHAR(MAX);
>
> SET @s = STUFF((SELECT ', ' + HeadlineTitle
> FROM @NonexistingHeadlines
> FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2,
> '');
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com


From: Plamen Ratchev on
Try this:

SET @s = N'The following Headlines were added to the database:' + NCHAR(13) + NCHAR(10) +
(SELECT CAST(HeadlineId AS NVARCHAR(10)) + N'. ' +
HeadlineTitle + NCHAR(13) + NCHAR(10)
FROM @NonexistingHeadlines
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)');

--
Plamen Ratchev
http://www.SQLStudio.com