July 24, 2013 at 9:34 am
I need to create 5 test databases by copying the live db's. There are many stored procedures in each db. Each sp is told which db to use, Use [NEHEN_prod]. I've been told that I have to go into each sp, about 50 per db, and change the NEHEN_prod to NEHEN_test. Is this necessary? If so, is there an easy way of doing it?
July 24, 2013 at 9:57 am
I'm pretty sure they are telling you wrong.. The USE statement is not permitted inside a stored procedure, when a stored procedure is created any use statements before it specify what database it is going into.
For example:
USE [yourdatabase]
GO
CREATE PROCEDURE dbo.SomeProc
AS
SELECT 1
GO
USE [yourdatabase]
GO
Tells it where to go to
CREATE PROCEDURE dbo.SomeProc
AS
SELECT 1
GO
Is the code that is compiled.
CEWII
July 24, 2013 at 10:11 am
Elliott Whitlow (7/24/2013)
I'm pretty sure they are telling you wrong.. The USE statement is not permitted inside a stored procedure...
Unless it's not in dynamic SQL, which is absolutely fine:
CREATE PROC p_TestUse
AS
BEGIN
DECLARE @sql NVARCHAR(4000)
SET @sql = 'USE DB1; SELECT DB_NAME();';
EXEC (@sql);
SET @sql = 'USE DB2; SELECT DB_NAME();';
EXEC (@sql);
END
July 24, 2013 at 10:16 am
Based on the original post this didn't look to be the case, but I agree in that case you might be right. I would say that if it was doing a USE to get into the current database that would be considered poor design.
CEWII
July 24, 2013 at 10:22 am
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?
July 24, 2013 at 10:28 am
Just to be clear................if the USE tells the procedure which db to go to AND I want the procedure to go to TEST instead of PROD, then I would need to change the USE statement. Correct?
July 24, 2013 at 10:30 am
NineIron (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!
July 24, 2013 at 10:35 am
Thanx. I don't think we're being served very well....................
July 24, 2013 at 10:41 am
NineIron (7/24/2013)
Thanx. I don't think we're being served very well....................
So, if you have no choice, but to rename database (as your client/employer requires you to do), you must go inside of every stored proc, function and view and replace reference to database name to the new one.
The above is quite clear reason why the practice of naming databases to differentiate environment is road to hell...
July 24, 2013 at 10:42 am
Eugene Elutin (7/24/2013)
NineIron (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.
CEWII
July 24, 2013 at 10:47 am
Eugene Elutin (7/24/2013)
NineIron (7/24/2013)
Thanx. I don't think we're being served very well....................So, if you have no choice, but to rename database (as your client/employer requires you to do), you must go inside of every stored proc, function and view and replace reference to database name to the new one.
The above is quite clear reason why the practice of naming databases to differentiate environment is road to hell...
It is your function to educate the client/employer as to WHY this methodology is bad, it is also important to recognize that in such configurations accidents are MORE likely to happen and they are going to have to live with that risk. They can set policy but that doesn't mean you can't challenge that policy or that the policy isn't wrong. Paranoia is a good trait for a DBA..
You likely need to look at the sprocs, functions, and views in ONE database to see if they use 3 part names when not needed or USE in dynamic code, if they don't then you don't need to modify anything. If you detect those then you only need to modify THAT object in all the copies. DO NOT continue the mistake of referencing the database when not needed.
Clear?
CEWII
July 24, 2013 at 10:50 am
...
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.
...
1. It's not a rant, but the expression of a strong feeling about the practive to name databases per enviornment.
2. I agree, there is no need in 3-part name when you want your object to always refer to "this" database. However, quite often, large/enterprise systems use multiple databases (first example which comes to mind would be Arhive/History db's). So, you very likely to have procedures and views which require cross-database access. That's where use of 3-part name justified.
July 24, 2013 at 10:56 am
Eugene Elutin (7/24/2013)
...
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.
...
1. It's not a rant, but the expression of a strong feeling about the practive to name databases per enviornment.
2. I agree, there is no need in 3-part name when you want your object to always refer to "this" database. However, quite often, large/enterprise systems use multiple databases (first example which comes to mind would be Arhive/History db's). So, you very likely to have procedures and views which require cross-database access. That's where use of 3-part name justified.
1. I did qualify with "Perhaps I read that wrong".. As DBAs our first duty is to data protection so I understand the direction you are going.
2. I have an article in the works that discusses NEVER using 3 part names in code and using synonyms to point to objects external to the local database. But I do see your point.
CEWII
July 24, 2013 at 11:22 am
So, in my case, I have no choice but to call the vendor a dope and move on. I think I'm going to call the vendor and get a quote to have him do the work.
July 24, 2013 at 11:29 am
NineIron (7/24/2013)
So, in my case, I have no choice but to call the vendor a dope and move on. I think I'm going to call the vendor and get a quote to have him do the work.
I don't see it that way, you need to do a little research to see how things are. As for changes you may have to engage the vendor. But as for having prod and non-prod on the same box I think you need to have a conversation with your management on the pitfalls of that idea.
CEWII
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply