@items
表類似於您當前的表。然後有步驟爲每個ItemClass
生成腳本。
DECLARE @n int, -- counter
@i int = 0, -- counter
@tableName nvarchar(255), -- stores name of the current table
@query nvarchar(4000) -- stores a query that creates table
-- creates table like yours
DECLARE @items TABLE (
ID int,
ItemName nvarchar(100),
ItemClass nvarchar(100),
Details xml
)
-- put some data in a table
INSERT INTO @items VALUES
(10, N'WebApp', N'Software', N'<root><row ID="10" ItemName="WebApp" ItemDescription="desc" DisplayID="4962" /></root>'),
(12, N'WebApp', N'Software', N'<root><row ID="12" ItemName="WebApp" ItemDescription="desc" DisplayID="5687" /></root>'),
(11, N'CRMapp', N'CRM', N'<root><row ID="11" ItemName="CRMapp" ItemDescription="desc" DisplayID="823678" /></root>')
-- that table will store data for script generating
DECLARE @tables TABLE (
ID int,
TableName nvarchar(100),
ColumnName sysname,
ColumnValue nvarchar(max)
)
--here we parse XML to get column names and data
INSERT INTO @tables
SELECT i.ID,
i.ItemClass as TableName,
CAST(t.c.query('local-name(.)') AS nvarchar(255)) AS ColumnName,
t.c.value('.', 'nvarchar(max)') ColumnValue
FROM @items i
CROSS APPLY Details.nodes('//@*') as t(c)
-- count tables
SELECT @n = COUNT(DISTINCT TableName)
FROM @tables
-- here we go!
WHILE @n > @i
BEGIN
-- select some table
SELECT TOP 1 @tableName = TableName
FROM @tables
-- in this CTE we get column names in order
;WITH tables_ AS (
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) as rn,
ColumnName
FROM @tables
WHERE TableName = @tableName
)
-- here we build the create part
SELECT @query = N'CREATE TABLE ' + QUOTENAME(@tableName) + '('
+
STUFF((
SELECT N','+ QUOTENAME(ColumnName) +' nvarchar(max)'
FROM tables_
GROUP BY rn,ColumnName
ORDER BY rn
FOR XML PATH('')),1,1,'')
+');
INSERT INTO ' + QUOTENAME(@tableName) +' VALUES'
-- here comes data
;WITH cte AS (
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) as rn,
ID,
ColumnValue
FROM @tables
WHERE TableName = @tableName
)
SELECT @query = @query + STUFF((
SELECT ',('''+ [1] + ''',''' +[2] + ''',''' +[3] + ''',''' +[4]+''')'
FROM cte
PIVOT (max (ColumnValue) for rn in ([1],[2],[3],[4])) as bb
FOR XML PATH('')),1,1,'');
-- print query
PRINT @query
---- execute query (first runt this script to print!)
--EXEC (@query)
SET @i+=1
SET @query = N''
DELETE FROM @tables
WHERE TableName = @tableName
END
,將產生(和執行)的腳本,如:
CREATE TABLE [Software]([ID] nvarchar(max),[ItemName] nvarchar(max),[ItemDescription] nvarchar(max),[DisplayID] nvarchar(max));
INSERT INTO [Software] VALUES('10','WebApp','desc','4962'),('12','WebApp','desc','5687')
CREATE TABLE [CRM]([ID] nvarchar(max),[ItemName] nvarchar(max),[ItemDescription] nvarchar(max),[DisplayID] nvarchar(max));
INSERT INTO [CRM] VALUES('11','CRMapp','desc','823678')
您可以添加檢查,如果表已經存在。還有一些邏輯可以爲表格列獲取正確的數據類型。也許XML中可能有4個以上的屬性。
你的sql小提琴沒有任何xml。它只有字符串「xml」。我們無法幫助您從中創建代碼來創建表格。它是建立在MySQL,而不是SQL服務器。 –
@SeanLange感謝您檢查我的問題。是的,我說我們可以假設我從xml中提取了一個表。所以,而不是xml:考慮任何表格:plz看到這個:http://www.sqlfiddle.com/#!9/6de0da/1/0:我必須從xml table =>獲取列名稱和行和表班級名稱。我找不到任何地方演示sql server。有沒有sql server小提琴的東西? –
所以你只需要幫助來創建表格?在你的問題中,你說你需要那個部分。這是問題。您聲明您知道如何獲取列,但不能創建腳本來創建表。我們無法幫助您,因爲表格和列需要使用相同的語句。 –