I am so close. Now, if you can tell me how to do this part, I will be forever grateful and get out of your forum :)
I generate a resultset based on the values they have entered for the valid attribute names. This may get hairy, but you can see what I am doing. The columns in my resultset that get generated are dynamic based on the code below that makes a column with a value for each XmlFieldName.
The output that gets produced is (notice how is uses whatever I have in the DB for the XmlFieldName for each attribute)
Now the EASY party for you probably but it has stumped me so far. How can I make my sproc return the xml in a variable so that when I do this, @myXML will have my XML in there?
DECLARE @myXML xml
exec <sproc> <someid>, @myXML
select @myXML
thanks!
Brian
I generate a resultset based on the values they have entered for the valid attribute names. This may get hairy, but you can see what I am doing. The columns in my resultset that get generated are dynamic based on the code below that makes a column with a value for each XmlFieldName.
Code:
ALTER PROCEDURE [dbo].[GenerateAddressesForExports] ( @AssociationID bigint ) AS CREATE TABLE #tblDynamicColumnNames ( XmlFieldName nvarchar(50) NOT NULL PRIMARY KEY ) INSERT INTO #tblDynamicColumnNames select XmlFieldName from DefaultAddressFieldDefs union all select XmlFieldName from CustomAddressFieldDefs ca where ca.AssociationId = @AssociationID CREATE TABLE #tblDynamicColumnValues ( EntityID bigint NOT NULL, XmlFieldName nvarchar(50) NOT NULL, StringValue nvarchar(500) NOT NULL ) INSERT INTO #tblDynamicColumnValues select top 50 a.EntityId, daf.XmlFieldName, af.StringValue from defaultaddressfielddefs daf inner join addressfields af on af.AddressFieldDefId = daf.AddressFieldDefId inner join addresses a on a.AddressId = af.Addressid inner join Membership m on m.EntityId = a.EntityId where m.AssociationID = @AssociationId DECLARE @pivotValues nvarchar(1000) SELECT @pivotValues = '' SELECT @pivotValues = @pivotValues + '[' + #tblDynamicColumnNames.XmlFieldName + '],' FROM #tblDynamicColumnNames GROUP BY #tblDynamicColumnNames.XmlFieldName SELECT @pivotValues = LEFT(@pivotValues, LEN(@pivotValues) - 1) DECLARE @sqlString nvarchar(1000) [B]SELECT @sqlString = 'select * from #tblDynamicColumnValues PIVOT (MAX(StringValue) FOR XmlFieldName IN (' + @pivotValues + ')) AS InfoPivot for xml raw(''Address''), ROOT(''Addresses''), TYPE'[/B] EXEC (@sqlString) drop table #tblDynamicColumnValues drop table #tblDynamicColumnNames
Code:
<Addresses> <Address EntityID="22619" City="Conroe" Country="" CountryCode="" State="TX" StreetAddress1="3205 W. Davis, Sp Ed Dept" StreetAddress2="Special Education Dept." StreetAddress3="" ZipCode="77304" /> <Address EntityID="22620" City="Kermit" Country="" CountryCode="" State="TX" StreetAddress1="601 South Poplar" StreetAddress2="" StreetAddress3="" ZipCode="79745" /> <Address EntityID="22621" City="Pleasanton" Country="" CountryCode="" State="TX" StreetAddress1="831 Stadium Drive" StreetAddress2="" StreetAddress3="" ZipCode="78064" /> <Address EntityID="22622" StreetAddress1="920 Burke" StreetAddress2="" /> </Addresses>
DECLARE @myXML xml
exec <sproc> <someid>, @myXML
select @myXML
thanks!
Brian
Originally posted by iburyak
Comment