Specifically, can I call a proc from the current database in a view. I already know about the openrowset hack, so this works, for example:
create view MyView as
select *
from openrowset (
'sqloledb',
'server=(local);trusted_connection=yes;',
'exec MyDatabase.dbo.MyStoredProcedure' -- Works fine
)
But I'd like to be able to call the proc from the current DB without hard-coding the name like so:
create view MyView as
select *
from openrowset (
'sqloledb',
'server=(local);trusted_connection=yes;',
'exec ' + db_name() + '.dbo.MyStoredProcedure' -- Don't want to hard-code DB name
)
This doesn't work, unfortunately, as openrowset expects literal strings, rather than variables of any sort.
Regardless of security and performance considerations, is there a workaround? It would make the maintenance of a legacy system much more bearable, as the proc that this view will call connects to a different database depending upon environment (dev, test, prod).