0

BACKGROUND:

I know how to run query against all databases on an instance, but I find that it returns correct results for some of the databases and incorrect ones for the other databases. All my databases have identical schema so there is no reason for the query to work agains some databases correctly and produce fake results for other databases. Basicaly query should show if record XYZ exists in Users table. If record exists query should return relevant record. Otherwise it should print NULL.

This is my query:

CREATE TABLE ##tempUsers(DB_Name VARCHAR(MAX), userID VARCHAR(MAX), role VARCHAR(MAX));
GO
DECLARE @command varchar(1000)
SELECT @command =  'Use ? INSERT INTO ##tempUsers select ''?'', userID, role from users where userID = ''XYZ'';'
EXEC sp_MSforeachdb @command
GO
SELECT Name, UserID, Role
FROM ##tempUsers i RIGHT JOIN sys.databases o  
ON o.name = i.db_name
GO
DROP TABLE ##tempUsers;

PROBLEM:

Query correctly identifies some of the databases containing given record in given table. There are also some databases that query shows as NULL even though they have the record that is needed. For such databases NULL is returned even though they contain record in question. Schema is identical across all databases and the databases are not case sensitive.

Through elimination I was able to find that the following part of the query is only inserting results from some databases to the temporary table.

SELECT @command =  'Use ? INSERT INTO ##tempUsers select ''?'', userID, role from users where userID like ''%tempUsers%'';'
EXEC sp_MSforeachdb @command

WHAT I TRIED

1) Running simple SELECT column1, Column2 against each of the databases that was mistakingly identified as not containing record produces correct results.

2) I tried to replace = '' with LIKE %%, but it made no difference.

QUESTION:

How can I alter my query so that it returns corrent results for all databases and not just for some of them?

QWE
  • 287
  • 2
  • 15
  • 1
    Possible duplicate of [How to run the same query on all the databases on an instance?](http://stackoverflow.com/questions/18462410/how-to-run-the-same-query-on-all-the-databases-on-an-instance) – StackUseR Nov 30 '16 at 11:28
  • I may be wrong, but I think what I am asking is different from the question you quoted. I alredy have a way to query all databases on an instance. I however find that the query produces fake results for some of them. – QWE Nov 30 '16 at 11:31
  • May be... Anyway see [How to execute sql against all dbs on a server](http://dba.stackexchange.com/questions/907/how-to-execute-sql-against-all-dbs-on-a-server). – StackUseR Nov 30 '16 at 11:42
  • Thank you Ashish. The question you quoted now suggests exactly the same thing that I am using right now and that I am not finding reliable. I guess what I need is a fix to the current method or a completelly different solution that will provide same results (just without falses) – QWE Nov 30 '16 at 11:50
  • Identify the database with unexpected results, extract the SQL string and run it. You might find that for example there are invisible characters and spaces, so the query, in fact, does not match and does not return records – Nick.Mc Nov 30 '16 at 12:50
  • Thank you Nick. I tried that and it appears that the SQL string works perfectly on it's own against each of the databases. – QWE Nov 30 '16 at 12:52
  • I also tried changing ='' to LIKE %% but it didn't make any difference. – QWE Nov 30 '16 at 12:53
  • check permissions on the DBs that have records but return NULLs... just guessing... – user2065377 Nov 30 '16 at 15:48
  • I'm running the query as SA so this shouldn't be an issue, but thank you for the suggestion. It was a reasonable guess. – QWE Nov 30 '16 at 16:12
  • Next suggestion: Change your sql to just insert the database name, just to confirm that it's really running in each database. – Nick.Mc Dec 02 '16 at 10:41
  • I just tried that and I can confirm that the query is indeed running in each database. – QWE Dec 02 '16 at 16:02
  • I could go on making troubleshooting suggestion but this statement makes me curious: _query shows as NULL_. If the record isn't inserted, nothing is going to show as NULL, the record just won't exist. For example of you run `SELECT * FROM ##tempUsers`, you won't see a NULL, you just won't see a record. Where are you seeing NULL? OK.. just one more suggestion: make it a permanent table instead of a temp table – Nick.Mc Dec 03 '16 at 07:38

0 Answers0