Obtaining database information and values in extended propertiesSQL Server 2008 R2 DMV QuestionEasier way to change every user in every database to default schema = dbo?Select values from 2 tables with same propertiesDatabase Properties Error - sys.spt_values missing but dbo.spt_values works?Find database user name from sysprocessesFind if column exist in certain tablesUnion of results of all database query to also include database nameQuerying SQL Server extended properties for tables/views and its columnsmerge like rows then update fk from another table to match the new valuesSQL Server displayed 'Grantor' vs system tables result
Implication of namely
Are British MPs missing the point, with these 'Indicative Votes'?
What was Prahlada's age when his father was killed?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
How can a day be of 24 hours?
Connect points with lines QGIS
Is it possible to get a referendum by a court decision?
Does int main() need a declaration on C++?
Notepad++ delete until colon for every line with replace all
Collected fruit by Seine's banks
Mathematica command that allows it to read my intentions
How to delete logs automatically after a certain time and restart the process that fills up the log file?
Do creatures with a speed 0ft., fly 30ft. (hover) ever touch the ground?
8 billion people in the world, increase each year by 2 precent, how many people will be in 9 months?
How badly should I try to prevent a user from XSSing themselves?
How to keep a dark protagonist who wants to keep his humanity dark?
How could sorcerers who are able to produce/manipulate almost all forms of energy communicate over large distances?
Processor speed limited at 0.4 Ghz
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
What is the difference between NTP and validity in Smith's "Logic: The Laws of Truth"?
Can I hook these wires up to find the connection to a dead outlet?
TikZ: Centering dash pattern between two nodes
Do Iron Man suits sport waste management systems?
Does the Idaho Potato Commission associate potato skins with healthy eating?
Obtaining database information and values in extended properties
SQL Server 2008 R2 DMV QuestionEasier way to change every user in every database to default schema = dbo?Select values from 2 tables with same propertiesDatabase Properties Error - sys.spt_values missing but dbo.spt_values works?Find database user name from sysprocessesFind if column exist in certain tablesUnion of results of all database query to also include database nameQuerying SQL Server extended properties for tables/views and its columnsmerge like rows then update fk from another table to match the new valuesSQL Server displayed 'Grantor' vs system tables result
I need to obtain each DB (together with its information) from SQL Server and I am doing so by the below:
use master;
select *
from sys.databases
where database_id > 4
order by name
I also need to obtain a particular value from extended properties. Currently I am doing the below FOR EACH DB manually to obtain this value:
use <db_name>
select value from sys.extended_properties
where class_desc='DATABASE' and name = 'Application Name'
However, I need to get the value outputted from the last query in a column for each of the DB displayed by the first query respectively. Can this be done?
sql-server
add a comment |
I need to obtain each DB (together with its information) from SQL Server and I am doing so by the below:
use master;
select *
from sys.databases
where database_id > 4
order by name
I also need to obtain a particular value from extended properties. Currently I am doing the below FOR EACH DB manually to obtain this value:
use <db_name>
select value from sys.extended_properties
where class_desc='DATABASE' and name = 'Application Name'
However, I need to get the value outputted from the last query in a column for each of the DB displayed by the first query respectively. Can this be done?
sql-server
add a comment |
I need to obtain each DB (together with its information) from SQL Server and I am doing so by the below:
use master;
select *
from sys.databases
where database_id > 4
order by name
I also need to obtain a particular value from extended properties. Currently I am doing the below FOR EACH DB manually to obtain this value:
use <db_name>
select value from sys.extended_properties
where class_desc='DATABASE' and name = 'Application Name'
However, I need to get the value outputted from the last query in a column for each of the DB displayed by the first query respectively. Can this be done?
sql-server
I need to obtain each DB (together with its information) from SQL Server and I am doing so by the below:
use master;
select *
from sys.databases
where database_id > 4
order by name
I also need to obtain a particular value from extended properties. Currently I am doing the below FOR EACH DB manually to obtain this value:
use <db_name>
select value from sys.extended_properties
where class_desc='DATABASE' and name = 'Application Name'
However, I need to get the value outputted from the last query in a column for each of the DB displayed by the first query respectively. Can this be done?
sql-server
sql-server
asked 3 hours ago
user1930901user1930901
373
373
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You could use a cursor to loop over the databases and store the resultsets in a temporary table
CREATE TABLE #TEMP(Databasename nvarchar(500),
value sql_variant)
DECLARE @sql NVARCHAR(max);
DECLARE @dbname NVARCHAR(500);
DECLARE c cursor LOCAL FAST_FORWARD FOR SELECT NAME
FROM sys.databases
WHERE database_id > 4
ORDER BY NAME;
OPEN c
FETCH NEXT FROM c INTO @dbname;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = '
use '+QUOTENAME(@dbname)+'
SELECT '''+QUOTENAME(@dbname)+''' as databasename,
value
FROM sys.extended_properties
WHERE class_desc=''DATABASE'' and name = ''Application Name''';
INSERT INTO #TEMP
EXEC(@sql);
FETCH NEXT FROM c INTO @dbname;
END
CLOSE c;
DEALLOCATE c;
SELECT * FROM #TEMP;
DROP TABLE #TEMP;
Testing
USE my_test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
USE Test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
Result
Databasename value
[my_test] ValueHere
[Test] ValueHere
add a comment |
Complementing other answers - you can use sp_ineachdb - written by Aaron Bertrand and is part of firstResponderKit from Brent Ozar.
DECLARE @cmd nvarchar(max) = N'
select db_name(), value from sys.extended_properties
where class_desc=''DATABASE'' and name = ''Application Name'';';
EXEC master.dbo.sp_ineachdb @command = @cmd, @user_only = 1;
add a comment |
I'd use dynamic T-SQL to combine the results into a single output.
DECLARE @cmd nvarchar(max);
SET @cmd = N'';
SELECT @cmd = @cmd + CASE WHEN @cmd = N'' THEN N'' ELSE N'
UNION ALL
' END + 'SELECT d.name
, AppName = (
SELECT ep.value
FROM ' + QUOTENAME(d.name) + N'.sys.extended_properties ep
WHERE ep.class_desc = N''DATABASE''
AND ep.name = N''Application Name''
)
, d.*
FROM sys.databases d
WHERE d.database_id = ' + CONVERT(nvarchar(11), d.database_id)
FROM sys.databases d
WHERE d.database_id > 4
AND d.state_desc = N'ONLINE'
AND d.user_access = 0
ORDER BY d.name;
SET @cmd = @cmd + N'
IF EXISTS (
SELECT 1
FROM sys.databases d
WHERE d.user_access <> 0
)
BEGIN
DECLARE @msg varchar(1000);
SET @msg = ''One or more databases are in single user mode, and cannot be accessed by this session.'';
RAISERROR (@msg, 14, 1);
END;
';
PRINT @cmd;
EXEC (@cmd);
The query above creates a T-SQL SELECT statement for each online, multi-user, non-system, database on the instance, and combines them using UNION ALL. It then executes all combined statements in one execution. If any database is not in multi-user mode, an error is generated to make it obvious that one or more databases are not included in the output.
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%2f233746%2fobtaining-database-information-and-values-in-extended-properties%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 could use a cursor to loop over the databases and store the resultsets in a temporary table
CREATE TABLE #TEMP(Databasename nvarchar(500),
value sql_variant)
DECLARE @sql NVARCHAR(max);
DECLARE @dbname NVARCHAR(500);
DECLARE c cursor LOCAL FAST_FORWARD FOR SELECT NAME
FROM sys.databases
WHERE database_id > 4
ORDER BY NAME;
OPEN c
FETCH NEXT FROM c INTO @dbname;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = '
use '+QUOTENAME(@dbname)+'
SELECT '''+QUOTENAME(@dbname)+''' as databasename,
value
FROM sys.extended_properties
WHERE class_desc=''DATABASE'' and name = ''Application Name''';
INSERT INTO #TEMP
EXEC(@sql);
FETCH NEXT FROM c INTO @dbname;
END
CLOSE c;
DEALLOCATE c;
SELECT * FROM #TEMP;
DROP TABLE #TEMP;
Testing
USE my_test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
USE Test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
Result
Databasename value
[my_test] ValueHere
[Test] ValueHere
add a comment |
You could use a cursor to loop over the databases and store the resultsets in a temporary table
CREATE TABLE #TEMP(Databasename nvarchar(500),
value sql_variant)
DECLARE @sql NVARCHAR(max);
DECLARE @dbname NVARCHAR(500);
DECLARE c cursor LOCAL FAST_FORWARD FOR SELECT NAME
FROM sys.databases
WHERE database_id > 4
ORDER BY NAME;
OPEN c
FETCH NEXT FROM c INTO @dbname;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = '
use '+QUOTENAME(@dbname)+'
SELECT '''+QUOTENAME(@dbname)+''' as databasename,
value
FROM sys.extended_properties
WHERE class_desc=''DATABASE'' and name = ''Application Name''';
INSERT INTO #TEMP
EXEC(@sql);
FETCH NEXT FROM c INTO @dbname;
END
CLOSE c;
DEALLOCATE c;
SELECT * FROM #TEMP;
DROP TABLE #TEMP;
Testing
USE my_test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
USE Test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
Result
Databasename value
[my_test] ValueHere
[Test] ValueHere
add a comment |
You could use a cursor to loop over the databases and store the resultsets in a temporary table
CREATE TABLE #TEMP(Databasename nvarchar(500),
value sql_variant)
DECLARE @sql NVARCHAR(max);
DECLARE @dbname NVARCHAR(500);
DECLARE c cursor LOCAL FAST_FORWARD FOR SELECT NAME
FROM sys.databases
WHERE database_id > 4
ORDER BY NAME;
OPEN c
FETCH NEXT FROM c INTO @dbname;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = '
use '+QUOTENAME(@dbname)+'
SELECT '''+QUOTENAME(@dbname)+''' as databasename,
value
FROM sys.extended_properties
WHERE class_desc=''DATABASE'' and name = ''Application Name''';
INSERT INTO #TEMP
EXEC(@sql);
FETCH NEXT FROM c INTO @dbname;
END
CLOSE c;
DEALLOCATE c;
SELECT * FROM #TEMP;
DROP TABLE #TEMP;
Testing
USE my_test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
USE Test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
Result
Databasename value
[my_test] ValueHere
[Test] ValueHere
You could use a cursor to loop over the databases and store the resultsets in a temporary table
CREATE TABLE #TEMP(Databasename nvarchar(500),
value sql_variant)
DECLARE @sql NVARCHAR(max);
DECLARE @dbname NVARCHAR(500);
DECLARE c cursor LOCAL FAST_FORWARD FOR SELECT NAME
FROM sys.databases
WHERE database_id > 4
ORDER BY NAME;
OPEN c
FETCH NEXT FROM c INTO @dbname;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = '
use '+QUOTENAME(@dbname)+'
SELECT '''+QUOTENAME(@dbname)+''' as databasename,
value
FROM sys.extended_properties
WHERE class_desc=''DATABASE'' and name = ''Application Name''';
INSERT INTO #TEMP
EXEC(@sql);
FETCH NEXT FROM c INTO @dbname;
END
CLOSE c;
DEALLOCATE c;
SELECT * FROM #TEMP;
DROP TABLE #TEMP;
Testing
USE my_test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
USE Test;
GO
EXEC sp_addextendedproperty
@name = N'Application Name',
@value = 'ValueHere';
Result
Databasename value
[my_test] ValueHere
[Test] ValueHere
answered 2 hours ago
Randi VertongenRandi Vertongen
4,331924
4,331924
add a comment |
add a comment |
Complementing other answers - you can use sp_ineachdb - written by Aaron Bertrand and is part of firstResponderKit from Brent Ozar.
DECLARE @cmd nvarchar(max) = N'
select db_name(), value from sys.extended_properties
where class_desc=''DATABASE'' and name = ''Application Name'';';
EXEC master.dbo.sp_ineachdb @command = @cmd, @user_only = 1;
add a comment |
Complementing other answers - you can use sp_ineachdb - written by Aaron Bertrand and is part of firstResponderKit from Brent Ozar.
DECLARE @cmd nvarchar(max) = N'
select db_name(), value from sys.extended_properties
where class_desc=''DATABASE'' and name = ''Application Name'';';
EXEC master.dbo.sp_ineachdb @command = @cmd, @user_only = 1;
add a comment |
Complementing other answers - you can use sp_ineachdb - written by Aaron Bertrand and is part of firstResponderKit from Brent Ozar.
DECLARE @cmd nvarchar(max) = N'
select db_name(), value from sys.extended_properties
where class_desc=''DATABASE'' and name = ''Application Name'';';
EXEC master.dbo.sp_ineachdb @command = @cmd, @user_only = 1;
Complementing other answers - you can use sp_ineachdb - written by Aaron Bertrand and is part of firstResponderKit from Brent Ozar.
DECLARE @cmd nvarchar(max) = N'
select db_name(), value from sys.extended_properties
where class_desc=''DATABASE'' and name = ''Application Name'';';
EXEC master.dbo.sp_ineachdb @command = @cmd, @user_only = 1;
answered 2 hours ago
KinKin
54.2k481192
54.2k481192
add a comment |
add a comment |
I'd use dynamic T-SQL to combine the results into a single output.
DECLARE @cmd nvarchar(max);
SET @cmd = N'';
SELECT @cmd = @cmd + CASE WHEN @cmd = N'' THEN N'' ELSE N'
UNION ALL
' END + 'SELECT d.name
, AppName = (
SELECT ep.value
FROM ' + QUOTENAME(d.name) + N'.sys.extended_properties ep
WHERE ep.class_desc = N''DATABASE''
AND ep.name = N''Application Name''
)
, d.*
FROM sys.databases d
WHERE d.database_id = ' + CONVERT(nvarchar(11), d.database_id)
FROM sys.databases d
WHERE d.database_id > 4
AND d.state_desc = N'ONLINE'
AND d.user_access = 0
ORDER BY d.name;
SET @cmd = @cmd + N'
IF EXISTS (
SELECT 1
FROM sys.databases d
WHERE d.user_access <> 0
)
BEGIN
DECLARE @msg varchar(1000);
SET @msg = ''One or more databases are in single user mode, and cannot be accessed by this session.'';
RAISERROR (@msg, 14, 1);
END;
';
PRINT @cmd;
EXEC (@cmd);
The query above creates a T-SQL SELECT statement for each online, multi-user, non-system, database on the instance, and combines them using UNION ALL. It then executes all combined statements in one execution. If any database is not in multi-user mode, an error is generated to make it obvious that one or more databases are not included in the output.
add a comment |
I'd use dynamic T-SQL to combine the results into a single output.
DECLARE @cmd nvarchar(max);
SET @cmd = N'';
SELECT @cmd = @cmd + CASE WHEN @cmd = N'' THEN N'' ELSE N'
UNION ALL
' END + 'SELECT d.name
, AppName = (
SELECT ep.value
FROM ' + QUOTENAME(d.name) + N'.sys.extended_properties ep
WHERE ep.class_desc = N''DATABASE''
AND ep.name = N''Application Name''
)
, d.*
FROM sys.databases d
WHERE d.database_id = ' + CONVERT(nvarchar(11), d.database_id)
FROM sys.databases d
WHERE d.database_id > 4
AND d.state_desc = N'ONLINE'
AND d.user_access = 0
ORDER BY d.name;
SET @cmd = @cmd + N'
IF EXISTS (
SELECT 1
FROM sys.databases d
WHERE d.user_access <> 0
)
BEGIN
DECLARE @msg varchar(1000);
SET @msg = ''One or more databases are in single user mode, and cannot be accessed by this session.'';
RAISERROR (@msg, 14, 1);
END;
';
PRINT @cmd;
EXEC (@cmd);
The query above creates a T-SQL SELECT statement for each online, multi-user, non-system, database on the instance, and combines them using UNION ALL. It then executes all combined statements in one execution. If any database is not in multi-user mode, an error is generated to make it obvious that one or more databases are not included in the output.
add a comment |
I'd use dynamic T-SQL to combine the results into a single output.
DECLARE @cmd nvarchar(max);
SET @cmd = N'';
SELECT @cmd = @cmd + CASE WHEN @cmd = N'' THEN N'' ELSE N'
UNION ALL
' END + 'SELECT d.name
, AppName = (
SELECT ep.value
FROM ' + QUOTENAME(d.name) + N'.sys.extended_properties ep
WHERE ep.class_desc = N''DATABASE''
AND ep.name = N''Application Name''
)
, d.*
FROM sys.databases d
WHERE d.database_id = ' + CONVERT(nvarchar(11), d.database_id)
FROM sys.databases d
WHERE d.database_id > 4
AND d.state_desc = N'ONLINE'
AND d.user_access = 0
ORDER BY d.name;
SET @cmd = @cmd + N'
IF EXISTS (
SELECT 1
FROM sys.databases d
WHERE d.user_access <> 0
)
BEGIN
DECLARE @msg varchar(1000);
SET @msg = ''One or more databases are in single user mode, and cannot be accessed by this session.'';
RAISERROR (@msg, 14, 1);
END;
';
PRINT @cmd;
EXEC (@cmd);
The query above creates a T-SQL SELECT statement for each online, multi-user, non-system, database on the instance, and combines them using UNION ALL. It then executes all combined statements in one execution. If any database is not in multi-user mode, an error is generated to make it obvious that one or more databases are not included in the output.
I'd use dynamic T-SQL to combine the results into a single output.
DECLARE @cmd nvarchar(max);
SET @cmd = N'';
SELECT @cmd = @cmd + CASE WHEN @cmd = N'' THEN N'' ELSE N'
UNION ALL
' END + 'SELECT d.name
, AppName = (
SELECT ep.value
FROM ' + QUOTENAME(d.name) + N'.sys.extended_properties ep
WHERE ep.class_desc = N''DATABASE''
AND ep.name = N''Application Name''
)
, d.*
FROM sys.databases d
WHERE d.database_id = ' + CONVERT(nvarchar(11), d.database_id)
FROM sys.databases d
WHERE d.database_id > 4
AND d.state_desc = N'ONLINE'
AND d.user_access = 0
ORDER BY d.name;
SET @cmd = @cmd + N'
IF EXISTS (
SELECT 1
FROM sys.databases d
WHERE d.user_access <> 0
)
BEGIN
DECLARE @msg varchar(1000);
SET @msg = ''One or more databases are in single user mode, and cannot be accessed by this session.'';
RAISERROR (@msg, 14, 1);
END;
';
PRINT @cmd;
EXEC (@cmd);
The query above creates a T-SQL SELECT statement for each online, multi-user, non-system, database on the instance, and combines them using UNION ALL. It then executes all combined statements in one execution. If any database is not in multi-user mode, an error is generated to make it obvious that one or more databases are not included in the output.
edited 2 hours ago
answered 2 hours ago
Max VernonMax Vernon
52k13114230
52k13114230
add a comment |
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%2f233746%2fobtaining-database-information-and-values-in-extended-properties%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