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?

Is the Gritty Realism variant incompatible with dungeon-based adventures?

More output neurons than labels?

Cine footage fron Saturn V launch's

Can a dragon's breath weapon pass through Leomund's Tiny Hut?

What made Windows ME so crash-prone?

Is this artwork (used in a video game) real?

What exactly is a Hadouken?

What is the word for "event executor"?

Snaking a clogged tub drain

What happens if there is no space for entry stamp in the passport for US visa?

Why doesn't philosophy have higher standards for its arguments?

Is it ethical for a company to ask its employees to move furniture on a weekend?

How can I find what program is preventing my Mac from going to sleep?

Kepler space telescope planets detection

Is it OK to use personal email ID for faculty job applications or should we use (current) institute's ID

Mechanical puzzle ID: Ring, barbell, and four-holed panel

How to say no to more work as a PhD student so I can graduate

Do aircraft cabins have suspension?

How to remove the first colon ':' from a timestamp?

Interviewing with an unmentioned 9 months of sick leave taken during a job

(Piano) is the purpose of sheet music to be played along to? Or a guide for learning and reference during playing?

Alternator dying so junk car?

How could an animal "smell" carbon monoxide?

Can a pizza stone be fixed after soap has been used to clean it?



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;








1















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






























    1















    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


























      1












      1








      1








      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 6 hours ago









      MDCCL

      7,1233 gold badges18 silver badges47 bronze badges




      7,1233 gold badges18 silver badges47 bronze badges










      asked 8 hours ago









      James JenkinsJames Jenkins

      2,4452 gold badges24 silver badges48 bronze badges




      2,4452 gold badges24 silver badges48 bronze badges




















          3 Answers
          3






          active

          oldest

          votes


















          2














          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














            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
                8 hours ago













              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









              2














              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





























                2














                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



























                  2












                  2








                  2







                  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 8 hours ago









                  James Jenkins

                  2,4452 gold badges24 silver badges48 bronze badges




                  2,4452 gold badges24 silver badges48 bronze badges










                  answered 8 hours ago









                  LowlyDBALowlyDBA

                  7,5025 gold badges27 silver badges46 bronze badges




                  7,5025 gold badges27 silver badges46 bronze badges























                      3














                      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



























                        3














                        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

























                          3












                          3








                          3







                          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 6 hours ago









                          Erik DarlingErik Darling

                          25.8k13 gold badges79 silver badges128 bronze badges




                          25.8k13 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
                                8 hours ago















                              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
                                8 hours ago













                              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 8 hours ago









                              ChessbrainChessbrain

                              2201 silver badge7 bronze badges




                              2201 silver badge7 bronze badges












                              • Thank you this works as well.

                                – James Jenkins
                                8 hours ago

















                              • Thank you this works as well.

                                – James Jenkins
                                8 hours ago
















                              Thank you this works as well.

                              – James Jenkins
                              8 hours ago





                              Thank you this works as well.

                              – James Jenkins
                              8 hours ago

















                              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

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480