Eugene Elutin (7/24/2013)
Thanx. So, if I were to copy a db and rename it from PROD to TEST, the sp's would run the same way on TEST without any code changes? Unless of course, the USE statement is within the procedure?
That is the worst possible practice to rename databases to differentiate environments in SQL Server. When I see that on client sites, I can straight away make a conclusion that they are served by not very experinced DBA's/Architects/Developers.
It creates so many unnecessary troubles!
And YES! It's guaranteed that you procs will run exactly the same way on TEST as they would in PROD wihtout any code changes.
Including the cases where objects referred using threee-part name which includes database name.
So, if your sp DELETES FROM PRODDB.dbo.Sometable, running it in TEST without change, will also attempt to delete FROM PRODDB.dbo.Sometable!
In your case USE is not something you should worry about...
If you want TEST environment, copy your PROD database onto another server or at least SQL instance and leave database name as it is!
While I generally agree lets not go off on a rant.. Perhaps I read that wrong, but we don't need to be testy.
As a rule you should never use a 3 part name when a 2 part name will do, in other words DELETE FROM SomeDB.dbo.SomeTable should not be used if the code being run is IN SomeDB, in that case DELETE FROM dbo.SomeTable is the right method.
I agree that having test/dev databases on the same server as prod is a monumentally bad idea, for exactly the reasons stated already. As a DBA your primary job, above ALL others is data safety and this configuration invites mistakes. You can have several development databases together, even a staging database, but there should be a distinct break between prod and non-prod.
Even if you only have one machine, a separate instance of SQL on that machine is still a better idea than running prod and non-prod together.
As for the original question the code should be fine provided they don't have USE statements in dynamic code or 3 part names when referring to the local database.