2

Is it possible to view the SQL used in Cognos's queries?

e.g. To get the XML definition of a report you can use the below SQL (copied from https://stackoverflow.com/a/24335760/361842):

SELECT     CMOBJNAMES.NAME AS ObjName
, CMOBJECTS.PCMID
, CMCLASSES.NAME AS ClassName
, cast(CMOBJPROPS7.spec as xml) ReportDefinition
FROM       CMOBJECTS 
INNER JOIN       CMOBJNAMES ON CMOBJECTS.CMID    = CMOBJNAMES.CMID
INNER JOIN       CMCLASSES       ON CMOBJECTS.CLASSID = CMCLASSES.CLASSID
LEFT OUTER JOIN  CMOBJPROPS7     ON CMOBJECTS.CMID = CMOBJPROPS7.CMID
WHERE     CMOBJECTS.CLASSID IN (10, 37)
ORDER BY CMOBJECTS.PCMID;

... and from that XML you can often find sqltext elements giving the underlying SQL. However, where existing queries are being used it's hard to see where that data's coming from.

I'd like the equivalent of the above SQL to find Query definitions; though so far have been unable to find any such column.

Failing that, is there a way to find this definition through the UI? I looked under Query Studio and found the query's lineage which gives some information about the query columns, but doesn't make the data's source clear.

NB: By query I'm referring to those such as R5BZDDAN_GRAPH in the below screenshot from Query Studio:

Cognos Queries

... which would be referred to in a Cognos report in a way such as:

<query name="Q_DEMO">
  <source>
    <model/>
  </source>
  <selection autoSummary="false">
    <dataItem aggregate="none" name="REG_REG" rollupAggregate="none">
      <expression>[AdvRepData].[Q_R5BZDDAN_GRAPH].[REG_REG]</expression>
    </dataItem>
    <dataItem aggregate="none" name="REG_ORG" rollupAggregate="none">
      <expression>[AdvRepData].[Q_R5BZDDAN_GRAPH].[REG_ORG]</expression>
    </dataItem>
    <!-- ... -->

UPDATE

For the benefit of others, here's an amended version of the above code for pulling back report definitons:

;with recurse
as (

    select Objects.CMID Id, ObjectClasses.Name Class, ObjectNames.NAME Name
    , cast('CognosObjects' as nvarchar(max)) ObjectPath
    from CMOBJECTS Objects
    inner join CMOBJNAMES ObjectNames 
        on ObjectNames.CMID = Objects.CMID 
        and ObjectNames.IsDefault = 1 --only get 1 result per object (could filter on language=English (LocaleId=24 / select LocaleId from CMLOCALES where Locale = 'en'))
    inner join CMCLASSES ObjectClasses on ObjectClasses.CLASSID = Objects.CLASSID
    where Objects.PCMID = objects.CMID --cleaner than selecting on root since not language sensitive
    --where ObjectClasses.NAME = 'root'

    union all 

    select Objects.CMID Id, ObjectClasses.Name Class, ObjectNames.NAME Name
    , r.ObjectPath + '\' + ObjectNames.NAME ObjectPath  --I use a backslash rather than forward slash as using this to build a windows path 
    from recurse r
    inner join CMOBJECTS Objects 
        on objects.PCMID = r.Id 
        and Objects.PCMID != objects.CMID --prevent ouroboros
    inner join CMOBJNAMES ObjectNames 
        on ObjectNames.CMID = Objects.CMID
        and ObjectNames.IsDefault = 1 --only get 1 result per object (could filter on language=English (LocaleId=24 / select LocaleId from CMLOCALES where Locale = 'en'))
    inner join CMCLASSES ObjectClasses 
        on ObjectClasses.CLASSID = Objects.CLASSID

)
select * 
from recurse 
where Class in ('report','query')
order by ObjectPath
Community
  • 1
  • 1
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • All of the query definitions for a report are in the report XML, if that's what you mean. What exactly do you mean by "Query definitions" and "existing queries"? Do you mean query shortcuts inside reports, SQL source in reports? If its in the report then it's in the XML. The tricky part is when a report is made from query subjects in a package - that needs to be evaluated using the report definition against the package and isn't stored anywhere – Nick.Mc Dec 10 '16 at 02:27
  • Thanks @NickMcDermaid. I've now added a screenshot to help clarify my question. I'm familiar with being used to embed sql statements directly in reports; but in this case I'm hoping to understand the pre-defined queries; which I assume are what you call `Query Subjects`; though am not familiar enough with Cognos terminology to be sure. Thanks again. – JohnLBevan Dec 10 '16 at 09:09

1 Answers1

3

Terminology:

  • Query Subject can be considered a table
  • Query Item can be considered a column

For your example the SQL might be defined in the R5BZDDAN_GRAPH query subject which is in turn defined in the Framework Manager model. The framework manager model is defined in a .cpf file which isn't in the content store at all. (it is an XML file though). This file is 'published' to Cognos to make packages.

There is also a cached version of the framework manager file on the actual cognos server (a .cqe file) although it is generally not recommended to rely on this

I say your SQL might be defined. If the query subject is a SQL query subject then that is where it is defined. If If the query subject is a model query subject then it is just a list of query items from other query subjects. These might be from many other query subjects which then have joins defined in Framework Manager. So there is no actual SQL defined there - it gets generated at run time

I'm not sure of your end requirement but there are three other ways to get SQL:

  • In Report Studio you can 'show generated SQL' on each query
  • In Framework Manager you can select one or more query subjects and show generated SQL
  • You can use a monitoring tool on your database to see what SQL is being submitted

If you just want to know how numbers are generated in your report, the most direct solution is to monitor your database.

Lastly keep in mind that in some rare cases, SQL defined in Framework Manager might be altered by the way the report is written

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • Thanks Nick; fantastic answer. FYI: Regarding my requirement, it was primarily just to understand how things fit together in Cognos. That was prompted by my previously having optimised a report for our company having spotted some poorly written sql in a report's `sqltext` definition, then having offered to help optimise a community member's report on the assumption it would look the same, only to come unstuck at seeing the use of queries for which I could not find the SQL. – JohnLBevan Dec 12 '16 at 12:47
  • ps. The community mentioned above is a secure forum; in case anyone has access and is interested, that thread's here: https://community.inforxtreme.com/infor-eam-7i-ee-be-product-group/f/infor-eam-7i-ee-be-product-group---discussion/8187/work-order-print-performance – JohnLBevan Dec 12 '16 at 12:48
  • 1
    Thanks for clarifying. Good luck in your endeavours – Nick.Mc Dec 12 '16 at 22:23