How do I query for system views in a SQL Server database?SQL SERVER - Linked Server and query performanceOracle GoldenGate add trandata errorsIndexed views on double linked serverT-SQL View — How to 'pre-fetch' schema using scalar function, then populate using table queryAutomatically detect table name in MSSQL Server database using stored functionAre views harmful for performance in PostgreSQL?How to create encryption certificate using PowerShell in Windows Server 2012 R2?SQL Server 2016 & 2017 Force Plan failing with reason 8695How can I export Query Store data?sys.dm_db_stats_properties seems to be misbehaving for a small table - how to get the number of records of a table in a different way?

Which Roman general was killed by his own soldiers for not letting them to loot a newly conquered city?

Inadvertently nuked my disk permission structure - why?

Why did Saturn V not head straight to the moon?

Commercial jet accompanied by small plane near Seattle

Reduce column width of table while also aligning values at decimal point

What to do when you reach a conclusion and find out later on that someone else already did?

Why is a dedicated QA team member necessary?

Why is my read in of data taking so long?

What is the lowest-speed bogey a jet fighter can intercept/escort?

Trapped in an ocean Temple in Minecraft?

Can the 2019 UA Artificer's Returning Weapon and Radiant Weapon infusions stack on the same weapon?

Why is chess failing to attract big name sponsors?

How may I concisely assign different values to a variable, depending on another variable?

ろくに and trouble understanding sentence meaning

How can I tell if there was a power cut while I was out?

Keeping an "hot eyeball planet" wet

powerhouse of ideas

401(k) investment after being fired. Do I own it?

Why was Sauron not trying to find the Ring, and instead of preparing for war?

Terence Tao–type books in other fields?

How to write a sincerely religious protagonist without preaching or affirming or judging their worldview?

Knights fighting a steam locomotive they believe is a dragon

Is it normal practice to screen share with a client?

3D Statue Park: U shapes



How do I query for system views in a SQL Server database?


SQL SERVER - Linked Server and query performanceOracle GoldenGate add trandata errorsIndexed views on double linked serverT-SQL View — How to 'pre-fetch' schema using scalar function, then populate using table queryAutomatically detect table name in MSSQL Server database using stored functionAre views harmful for performance in PostgreSQL?How to create encryption certificate using PowerShell in Windows Server 2012 R2?SQL Server 2016 & 2017 Force Plan failing with reason 8695How can I export Query Store data?sys.dm_db_stats_properties seems to be misbehaving for a small table - how to get the number of records of a table in a different way?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have some queries that run against Query Store, the view sys.query_store_wait_stats is not in SQL Server 2016, but it is in SQL Server 2017. I want my queries to work in SQL Server 2016 and SQL Server 2017+.



To accomplish this I used IF EXISTS as below



EXEC sp_query_store_flush_db;

SELECT * INTO Admin.dbo.query_store_runtime_stats FROM sys.query_store_runtime_stats;
SELECT * INTO Admin.dbo.query_store_runtime_stats_interval FROM sys.query_store_runtime_stats_interval;
SELECT * INTO Admin.dbo.query_store_plan FROM sys.query_store_plan;
SELECT * INTO Admin.dbo.query_store_query FROM sys.query_store_query;
SELECT * INTO Admin.dbo.query_store_query_text FROM sys.query_store_query_text;
IF EXISTS(select * FROM sys.views where name = 'query_store_wait_stats') -- View not in SQL Server 2016
Begin
SELECT * INTO Admin.dbo.query_store_wait_stats FROM sys.query_store_wait_stats;
End
SELECT * INTO Admin.dbo.query_context_settings FROM sys.query_context_settings;


Which would seem like a good solution, EXCEPT that, the below does not return any results on SQL Server 2017 database with Query Store active



select * FROM sys.views where name = 'sys.query_store_wait_stats'


Microsoft has this answer: How do I find all views in a database?



USE <database_name>; 
GO
SELECT name AS view_name
,SCHEMA_NAME(schema_id) AS schema_name
,OBJECTPROPERTYEX(object_id,'IsIndexed') AS IsIndexed
,OBJECTPROPERTYEX(object_id,'IsIndexable') AS IsIndexable
,create_date
,modify_date
FROM sys.views;


