Zé Carlos
Zé Carlos

Reputation: 3807

SQL Server: put select result in variable

When i do for example:

select * from test

This will produce a result with all columns and rows of table test. It is possible to assign this result as "text" in one variable?

Thanks

Upvotes: 1

Views: 3082

Answers (3)

Martin Smith
Martin Smith

Reputation: 452967

This does what you asked for

DECLARE @Result nvarchar(max)
DECLARE @Xml xml = (select * from master..spt_values FOR XML PATH)
SELECT @Result = CAST(@Xml.query('string(.)') as nvarchar(max))
PRINT @Result

I suspect that you probably want some column/row delimiters in there though.

I'm not sure whether this could be altered to do this. My XML skills are a bit lacking.

Upvotes: 1

MAW74656
MAW74656

Reputation: 3539

Just concatenate the fields you want (or all of them) into a varchar(max) or similiar.

Upvotes: 0

x2.
x2.

Reputation: 9668

I think you need to cast all columns as nvarchar with concatinating it.

Upvotes: 0

Related Questions