• sp_MSforeachdb is convenient, but it has huge overhead and is probably overkill for what you need.

    You can use sp_executesql to return a value from dynamic SQL, albeit that it's not straightforward. Still, it's a good technique to know, so here it is:

    DECLARE @sql nvarchar(4000)

    DECLARE @version varchar(100) --or whatever

    SET @sql = N'SELECT TOP (1) @version = version_number ' +

    'FROM ['+ @DB_Name+ N'].dbo.schema_version '

    EXEC sp_executesql @sql, N'@version varchar(100) OUTPUT', @version OUTPUT

    IF @version LIKE ...

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.