My question is a bit unusual.
I have a table with 3 columns (SQL Fiddle Link):
ItemIdentifer ItemClasses RelevantItemDetails
Item1 Software <xml document>
Item1 CRM <xml document>
Now, what I need to do is:
Select rows with a given identifier. (say Item1)
Build a temporary table for each of the rows with the given identifier: such that Table_Name is the ItemClass of that row. (so, in this case, I will have 2 tables -- Software and CRM.)
Convert the RelevantItemDetails value => an xml text => into the table entries of the corresponding tables (I know this :)).
I have learnt to do step 3 through openxml page https://learn.microsoft.com/en-us/sql/t-sql/functions/openxml-transact-sql . I created a sql procedure which returns a table from the xml. Now, I just have to insert it into the temporary table I created in step 2 (with both column names and values). And I am lost beyond this.
I suspect that for 1 & 2, I have to build a procedure that does these things dynamically.
Any help will be greatly helpful.
To clarify: I am not getting 2 things:
Build a table dynamically with the ItemClass name as the table name. (so, 2 tables for Item1 here).
The columns and rows of that table are defined by the xml document (i.e. another table).
So, I just need to create tables with name defined by ItemClass and columns and row entries defined by the xml document (assume I have a table created from the xml, it too will be dynamically generated, but I know how to generate it).
I read creating tables dynamically here (T-SQL How to create tables dynamically in stored procedures?) but then the columns are fixed. In my case columns are derived from another table. And I have to insert values too (again, derived from the same table from which columns come).
EDIT: Sample row.
ID ItemName ItemClass Details
10 WebApp Software <root><row ID="10" ItemName="WebApp" ItemDescription="desc" DisplayID="4962" /></root>
EDIT: Sample Final Table.
TableName == Software
ID ItemName ItemDescription DisplayID
10 WebApp "desc" 4962