SQL Server 解决方案:
您可以尝试以下解决方案之一:
IF EXISTS(SELECT * FROM tempdb.sys.columns c
WHERE c.object_id=OBJECT_ID(N'tempdb..#MyTempTable' AND c.name='Age')
BEGIN
INSERT INTO #MyTempTable(Name,Age)
SELECT ... FROM ... CROSS APPLY XmlCol.nodes('/...') a(b)
END
ELSE IF EXISTS(SELECT * FROM tempdb.sys.columns c
WHERE c.object_id=OBJECT_ID(N'tempdb..#MyTempTable' AND c.name='Race')
BEGIN
INSERT INTO #MyTempTable(Name,Race)
SELECT ... FROM ... CROSS APPLY XmlCol.nodes('/...') a(b)
END
或者
INSERT INTO #MyTempTable(Name,Age,Race)
SELECT ... AS Name,
a.b.value('(@Age)[1]','TINYINT') AS Age
a.b.value('(@Race)[1]','NVARCHAR(50)') AS Race, ...
CASE WHEN <condition for Race column> THEN value ELSE NULL AS Race
FROM ... CROSS APPLY XmlCol.nodes('/...') a(b)
注1:因为从系统视图读取数据sys.columns
需要特殊权限(见链接)最简单的解决方案是第二种解决方案。
注意 2:从 SQL Server 2008 开始,您可以使用SPARSE 列来减少那些具有许多 NULL 列的列的存储大小。