How do I assign the results of an exec command to a variable. like the below, so when I do select @sql2 I get the result of the executed varchar sql.
declare @sql varchar(500)
declare @sql2 varchar(max)
set @sql = 'SELECT
PDB.OutletBrandID, OB.BrandName
FROM
ProductDistributionBrand PDB
INNER JOIN
[IND_roadchef].dbo.OutletBrands OB
ON
PDB.OutletBrandID = OB.OutletBrandID
FOR XML PATH(''ProductDistributionBrandDetail''),ROOT(''ProductDistributionBrandDetails''),TYPE'
--select @sql
set @sql2 = exec(@sql)
select @sql2
Use an output param:
declare @sql nvarchar(500)
declare @xml XML
set @sql = 'set @xml = (SELECT ..... FOR XML PATH(''ProductDistributionBrandDetail''),ROOT(''ProductDistributionBrandDetails''),TYPE)'
EXEC sp_executesql @sql, N'@xml XML output', @xml = @xml OUTPUT
select @xml