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;
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 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
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
Jul 16 at 15:40
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 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
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 Jul 16 at 17:11
Erik DarlingErik Darling
25.9k13 gold badges79 silver badges128 bronze badges
25.9k13 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
Jul 16 at 15:40
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
Jul 16 at 15:40
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 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
add a comment |
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
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