Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Edit multiple stored procedures Expand / Collapse
Author
Message
Posted Wednesday, July 24, 2013 9:34 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 7:59 AM
Points: 119, Visits: 184
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?
Post #1477134
Posted Wednesday, July 24, 2013 9:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
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
Post #1477145
Posted Wednesday, July 24, 2013 10:11 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:36 AM
Points: 2,836, Visits: 5,066
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



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1477150
Posted Wednesday, July 24, 2013 10:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
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
Post #1477152
Posted Wednesday, July 24, 2013 10:22 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 7:59 AM
Points: 119, Visits: 184
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?
Post #1477153
Posted Wednesday, July 24, 2013 10:28 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 7:59 AM
Points: 119, Visits: 184
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?
Post #1477155
Posted Wednesday, July 24, 2013 10:30 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:36 AM
Points: 2,836, Visits: 5,066
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!







_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1477157
Posted Wednesday, July 24, 2013 10:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 7:59 AM
Points: 119, Visits: 184
Thanx. I don't think we're being served very well....................
Post #1477158
Posted Wednesday, July 24, 2013 10:41 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:36 AM
Points: 2,836, Visits: 5,066
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...


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1477160
Posted Wednesday, July 24, 2013 10:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
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
Post #1477161
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse