one of the core caveats of the development life cycle is that the code is tested in dev, and is promoted UNCHANGED to the next layer for acceptance and testing.
if you have to modify procedures based on where they get deployed, your system is not following the rules.
the procedure should not care about database names. if it is making a cross database query, it should be from database [Phm].[dbo].[TargetTable] to [Med].dbo.[TargetTable]
So f you should have Three SQL instances, for DEV/QA and Prod., i would expect two identically named databases on
of you have ONE SQL isntance, with six databases, you will need to create synonyms for the objects instead.
CREATE SYNONYM dbo.synTargetTable FOR [Phm].[dbo].TargetTable
then the procedure should target the synonym [synTargetTable] so the code never changes, but the underlying pointer/Synonym can be changes per environment.
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!