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

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

                              Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                              Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?