Hello rmiao,
Sorry for the late reply,

In my case the 2nd database is not always resided in the same server, some times the database resided in another server so i am using linked server.

I wrote the code accordingly. I am checking for second database, if it is available i am using linked server whether it is residing in the same server or not.

But now i am getting the following mentioned error.

Can you please tell/confirm whether linked server is not working if the second database is residing in the same server, so that i will change the logic in my code?

Thanks and Regards,
Hafeez.