我有一个窘迫要解决,使用TSql。
以下是整个日志文件中的一组示例数据。
我正在进行将“有效”内容转换为列的过程。除了唯一的XML格式的行,我可以将每一行都放入Columns中。正如问题部分所描述的那样,当涉及到只包含XML元素的行时,如果没有完全轰炸,它就会出错。
我已经能够从解析器中隔离出“无效”行,但是数据所涉及的行与后续信息无关。
内容中包含{DELIM}
的行需要将后续行与其连接,以生成单行。
示例数据:
RowID FileID Contents
-------------------- -------------------- ---------
3595 1 8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3596 1 <LoggedOn>true</LoggedOn>
3597 1 <ProfileLoaded>false</ProfileLoaded>
3598 1 <UsingLocalLogon>false</UsingLocalLogon>
3599 1 <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
3600 1 <ErrorOccured>false</ErrorOccured>
3601 1 <ErrorString />
3602 1 </ImpersonationResultClass>
3629 1 8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3630 1 <LoggedOn>true</LoggedOn>
3631 1 <ProfileLoaded>false</ProfileLoaded>
3632 1 <UsingLocalLogon>false</UsingLocalLogon>
3633 1 <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
3634 1 <ErrorOccured>false</ErrorOccured>
3635 1 <ErrorString />
3636 1 </ImpersonationResultClass>
3653 1 8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3654 1 <LoggedOn>true</LoggedOn>
3655 1 <ProfileLoaded>false</ProfileLoaded>
3656 1 <UsingLocalLogon>false</UsingLocalLogon>
3657 1 <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
3658 1 <ErrorOccured>false</ErrorOccured>
3659 1 <ErrorString />
3660 1 </ImpersonationResultClass>
5239 1 8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5240 1 <LoggedOn>true</LoggedOn>
5241 1 <ProfileLoaded>false</ProfileLoaded>
5242 1 <UsingLocalLogon>false</UsingLocalLogon>
5243 1 <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
5244 1 <ErrorOccured>false</ErrorOccured>
5245 1 <ErrorString />
5246 1 </ImpersonationResultClass>
我可以帮助将后续的XML元素压缩到具有有效DateTime的单行吗?
例如:
RowID FileID Contents
-------------------- -------------------- ---------
3595 1 8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
3629 1 8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
3653 1 8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
5239 1 8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
答案 0 :(得分:0)
我不确定你为什么需要这个,它可能效率不高,可能有更好的方法。
然而,您可能没有其他选择,这是一种方法。
您的数据:
declare @data table(RowID int, FileID int, Contents nvarchar(max))
insert into @data(RowID, FileID, Contents) values
(3595, 1, '8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
, (3596, 1, '<LoggedOn>true</LoggedOn>')
, (3597, 1, '<ProfileLoaded>false</ProfileLoaded>')
, (3598, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
, (3599, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
, (3600, 1, '<ErrorOccured>false</ErrorOccured>')
, (3601, 1, '<ErrorString />')
, (3602, 1, '</ImpersonationResultClass>')
, (3629, 1, '8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
, (3630, 1, '<LoggedOn>true</LoggedOn>')
, (3631, 1, '<ProfileLoaded>false</ProfileLoaded>')
, (3632, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
, (3633, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
, (3634, 1, '<ErrorOccured>false</ErrorOccured>')
, (3635, 1, '<ErrorString />')
, (3636, 1, '</ImpersonationResultClass>')
, (3653, 1, '8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
, (3654, 1, '<LoggedOn>true</LoggedOn>')
, (3655, 1, '<ProfileLoaded>false</ProfileLoaded>')
, (3656, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
, (3657, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
, (3658, 1, '<ErrorOccured>false</ErrorOccured>')
, (3659, 1, '<ErrorString />')
, (3660, 1, '</ImpersonationResultClass>')
, (5239, 1, '8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
, (5240, 1, '<LoggedOn>true</LoggedOn>')
, (5241, 1, '<ProfileLoaded>false</ProfileLoaded>')
, (5242, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
, (5243, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
, (5244, 1, '<ErrorOccured>false</ErrorOccured>')
, (5245, 1, '<ErrorString />')
, (5246, 1, '</ImpersonationResultClass>');
查询:
with rows as(
Select RowID, FileID, Contents
, n = ROW_NUMBER() over(order by RowId)
From @data
Where Contents like '%{DELIM}%'
), cat as (
Select ID = r1.RowID, d.RowID, d.Contents From rows as r1
Left Join rows as r2 on r1.n = r2.n - 1
Left Join @data as d On d.RowID > r1.RowID and (d.RowID < r2.RowID or r2.RowID is null)
)
Select r.RowID, r.FileId
, r.Contents+(
Select c.Contents
From cat as c
Where c.ID = r.RowID
Order By c.RowID
For XML PATH (''), TYPE
).value('.', 'varchar(max)')
From rows r
另一种选择:
with rows as(
Select RowID, FileID, Contents
, n = Case When Contents like '%{DELIM}%' Then
ROW_NUMBER() over(partition by case when Contents like '%{DELIM}%' then 0 else 1 end order by RowId)
Else ROW_NUMBER() over(order by RowId) -
ROW_NUMBER() over(order by case when Contents like '%{DELIM}%' then 1 else 0 end, RowId)
End
From @data
)
Select r.RowID, r.FileId
, FIRST_VALUE(Contents) Over(partition by n order by RowId) + (
Select r2.Contents
From rows as r2
Where r.n = r2.n and Contents not like '%{DELIM}%'
Order By r2.RowID
For XML PATH (''), TYPE
).value('.', 'varchar(max)')
From rows as r
Where r.Contents like '%{DELIM}%'
Order By RowID
输出:
RowID FileId Contents
3595 1 8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
3629 1 8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
3653 1 8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
5239 1 8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>