{"id":70,"date":"2008-07-22T22:01:12","date_gmt":"2008-07-23T03:01:12","guid":{"rendered":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/?p=70"},"modified":"2008-07-24T21:00:01","modified_gmt":"2008-07-25T02:00:01","slug":"ssas-2008-dmvs-querying-from-the-sql-server-and-database-diagrams","status":"publish","type":"post","link":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/70_ssas-2008-dmvs-querying-from-the-sql-server-and-database-diagrams","title":{"rendered":"SSAS 2008 DMVs &#8211; querying from the SQL Server and database diagrams"},"content":{"rendered":"<p>In Analysis Services 2008 Microsoft introduced many\u00a0schema rowsets that are very similar to SQL Server Data Management Views (DMVs). Although in Books Online Microsoft refers to these new tables as &#8220;schema rowsets&#8221;, it is just easier for now to call them DMVs. You can use these DMVs to query SSAS 2008 metadata and actual data. Here is the <a href=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/39_katmai-analysis-services-2008-november-ctp5-tests-on-metadata-rowsets\" target=\"_blank\">link<\/a>\u00a0 to my original post about DMVs.<\/p>\n<p>You can execute queries on DMVs directly in Analysis Services. For example you can connect to Analysis Services from SQL Server Management Studio, choose menu item &#8220;File&#8221;-&gt;&#8221;New&#8221;-&gt;&#8221;Analysis Services MDX Query&#8221; and write your query to select data from DMV.<\/p>\n<p>You can also execute queries against SSAS DMVs from SQL Server environment.\u00a0At this point I believe this is going to be preferred method to query SSAS DMVs. This is because queries on DMVs in Analysis Services has many limitations, yet these limitations do not exists for queries\u00a0from SQL Server linked server. Example of such very important limitation &#8211; in Analysis services you cannot join 2 DMVs.<\/p>\n<p><!--more-->To query DMVs from SQL Server\u00a0you will need to create linked server that points to Analysis Services database. Here is example of the code that creates linked server named SSAS2008Test:<\/p>\n<blockquote><p>EXEC master.dbo.sp_addlinkedserver<br \/>\n\u00a0\u00a0 @server = N&#8217;SSAS2008Test&#8217;<br \/>\n\u00a0, @srvproduct=N&#8217;MSOLAP&#8217;<br \/>\n\u00a0, @provider=N&#8217;MSOLAP&#8217;<br \/>\n\u00a0, @datasrc=N&#8217;VirtualPC1&#8242; &#8212; Server Name<br \/>\n\u00a0, @catalog=N&#8217;Adventure Works DW&#8217; &#8212; Database Name<br \/>\n\u00a0go<br \/>\n\u00a0<br \/>\n\u00a0EXEC master.dbo.sp_addlinkedsrvlogin<br \/>\n\u00a0\u00a0 @rmtsrvname=N&#8217;SSAS2008Test&#8217;<br \/>\n\u00a0, @useself=N&#8217;False&#8217;<br \/>\n\u00a0, @locallogin=NULL<br \/>\n\u00a0, @rmtuser=NULL<br \/>\n\u00a0, @rmtpassword=NULL<br \/>\nGO<\/p><\/blockquote>\n<p>After linked server is created, you can query DMVs using OpenQuery function. For example, this is the query executed from SQL Server to get Analysis Services connection information:<\/p>\n<blockquote><p>SELECT *<br \/>\n\u00a0 FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $SYSTEM.DISCOVER_CONNECTIONS&#8217;)<\/p><\/blockquote>\n<p>After setting up SSAS linked server I decided to create database diagram from SSAS DMVs. This looked like an easy task.\u00a0For each DMV I\u00a0create a statement that selects data from DMV and selects data into SQL Server table. I actually created statement that creates these statements:<\/p>\n<blockquote><p>SELECT &#8216;SELECT * INTO &#8216; + SUBSTRING(TABLE_NAME,1,100)<br \/>\n+ &#8216; FROM OPENQUERY(SSAS2008Test, &#8221;SELECT * FROM $SYSTEM.&#8217; + SUBSTRING(TABLE_NAME,1,100) + &#8221;&#8217;)&#8217;<br \/>\n\u00a0FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $system.dbschema_tables&#8217;) as s<br \/>\nWHERE SUBSTRING(TABLE_SCHEMA,1,100) = N&#8217;$SYSTEM&#8217;\u00a0<\/p><\/blockquote>\n<p>Result of the query above is:<\/p>\n<blockquote><p>SELECT * INTO DBSCHEMA_CATALOGS<br \/>\n\u00a0 FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $SYSTEM.DBSCHEMA_CATALOGS&#8217;)<br \/>\nSELECT * INTO DBSCHEMA_TABLES<br \/>\n\u00a0 FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $SYSTEM.DBSCHEMA_TABLES&#8217;)<br \/>\nSELECT * INTO DBSCHEMA_COLUMNS<br \/>\n\u00a0 FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $SYSTEM.DBSCHEMA_COLUMNS&#8217;)<br \/>\nSELECT * INTO DBSCHEMA_PROVIDER_TYPES<br \/>\n\u00a0 FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $SYSTEM.DBSCHEMA_PROVIDER_TYPES&#8217;)<br \/>\nSELECT * INTO MDSCHEMA_CUBES<br \/>\n\u00a0 FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $SYSTEM.MDSCHEMA_CUBES&#8217;)<br \/>\n&#8230;.<\/p><\/blockquote>\n<p>Then I executed each query. It was a bit more complicated then I thought it is going to be:<\/p>\n<ul>\n<li>I\u00a0had to change some queries to use restrictions using SYSTEMRESTRICTSCHEMA functions.<\/li>\n<li>Some other queries were giving me error messages, that I reported to Microsoft. For example I could not run query on DMV: $SYSTEM.MDSCHEMA_MEMBERS. Error message was: &#8220;Server: The operation has been cancelled due to memory pressure.&#8221;. I went around this problem by selecting TOP 0 records, as all I wanted for this exercise was table structure. I am sure these errors will be fixed in final SQL Server release (I was doing these tests on RC0).<\/li>\n<li>SQL Server was reporting errors if I tried to select from DMVs that contained certain type of field. For example, if I tried to query data from $SYSTEM.MDSCHEMA_MEASUREGROUP_DIMENSIONS DMV, I was getting error message: &#8220;The OLE DB provider &#8220;MSOLAP&#8221; for linked server &#8220;SSAS2008Test&#8221; supplied invalid metadata for column &#8220;DIMENSION_PATH&#8221;. The data type is not supported.&#8221; This field &#8220;DIMENSION_PATH&#8221; is clearly special field, as when you query it directly in SSAS, you can see [+} sign by it side that you can click on and expand it. For my tests I simply excluded this field from the queries I run from linked SQL Server, but then latter I added it to the new SQL table with the type NTEXT.<\/li>\n<\/ul>\n<p>After this exercise I had SQL Server database with the SSAS DMVs structure.\u00a0Based on these tables I created 4 database\u00a0diagrams. I\u00a0divided tables into database diagrams based on their name prefix.<\/p>\n<p>DBSCHEMA tables:<\/p>\n<p><a href=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dbschema.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-71\" title=\"dbschema\" src=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dbschema-257x300.jpg\" alt=\"DBSCHEMA tables\" width=\"257\" height=\"300\" srcset=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dbschema-257x300.jpg 257w, http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dbschema.jpg 772w\" sizes=\"(max-width: 257px) 100vw, 257px\" \/><\/a><\/p>\n<p>DISCOVER tables<\/p>\n<p><a href=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/discover.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-72\" title=\"DISCOVER tables\" src=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/discover-241x300.jpg\" alt=\"DISCOVER tables\" width=\"241\" height=\"300\" srcset=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/discover-241x300.jpg 241w, http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/discover.jpg 1347w\" sizes=\"(max-width: 241px) 100vw, 241px\" \/><\/a><br \/>\n\u00a0<\/p>\n<p>DMSCHEMA tables<\/p>\n<p><a href=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dmschema.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-73\" title=\"DMSCHEMA tables\" src=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dmschema-231x300.jpg\" alt=\"DMSCHEMA tables\" width=\"231\" height=\"300\" srcset=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dmschema-231x300.jpg 231w, http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/dmschema.jpg 1084w\" sizes=\"(max-width: 231px) 100vw, 231px\" \/><\/a><\/p>\n<p>MDSCHEMA tables<\/p>\n<p><a href=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/mdschema.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-74\" title=\"MDSCHEMA tables\" src=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/mdschema-239x300.jpg\" alt=\"MDSCHEMA tables\" width=\"239\" height=\"300\" srcset=\"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/mdschema-239x300.jpg 239w, http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-content\/uploads\/2008\/07\/mdschema.jpg 1287w\" sizes=\"(max-width: 239px) 100vw, 239px\" \/><\/a><\/p>\n<p>One thing that you will notice is that in all DMVs all string type fields are treated as type NTEXT. This will affect how you write queries from SQL Server. For example, if you execute following query:<\/p>\n<blockquote><p>SELECT *<br \/>\n\u00a0FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $system.dbschema_tables&#8217;) as s<br \/>\nWHERE TABLE_SCHEMA = N&#8217;$SYSTEM&#8217;<\/p><\/blockquote>\n<p>You will get this error message:<\/p>\n<blockquote><p>Msg 402, Level 16, State 1, Line 3<br \/>\nThe data types ntext and nvarchar are incompatible in the equal to operator.<\/p><\/blockquote>\n<p>To get results you will have to adjust your query, for example\u00a0by converting TABLE_SCHEMA field to varchar type:<\/p>\n<blockquote><p>SELECT *<br \/>\n\u00a0FROM OPENQUERY(SSAS2008Test, &#8216;SELECT * FROM $system.dbschema_tables&#8217;) as s<br \/>\nWHERE SUBSTRING(TABLE_SCHEMA,1,100) = N&#8217;$SYSTEM&#8217;<\/p><\/blockquote>\n<p>Note: Database diagrams above do not have primary keys or relationships defined between them (yet).<\/p>\n<p><strong>July 24, 2008 Note:<\/strong> Some of the database diagrams were updated &#8211; I added missing DMVs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Analysis Services 2008 Microsoft introduced many schema rowsets that are very similar to SQL Server Data Management Views (DMVs). Although in Books Online Microsoft refers to these new tables as &#8220;schema rowsets&#8221;, it is just easier for now to call them DMVs. You can use these DMVs to query SSAS 2008 metadata and actual data. Here is the link  to my original post about DMVs.<\/p>\n<p>You can execute queries on DMVs directly in Analysis Services. For example you can connect to Analysis Services from SQL Server Management Studio, choose menu item &#8220;File&#8221;->&#8221;New&#8221;->&#8221;Analysis Services MDX Query&#8221; and write your query to select data from DMV.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[9,8],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":0,"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ssas-info.com\/VidasMatelisBlog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}