Create dynamic SQL query to list all users from over all databases
I want to create a dynamic query to list all unique users in a single table, from which I only need the username, from all different databases, which have the same database schema.
As the names of the tables differ on the different environments I would like to have something dynamic so it can be executed on different environments.
The basic query looks like this:
SELECT USERNAME FROM DATABASE.DBO.USERTABLE WHERE LOGIN = 'Y'
To execute this query on 3 databases, it would look like this:
SELECT *
FROM ( SELECT UT.USERNAME AS 'DB1'
FROM DB1.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) a
full outer join
( SELECT UT.USERNAME AS 'DB2'
FROM DOSSIER.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) b ON a.DB1 = b.DB2
full outer join
( SELECT UT.USERNAME AS 'DB3'
FROM DB3.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) c ON a.DB1 = c.DB3
ORDER BY A.DB1
Is there an easy way to create a dynamic query for this, which can be applied on multiple databases?
I've already tried the code below, but I found it hard to get the JOIN ON working.
DECLARE @Sql NVARCHAR(MAX) = NULL;
SELECT @Sql = COALESCE(@Sql + ' AS '+ name + ' FULL OUTER JOIN ON' + CHAR(13) + CHAR(10), '' ) + 'SELECT USERNAME AS ' + name + ' FROM [' + name + '].[DBO].[USERTABLE] AS '+ name
FROM master.sys.databases
WHERE NOT [name] IN ( 'master', 'tempdb', 'model', 'msdb')
PRINT @Sql;
So what I would like to create is the thing below.
SELECT USERNAME
FROM [DATA].[DBO].[USERTABLE]
RESULTS
USER1
USER2
USER3
SELECT USERNAME
FROM [OTHER].[DBO].[USERTABLE]
RESULTS
USER3
USER4
USER6
SELECT USERNAME
FROM [NEW].[DBO].[USERTABLE]
RESULTS
USER1
USER6
USER7
SELECT USERNAME
FROM [SOME].[DBO].[USERTABLE]
RESULTS
USER2
USER5
USER7
So the resulting query should present the list below.
DATA____OTHER___NEW_____SOME____
USER1___________USER1___________
USER2___________________USER2___
USER3___USER3___________________
________USER4___________________
________________________USER5___
________USER6___USER6___________
________________USER7___USER7___
As mentioned, this has te be dynamic, as:
1. I don't know before how many databases are on an environment;
2. It has to be easy usable for anyone;
sql dynamic
add a comment |
I want to create a dynamic query to list all unique users in a single table, from which I only need the username, from all different databases, which have the same database schema.
As the names of the tables differ on the different environments I would like to have something dynamic so it can be executed on different environments.
The basic query looks like this:
SELECT USERNAME FROM DATABASE.DBO.USERTABLE WHERE LOGIN = 'Y'
To execute this query on 3 databases, it would look like this:
SELECT *
FROM ( SELECT UT.USERNAME AS 'DB1'
FROM DB1.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) a
full outer join
( SELECT UT.USERNAME AS 'DB2'
FROM DOSSIER.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) b ON a.DB1 = b.DB2
full outer join
( SELECT UT.USERNAME AS 'DB3'
FROM DB3.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) c ON a.DB1 = c.DB3
ORDER BY A.DB1
Is there an easy way to create a dynamic query for this, which can be applied on multiple databases?
I've already tried the code below, but I found it hard to get the JOIN ON working.
DECLARE @Sql NVARCHAR(MAX) = NULL;
SELECT @Sql = COALESCE(@Sql + ' AS '+ name + ' FULL OUTER JOIN ON' + CHAR(13) + CHAR(10), '' ) + 'SELECT USERNAME AS ' + name + ' FROM [' + name + '].[DBO].[USERTABLE] AS '+ name
FROM master.sys.databases
WHERE NOT [name] IN ( 'master', 'tempdb', 'model', 'msdb')
PRINT @Sql;
So what I would like to create is the thing below.
SELECT USERNAME
FROM [DATA].[DBO].[USERTABLE]
RESULTS
USER1
USER2
USER3
SELECT USERNAME
FROM [OTHER].[DBO].[USERTABLE]
RESULTS
USER3
USER4
USER6
SELECT USERNAME
FROM [NEW].[DBO].[USERTABLE]
RESULTS
USER1
USER6
USER7
SELECT USERNAME
FROM [SOME].[DBO].[USERTABLE]
RESULTS
USER2
USER5
USER7
So the resulting query should present the list below.
DATA____OTHER___NEW_____SOME____
USER1___________USER1___________
USER2___________________USER2___
USER3___USER3___________________
________USER4___________________
________________________USER5___
________USER6___USER6___________
________________USER7___USER7___
As mentioned, this has te be dynamic, as:
1. I don't know before how many databases are on an environment;
2. It has to be easy usable for anyone;
sql dynamic
What about UNION ALL instead of outer join?
– jarlh
Nov 28 '18 at 10:20
Hi jarlh, thank you for your suggestion. This however isn't what I'm looking for. I need 1 column from each database, joined on another column. The union all puts the data from all the columns in 1 column.
– Billy Who
Dec 19 '18 at 11:08
add a comment |
I want to create a dynamic query to list all unique users in a single table, from which I only need the username, from all different databases, which have the same database schema.
As the names of the tables differ on the different environments I would like to have something dynamic so it can be executed on different environments.
The basic query looks like this:
SELECT USERNAME FROM DATABASE.DBO.USERTABLE WHERE LOGIN = 'Y'
To execute this query on 3 databases, it would look like this:
SELECT *
FROM ( SELECT UT.USERNAME AS 'DB1'
FROM DB1.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) a
full outer join
( SELECT UT.USERNAME AS 'DB2'
FROM DOSSIER.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) b ON a.DB1 = b.DB2
full outer join
( SELECT UT.USERNAME AS 'DB3'
FROM DB3.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) c ON a.DB1 = c.DB3
ORDER BY A.DB1
Is there an easy way to create a dynamic query for this, which can be applied on multiple databases?
I've already tried the code below, but I found it hard to get the JOIN ON working.
DECLARE @Sql NVARCHAR(MAX) = NULL;
SELECT @Sql = COALESCE(@Sql + ' AS '+ name + ' FULL OUTER JOIN ON' + CHAR(13) + CHAR(10), '' ) + 'SELECT USERNAME AS ' + name + ' FROM [' + name + '].[DBO].[USERTABLE] AS '+ name
FROM master.sys.databases
WHERE NOT [name] IN ( 'master', 'tempdb', 'model', 'msdb')
PRINT @Sql;
So what I would like to create is the thing below.
SELECT USERNAME
FROM [DATA].[DBO].[USERTABLE]
RESULTS
USER1
USER2
USER3
SELECT USERNAME
FROM [OTHER].[DBO].[USERTABLE]
RESULTS
USER3
USER4
USER6
SELECT USERNAME
FROM [NEW].[DBO].[USERTABLE]
RESULTS
USER1
USER6
USER7
SELECT USERNAME
FROM [SOME].[DBO].[USERTABLE]
RESULTS
USER2
USER5
USER7
So the resulting query should present the list below.
DATA____OTHER___NEW_____SOME____
USER1___________USER1___________
USER2___________________USER2___
USER3___USER3___________________
________USER4___________________
________________________USER5___
________USER6___USER6___________
________________USER7___USER7___
As mentioned, this has te be dynamic, as:
1. I don't know before how many databases are on an environment;
2. It has to be easy usable for anyone;
sql dynamic
I want to create a dynamic query to list all unique users in a single table, from which I only need the username, from all different databases, which have the same database schema.
As the names of the tables differ on the different environments I would like to have something dynamic so it can be executed on different environments.
The basic query looks like this:
SELECT USERNAME FROM DATABASE.DBO.USERTABLE WHERE LOGIN = 'Y'
To execute this query on 3 databases, it would look like this:
SELECT *
FROM ( SELECT UT.USERNAME AS 'DB1'
FROM DB1.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) a
full outer join
( SELECT UT.USERNAME AS 'DB2'
FROM DOSSIER.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) b ON a.DB1 = b.DB2
full outer join
( SELECT UT.USERNAME AS 'DB3'
FROM DB3.DBO.USERTABLE UT
WHERE UT.LOGIN = 'Y'
) c ON a.DB1 = c.DB3
ORDER BY A.DB1
Is there an easy way to create a dynamic query for this, which can be applied on multiple databases?
I've already tried the code below, but I found it hard to get the JOIN ON working.
DECLARE @Sql NVARCHAR(MAX) = NULL;
SELECT @Sql = COALESCE(@Sql + ' AS '+ name + ' FULL OUTER JOIN ON' + CHAR(13) + CHAR(10), '' ) + 'SELECT USERNAME AS ' + name + ' FROM [' + name + '].[DBO].[USERTABLE] AS '+ name
FROM master.sys.databases
WHERE NOT [name] IN ( 'master', 'tempdb', 'model', 'msdb')
PRINT @Sql;
So what I would like to create is the thing below.
SELECT USERNAME
FROM [DATA].[DBO].[USERTABLE]
RESULTS
USER1
USER2
USER3
SELECT USERNAME
FROM [OTHER].[DBO].[USERTABLE]
RESULTS
USER3
USER4
USER6
SELECT USERNAME
FROM [NEW].[DBO].[USERTABLE]
RESULTS
USER1
USER6
USER7
SELECT USERNAME
FROM [SOME].[DBO].[USERTABLE]
RESULTS
USER2
USER5
USER7
So the resulting query should present the list below.
DATA____OTHER___NEW_____SOME____
USER1___________USER1___________
USER2___________________USER2___
USER3___USER3___________________
________USER4___________________
________________________USER5___
________USER6___USER6___________
________________USER7___USER7___
As mentioned, this has te be dynamic, as:
1. I don't know before how many databases are on an environment;
2. It has to be easy usable for anyone;
sql dynamic
sql dynamic
edited Dec 19 '18 at 11:22
Billy Who
asked Nov 28 '18 at 10:18
Billy WhoBilly Who
62
62
What about UNION ALL instead of outer join?
– jarlh
Nov 28 '18 at 10:20
Hi jarlh, thank you for your suggestion. This however isn't what I'm looking for. I need 1 column from each database, joined on another column. The union all puts the data from all the columns in 1 column.
– Billy Who
Dec 19 '18 at 11:08
add a comment |
What about UNION ALL instead of outer join?
– jarlh
Nov 28 '18 at 10:20
Hi jarlh, thank you for your suggestion. This however isn't what I'm looking for. I need 1 column from each database, joined on another column. The union all puts the data from all the columns in 1 column.
– Billy Who
Dec 19 '18 at 11:08
What about UNION ALL instead of outer join?
– jarlh
Nov 28 '18 at 10:20
What about UNION ALL instead of outer join?
– jarlh
Nov 28 '18 at 10:20
Hi jarlh, thank you for your suggestion. This however isn't what I'm looking for. I need 1 column from each database, joined on another column. The union all puts the data from all the columns in 1 column.
– Billy Who
Dec 19 '18 at 11:08
Hi jarlh, thank you for your suggestion. This however isn't what I'm looking for. I need 1 column from each database, joined on another column. The union all puts the data from all the columns in 1 column.
– Billy Who
Dec 19 '18 at 11:08
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53517088%2fcreate-dynamic-sql-query-to-list-all-users-from-over-all-databases%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53517088%2fcreate-dynamic-sql-query-to-list-all-users-from-over-all-databases%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
What about UNION ALL instead of outer join?
– jarlh
Nov 28 '18 at 10:20
Hi jarlh, thank you for your suggestion. This however isn't what I'm looking for. I need 1 column from each database, joined on another column. The union all puts the data from all the columns in 1 column.
– Billy Who
Dec 19 '18 at 11:08