But it only returns user views, NOT system views.



A couple of simple tests also do not return the system view, which is present. You can try them without a WHERE and not get any system views.



select * FROM sys.views where name = 'sys.query_store_wait_stats'

SELECT *
-- DISTINCT NAME
FROM SYS.OBJECTS
WHERE TYPE IN ('U','V')
AND NAME= 'query_store_wait_stats'


sys.query_store_wait_stats



I could test for SQL Server version, but If the view gets added to SQL Server 2016 in a Service Pack, I want my query to have only checked for the presence of the view.



How do I find (or test for) the presence of a system view?










share|improve this question






























    2















    I have some queries that run against Query Store, the view sys.query_store_wait_stats is not in SQL Server 2016, but it is in SQL Server 2017. I want my queries to work in SQL Server 2016 and SQL Server 2017+.



    To accomplish this I used IF EXISTS as below



    EXEC sp_query_store_flush_db;

    SELECT * INTO Admin.dbo.query_store_runtime_stats FROM sys.query_store_runtime_stats;
    SELECT * INTO Admin.dbo.query_store_runtime_stats_interval FROM sys.query_store_runtime_stats_interval;
    SELECT * INTO Admin.dbo.query_store_plan FROM sys.query_store_plan;
    SELECT * INTO Admin.dbo.query_store_query FROM sys.query_store_query;
    SELECT * INTO Admin.dbo.query_store_query_text FROM sys.query_store_query_text;
    IF EXISTS(select * FROM sys.views where name = 'query_store_wait_stats') -- View not in SQL Server 2016
    Begin
    SELECT * INTO Admin.dbo.query_store_wait_stats FROM sys.query_store_wait_stats;
    End
    SELECT * INTO Admin.dbo.query_context_settings FROM sys.query_context_settings;


    Which would seem like a good solution, EXCEPT that, the below does not return any results on SQL Server 2017 database with Query Store active



    select * FROM sys.views where name = 'sys.query_store_wait_stats'


    Microsoft has this answer: How do I find all views in a database?



    USE <database_name>; 
    GO
    SELECT name AS view_name
    ,SCHEMA_NAME(schema_id) AS schema_name
    ,OBJECTPROPERTYEX(object_id,'IsIndexed') AS IsIndexed
    ,OBJECTPROPERTYEX(object_id,'IsIndexable') AS IsIndexable
    ,create_date
    ,modify_date
    FROM sys.views;


    But it only returns user views, NOT system views.



    A couple of simple tests also do not return the system view, which is present. You can try them without a WHERE and not get any system views.



    select * FROM sys.views where name = 'sys.query_store_wait_stats'

    SELECT *
    -- DISTINCT NAME
    FROM SYS.OBJECTS
    WHERE TYPE IN ('U','V')
    AND NAME= 'query_store_wait_stats'


    sys.query_store_wait_stats



    I could test for SQL Server version, but If the view gets added to SQL Server 2016 in a Service Pack, I want my query to have only checked for the presence of the view.



    How do I find (or test for) the presence of a system view?










    share|improve this question


























      2












      2








      2








      I have some queries that run against Query Store, the view sys.query_store_wait_stats is not in SQL Server 2016, but it is in SQL Server 2017. I want my queries to work in SQL Server 2016 and SQL Server 2017+.



      To accomplish this I used IF EXISTS as below



      EXEC sp_query_store_flush_db;

      SELECT * INTO Admin.dbo.query_store_runtime_stats FROM sys.query_store_runtime_stats;
      SELECT * INTO Admin.dbo.query_store_runtime_stats_interval FROM sys.query_store_runtime_stats_interval;
      SELECT * INTO Admin.dbo.query_store_plan FROM sys.query_store_plan;
      SELECT * INTO Admin.dbo.query_store_query FROM sys.query_store_query;
      SELECT * INTO Admin.dbo.query_store_query_text FROM sys.query_store_query_text;
      IF EXISTS(select * FROM sys.views where name = 'query_store_wait_stats') -- View not in SQL Server 2016
      Begin
      SELECT * INTO Admin.dbo.query_store_wait_stats FROM sys.query_store_wait_stats;
      End
      SELECT * INTO Admin.dbo.query_context_settings FROM sys.query_context_settings;


      Which would seem like a good solution, EXCEPT that, the below does not return any results on SQL Server 2017 database with Query Store active



      select * FROM sys.views where name = 'sys.query_store_wait_stats'


      Microsoft has this answer: How do I find all views in a database?



      USE <database_name>; 
      GO
      SELECT name AS view_name
      ,SCHEMA_NAME(schema_id) AS schema_name
      ,OBJECTPROPERTYEX(object_id,'IsIndexed') AS IsIndexed
      ,OBJECTPROPERTYEX(object_id,'IsIndexable') AS IsIndexable
      ,create_date
      ,modify_date
      FROM sys.views;


      But it only returns user views, NOT system views.



      A couple of simple tests also do not return the system view, which is present. You can try them without a WHERE and not get any system views.



      select * FROM sys.views where name = 'sys.query_store_wait_stats'

      SELECT *
      -- DISTINCT NAME
      FROM SYS.OBJECTS
      WHERE TYPE IN ('U','V')
      AND NAME= 'query_store_wait_stats'


      sys.query_store_wait_stats



      I could test for SQL Server version, but If the view gets added to SQL Server 2016 in a Service Pack, I want my query to have only checked for the presence of the view.



      How do I find (or test for) the presence of a system view?










      share|improve this question
















      I have some queries that run against Query Store, the view sys.query_store_wait_stats is not in SQL Server 2016, but it is in SQL Server 2017. I want my queries to work in SQL Server 2016 and SQL Server 2017+.



      To accomplish this I used IF EXISTS as below



      EXEC sp_query_store_flush_db;

      SELECT * INTO Admin.dbo.query_store_runtime_stats FROM sys.query_store_runtime_stats;
      SELECT * INTO Admin.dbo.query_store_runtime_stats_interval FROM sys.query_store_runtime_stats_interval;
      SELECT * INTO Admin.dbo.query_store_plan FROM sys.query_store_plan;
      SELECT * INTO Admin.dbo.query_store_query FROM sys.query_store_query;
      SELECT * INTO Admin.dbo.query_store_query_text FROM sys.query_store_query_text;
      IF EXISTS(select * FROM sys.views where name = 'query_store_wait_stats') -- View not in SQL Server 2016
      Begin
      SELECT * INTO Admin.dbo.query_store_wait_stats FROM sys.query_store_wait_stats;
      End
      SELECT * INTO Admin.dbo.query_context_settings FROM sys.query_context_settings;


      Which would seem like a good solution, EXCEPT that, the below does not return any results on SQL Server 2017 database with Query Store active



      select * FROM sys.views where name = 'sys.query_store_wait_stats'


      Microsoft has this answer: How do I find all views in a database?



      USE <database_name>; 
      GO
      SELECT name AS view_name
      ,SCHEMA_NAME(schema_id) AS schema_name
      ,OBJECTPROPERTYEX(object_id,'IsIndexed') AS IsIndexed
      ,OBJECTPROPERTYEX(object_id,'IsIndexable') AS IsIndexable
      ,create_date
      ,modify_date
      FROM sys.views;


      But it only returns user views, NOT system views.



      A couple of simple tests also do not return the system view, which is present. You can try them without a WHERE and not get any system views.



      select * FROM sys.views where name = 'sys.query_store_wait_stats'

      SELECT *
      -- DISTINCT NAME
      FROM SYS.OBJECTS
      WHERE TYPE IN ('U','V')
      AND NAME= 'query_store_wait_stats'


      sys.query_store_wait_stats



      I could test for SQL Server version, but If the view gets added to SQL Server 2016 in a Service Pack, I want my query to have only checked for the presence of the view.



      How do I find (or test for) the presence of a system view?







      sql-server sql-server-2016 view query-store






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 16 at 17:17









      MDCCL

      7,1343 gold badges18 silver badges47 bronze badges




      7,1343 gold badges18 silver badges47 bronze badges










      asked Jul 16 at 15:09









      James JenkinsJames Jenkins

      2,4492 gold badges24 silver badges48 bronze badges




      2,4492 gold badges24 silver badges48 bronze badges




















          3 Answers
          3






          active

          oldest

          votes


















          3














          You just need to remove the schema name and use all_views.



          SQL Fiddle: http://www.sqlfiddle.com/#!18/9eecb/50897



          select * from sys.all_views where name = 'query_store_wait_stats'





          share|improve this answer
































            4














            It's not just if objects exist as a whole, you also have to make sure columns line up. Microsoft adds new stuff, and sometimes backports it. It's nice not to rely on version checking, or worry about it failing if someone hasn't rebooted after patching.



            As a related example, when dm_exec_query_stats got information about tempdb spills and I wanted to show those columns, I used code like this to test for them.



            DECLARE @tempdb_spills BIT = 0;

            IF 4 = ( SELECT COUNT(*)
            FROM sys.all_columns AS ac
            WHERE ac.name IN ( 'total_spills', 'last_spills', 'min_spills', 'max_spills' )
            AND OBJECT_NAME(ac.object_id) = 'dm_exec_query_stats' )

            SET @tempdb_spills = 1;





            share|improve this answer






























              1














              You could use the system_views instead of all_views.



              SELECT * FROM sys.system_views WHERE name = 'query_store_wait_stats'


              That way you can avoid selecting user defined views at the same time.






              share|improve this answer























              • Thank you this works as well.

                – James Jenkins
                Jul 16 at 15:40













              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "182"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f243004%2fhow-do-i-query-for-system-views-in-a-sql-server-database%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              You just need to remove the schema name and use all_views.



              SQL Fiddle: http://www.sqlfiddle.com/#!18/9eecb/50897



              select * from sys.all_views where name = 'query_store_wait_stats'





              share|improve this answer





























                3














                You just need to remove the schema name and use all_views.



                SQL Fiddle: http://www.sqlfiddle.com/#!18/9eecb/50897



                select * from sys.all_views where name = 'query_store_wait_stats'





                share|improve this answer



























                  3












                  3








                  3







                  You just need to remove the schema name and use all_views.



                  SQL Fiddle: http://www.sqlfiddle.com/#!18/9eecb/50897



                  select * from sys.all_views where name = 'query_store_wait_stats'





                  share|improve this answer















                  You just need to remove the schema name and use all_views.



                  SQL Fiddle: http://www.sqlfiddle.com/#!18/9eecb/50897



                  select * from sys.all_views where name = 'query_store_wait_stats'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 16 at 15:22









                  James Jenkins

                  2,4492 gold badges24 silver badges48 bronze badges




                  2,4492 gold badges24 silver badges48 bronze badges










                  answered Jul 16 at 15:19









                  LowlyDBALowlyDBA

                  7,5305 gold badges27 silver badges46 bronze badges




                  7,5305 gold badges27 silver badges46 bronze badges























                      4














                      It's not just if objects exist as a whole, you also have to make sure columns line up. Microsoft adds new stuff, and sometimes backports it. It's nice not to rely on version checking, or worry about it failing if someone hasn't rebooted after patching.



                      As a related example, when dm_exec_query_stats got information about tempdb spills and I wanted to show those columns, I used code like this to test for them.



                      DECLARE @tempdb_spills BIT = 0;

                      IF 4 = ( SELECT COUNT(*)
                      FROM sys.all_columns AS ac
                      WHERE ac.name IN ( 'total_spills', 'last_spills', 'min_spills', 'max_spills' )
                      AND OBJECT_NAME(ac.object_id) = 'dm_exec_query_stats' )

                      SET @tempdb_spills = 1;





                      share|improve this answer



























                        4














                        It's not just if objects exist as a whole, you also have to make sure columns line up. Microsoft adds new stuff, and sometimes backports it. It's nice not to rely on version checking, or worry about it failing if someone hasn't rebooted after patching.



                        As a related example, when dm_exec_query_stats got information about tempdb spills and I wanted to show those columns, I used code like this to test for them.



                        DECLARE @tempdb_spills BIT = 0;

                        IF 4 = ( SELECT COUNT(*)
                        FROM sys.all_columns AS ac
                        WHERE ac.name IN ( 'total_spills', 'last_spills', 'min_spills', 'max_spills' )
                        AND OBJECT_NAME(ac.object_id) = 'dm_exec_query_stats' )

                        SET @tempdb_spills = 1;





                        share|improve this answer

























                          4












                          4








                          4







                          It's not just if objects exist as a whole, you also have to make sure columns line up. Microsoft adds new stuff, and sometimes backports it. It's nice not to rely on version checking, or worry about it failing if someone hasn't rebooted after patching.



                          As a related example, when dm_exec_query_stats got information about tempdb spills and I wanted to show those columns, I used code like this to test for them.



                          DECLARE @tempdb_spills BIT = 0;

                          IF 4 = ( SELECT COUNT(*)
                          FROM sys.all_columns AS ac
                          WHERE ac.name IN ( 'total_spills', 'last_spills', 'min_spills', 'max_spills' )
                          AND OBJECT_NAME(ac.object_id) = 'dm_exec_query_stats' )

                          SET @tempdb_spills = 1;





                          share|improve this answer













                          It's not just if objects exist as a whole, you also have to make sure columns line up. Microsoft adds new stuff, and sometimes backports it. It's nice not to rely on version checking, or worry about it failing if someone hasn't rebooted after patching.



                          As a related example, when dm_exec_query_stats got information about tempdb spills and I wanted to show those columns, I used code like this to test for them.



                          DECLARE @tempdb_spills BIT = 0;

                          IF 4 = ( SELECT COUNT(*)
                          FROM sys.all_columns AS ac
                          WHERE ac.name IN ( 'total_spills', 'last_spills', 'min_spills', 'max_spills' )
                          AND OBJECT_NAME(ac.object_id) = 'dm_exec_query_stats' )

                          SET @tempdb_spills = 1;






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 16 at 17:11









                          Erik DarlingErik Darling

                          25.9k13 gold badges79 silver badges128 bronze badges




                          25.9k13 gold badges79 silver badges128 bronze badges





















                              1














                              You could use the system_views instead of all_views.



                              SELECT * FROM sys.system_views WHERE name = 'query_store_wait_stats'


                              That way you can avoid selecting user defined views at the same time.






                              share|improve this answer























                              • Thank you this works as well.

                                – James Jenkins
                                Jul 16 at 15:40















                              1














                              You could use the system_views instead of all_views.



                              SELECT * FROM sys.system_views WHERE name = 'query_store_wait_stats'


                              That way you can avoid selecting user defined views at the same time.






                              share|improve this answer























                              • Thank you this works as well.

                                – James Jenkins
                                Jul 16 at 15:40













                              1












                              1








                              1







                              You could use the system_views instead of all_views.



                              SELECT * FROM sys.system_views WHERE name = 'query_store_wait_stats'


                              That way you can avoid selecting user defined views at the same time.






                              share|improve this answer













                              You could use the system_views instead of all_views.



                              SELECT * FROM sys.system_views WHERE name = 'query_store_wait_stats'


                              That way you can avoid selecting user defined views at the same time.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 16 at 15:26









                              ChessbrainChessbrain

                              2651 silver badge8 bronze badges




                              2651 silver badge8 bronze badges












                              • Thank you this works as well.

                                – James Jenkins
                                Jul 16 at 15:40

















                              • Thank you this works as well.

                                – James Jenkins
                                Jul 16 at 15:40
















                              Thank you this works as well.

                              – James Jenkins
                              Jul 16 at 15:40





                              Thank you this works as well.

                              – James Jenkins
                              Jul 16 at 15:40

















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Database Administrators Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f243004%2fhow-do-i-query-for-system-views-in-a-sql-server-database%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                              Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                              Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form