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;
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'

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
add a comment |
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'

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
add a comment |
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'

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
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'

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
sql-server sql-server-2016 view query-store
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
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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'
add a comment |
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;
add a comment |
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.
Thank you this works as well.
– James Jenkins
8 hours ago
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'
add a comment |
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'
add a comment |
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'
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'
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
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered 6 hours ago
Erik DarlingErik Darling
25.8k13 gold badges79 silver badges128 bronze badges
25.8k13 gold badges79 silver badges128 bronze badges
add a comment |
add a comment |
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.
Thank you this works as well.
– James Jenkins
8 hours ago
add a comment |
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.
Thank you this works as well.
– James Jenkins
8 hours ago
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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