-- Switch between database names depending on server IF db_id('database2_dev') is not null BEGIN INSERT INTO database2_dev.dbo.tablename (cols) SELECT cols FROM database1_dev.dbo.tablename END ELSE BEGIN INSERT INTO database2.dbo.tablename (cols) SELECT cols FROM database1.dbo.tablename END