How to query object attributes in the historian that still exist in the GR?

I want to create a stored procedure that queries for all the tags in Historian that contain a specific string in the name such as "#PV", belong to a specific Area and also currently exist in the IDE.

I am using the "dbo._Tag" table and "dbo._TagExtendedProperty" table, but both these tables return all the historical tags including the deleted tags. I only want currently existing tags. I thought of using the "dbo.History" table with "QualityDetail" property set to "Good" but this table needs the tag names as parameters to work and so I can't use it to get all tags with contains a specific string like "#PV".

This is the query I am currently using: 

SELECT [TagName] as Tag,
[TagExtendedPropertyKey],
[PropertyNameKey],
CAST([PropertyValue] AS NVARCHAR) as AreaLocation,
[ChangeVersion]
FROM [Runtime].[dbo].[_TagExtendedProperty]
WHERE [TagName] LIKE ''%Ai1Scaled#PV''
AND [PropertyNameKey] = ''3'') AS datasource
WHERE [AreaLocation] LIKE ''%' + @Areaname + '%''

Parents
  • Sorry for the late update! and thanks for the suggestions. I am currently using the following to get to values that are active and don't belong to deleted objects: 

    SELECT DISTINCT datasource.Tag
    FROM (
    SELECT tep.[TagName] as Tag,
    CAST(tep.[PropertyValue] AS NVARCHAR) as AreaLocation,
    tep.[ChangeVersion]
    FROM [Runtime].[dbo].[_TagExtendedProperty] tep
    WHERE tep.[TagName] LIKE ''<TAGNAME>''
    AND CAST(tep.[PropertyValue] AS NVARCHAR) LIKE ''<AREANAME>''
    AND tep.[PropertyNameKey] = ''3''
    ) AS datasource
    JOIN ( SELECT His.[TagName], His.[OPCQuality]
    FROM dbo.History His
    WHERE His.TagName LIKE ''<TAGNAME>''
    AND His.OPCQuality = 192) AS history
    ON datasource.Tag = history.TagName
    ORDER BY datasource.Tag ASC

    Basically, I am using the PropertyNameKey property from the dbo._TagExtendedProperty table to filter out the attributes that don't exist (or so I think). 

Reply
  • Sorry for the late update! and thanks for the suggestions. I am currently using the following to get to values that are active and don't belong to deleted objects: 

    SELECT DISTINCT datasource.Tag
    FROM (
    SELECT tep.[TagName] as Tag,
    CAST(tep.[PropertyValue] AS NVARCHAR) as AreaLocation,
    tep.[ChangeVersion]
    FROM [Runtime].[dbo].[_TagExtendedProperty] tep
    WHERE tep.[TagName] LIKE ''<TAGNAME>''
    AND CAST(tep.[PropertyValue] AS NVARCHAR) LIKE ''<AREANAME>''
    AND tep.[PropertyNameKey] = ''3''
    ) AS datasource
    JOIN ( SELECT His.[TagName], His.[OPCQuality]
    FROM dbo.History His
    WHERE His.TagName LIKE ''<TAGNAME>''
    AND His.OPCQuality = 192) AS history
    ON datasource.Tag = history.TagName
    ORDER BY datasource.Tag ASC

    Basically, I am using the PropertyNameKey property from the dbo._TagExtendedProperty table to filter out the attributes that don't exist (or so I think). 

Children
No Data