0

I have a query that gave an error that i dont know exactly why gave that error. What is the problem?

   SELECT top 10 [ID]
      ,[EVENTCLASS]
      ,[EVENTNAME]
      ,[EVENTDATA]
      ,[EVENTDATE]
      ,[BYUSER]
      ,[IDENTIFIER]
      ,[ZONE]
      ,[ARCHIVED]
      , metadata.value('(./@Value)[1]', 'nvarchar(max)') as CurrentEvent
FROM [dbo].[EventLog] with(nolock)
  cross apply eventdata.nodes ('/document/data[@name = ''EventStatus'']') as metadata
  where convert (date, eventdate) > '2019-04-15'
  and  metadata.value('(./@value)[1]', 'nvarchar(max)') = 'Recognized'

I have to search in Eventdata column that is an XML like this :

<Document ID="50f1c559-7a2a-4420-8fcb-de1e3d523c1a" Action="CREATE">
  <Data Name="EventTenant" Value="soc" Type="System.String" />
  <Data Name="TargetTenant" Value="soc" Type="System.String" />
  <Data Name="UserId" Value="519" Type="System.Int32" />
  <Data Name="EventStatus" Value="Recognized" Type="System.String" />
  <Data Name="TimeStamp" Value="2019-03-15 12:22:02.095" Type="System.String" />
  <Data Name="NextEventStatus" Value="Exported" Type="System.String" />
  <Data Name="NextEventAppId" Value="003" Type="System.String" />
  <Data Name="DocumentId" Value="50f1c559-7a2a-4420-8fcb-de1e3d523c1a,003" Type="System.String" />
</Document>

I want to search by EventStatus and its value.

1
  • XML is case-sensitive...if you're using XPath: «document/data» s/b «Document/Data»...and also «@name=''EventStatus''» s/b «@Name='EventStatus'»...apart from those there are some other errors are there too... Commented May 6, 2019 at 6:17

1 Answer 1

2

You are shredding on nodes where Name=EventStatus and you use a where clause where Value=Recognized.

Those can be combined into one using exist something like this.

SELECT top 10 [ID]
      ,[EVENTCLASS]
      ,[EVENTNAME]
      ,[EVENTDATA]
      ,[EVENTDATE]
      ,[BYUSER]
      ,[IDENTIFIER]
      ,[ZONE]
      ,[ARCHIVED]
FROM [dbo].[EventLog]
where convert (date, eventdate) > '2019-04-15'
  and eventdata.exist('/Document/Data[@Name = "EventStatus" and @Value = "Recognized"]') = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.