• ''?'' can be used in the SELECT statement to get names of databases.

    exec sp_MSForEachDB

    '

    USE ?;

    if exists

    (select table_name

    from information_schema.tables

    where table_name = ''RS_Patient_6'')

    begin;

    select ''?'' as dbname, * from RS_Patient_6

    where PatientID_6_660 = ''<patient id>''

    end;

    ';