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
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.
I think you need to cast all columns as nvarchar with concatinating it.
Just concatenate the fields you want (or all of them) into a varchar(max) or similiar.
精彩评论