2

I'm trying to adapt an SQL query to check the value present in a certain field that is present in every database on my server.

There are 100 individual databases, and I would like to check a specific record of each one.

The answer is probably to use a command like the one below, but I'm having difficulty adapting it.

EXECUTE sp_MSForEachDB 
    'USE ?; SELECT DB_NAME()AS DBName, 
    COUNT(1)AS [Count] FROM CUSTOMERS'

I have had greater success with the link below;

https://stackoverflow.com/a/18462734/3461845

I need to be able to perform this query:

SELECT [SettingName],[SettingValue]  FROM [HostSettings] Where [SettingName] = 'SMTPServer'

And also pull back the name of the database for each row that is returned;

DBName | SettingName | SettingValue

Database1 | SMTPServer | smtp.gmail.com

Database2 | SMTPServer | smtp.gmail.com

Database3 | SMTPServer | smtp.yahoo.com

Any help is greatly appreciated.

Thanks!

4 Answers 4

2
DECLARE @T TABLE
([DbName] SYSNAME,
[SettingName] VARCHAR(255),
[SettingValue] VARCHAR(255));

INSERT INTO
@T
EXEC sp_MSForEachDB
'SELECT
    ''?'',
    [SettingName],
    [SettingValue]
FROM
    [?]..[HostSettings]
WHERE
    [SettingName] = ''SMTPServer''';

SELECT * FROM @T;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for replying, I'm afraid I get the following error Incorrect syntax near '-'. I've had a google, and it might be to do with whitespace, but I've tried stripping out what I can and it's still not working.
I think this is because my table names have hyphens in them... Just trying to work this out now.
0

You can use Registered Servers in SSMS. To run the same query on all databases.

  1. In SSMS, go to View

  2. Then Registered Servers

  3. Right Click on Local Server Group then New Server Group

  4. Type any name for the Group

  5. Then the the Group name you created, right click and New Server Registration

  6. And Server name Localhost (for example)

  7. Then Right Click on Localhost and New Query. That will run all the same query on all databases

1 Comment

I have tried this, and it doesn't work for my scenario.
0

Ignore below. Try this.

DECLARE @sql VARCHAR(8000)
DECLARE @string VARCHAR(100)= 'SMTPServer'
SET @sql = 'EXECUTE sp_MSForEachDB 
    ''USE ?; SELECT DB_NAME()AS DBName, 
        SettingName,
        SettingValue
    FROM HostSettings
    WHERE SettingName =''' + '''' + @string +'''''' + ''''

EXEC (@sql)

Comments

0
declare @userdb_list table (name varchar(4000) not null);
-- fill the db list with custom subset
insert into @userdb_list
select name from sys.databases --can add where condition to filter db names

declare
@curr_userdb varchar(300),
@db_placeholder varchar(300),
@final_db_exec_query varchar(max),
@query varchar(max);
set @query = '' --  <add ur query here> 

set @db_placeholder = 'use {db}'; 
set @curr_userdb = (select min(name) from @userdb_list);

while @curr_userdb is not null
begin
set @final_db_exec_query = replace(@db_placeholder, '{db}', @curr_userdb +  '  ' + @query);
exec (@final_db_exec_query);
--print @final_db_exec_query
set @curr_userdb = (select min(name) from @userdb_list where name > @curr_userdb);
end
GO

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.