Hi Varun,
You are trying to connect to the non existing database that is why the error is thrown.
In @cmd variable you are trying to use non existing database.
You can recreate the error if you want by typing the below command in sql server query window
USE databasenotexist;
now you can see the error in your sql server studio
Try hard coding the database name in this line like this
SET @dbName = 'your database name here';
and check it.
Regards,
RK