how to override the database selected in the available database dropdown

Lindbom, Peter 0 Reputation points
2026-08-04T13:37:55.46+00:00

I need to union results from two databases using fully qualified objects/views but the query after the union returns zero rows.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 136.2K Reputation points MVP Volunteer Moderator
    2026-08-04T21:18:20.71+00:00

    I guess then that your tables are empty, or that your WHERE conditions filter out all rows.

    Since I don't possess a crystal ball, I can't say more exactly why you don't get any data back, but if you as a start share the query, we might be able to say something more useful.

    In the meanwhile, here is an example of a UNION query that does return rows:

    CREATE DATABASE DB111
    CREATE DATABASE DB222
    go
    USE DB111
    go
    CREATE TABLE pannkaka (a int NOT NULL, 
                           b varchar(123) NOT NULL,
                           CONSTRAINT pk_pannkaka PRIMARY KEY (a)
    )
    INSERT pannkaka(a, b)
       SELECT object_id, name 
       FROM   sys.objects
    go
    USE DB222
    go
    CREATE TABLE korvomos (a int NOT NULL, 
                           b varchar(123) NOT NULL,
                           CONSTRAINT pk_korvomos PRIMARY KEY (a)
    )
    INSERT korvomos(a, b)
       SELECT object_id, name 
       FROM   sys.objects
    go
    USE tempdb
    go
    SELECT a, b 
    FROM   DB111.dbo.pannkaka
    WHERE  b LIKE '%obj%'
    UNION ALL
    SELECT a, b 
    FROM   DB222.dbo.korvomos
    WHERE  b LIKE '%hob%'
    go
    DROP DATABASE DB111, DB222
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.