See
How to return a value from dynamicaly executed T-SQL code FAQ #8130
>
>I have a SP in one DB that needs to call a SP in another database that returns a value. The problem is that I would like for the DB name to be user-configured in the first DB.
>
>Below is a sample of how it works if the DB is hard-coded:
>
>declare @x varchar(20)
>set @x = 'LookUp_Value'
>exec [Database2].[dbo].[my_sp] @x output
>print (@x)
>
>This works fine. However, as I stated earlier, I need to make the DB name user-configurable.
>
>Here's is what I am attempting (which does not work) ...
>
>declare @db varchar(20)
>declare @x varchar(20)
>declare @exec varchar(100)
>set @db = 'Database2'
>set @x = 'LookUp_Value'
>set @exec = '[' + @db + '].[dbo].[my_sp] @x output'
>exec (@exec)
>print (@x)
>
>... any ideas?
>
>Thanks
>Doug
--sb--