SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Edit multiple stored procedures


Edit multiple stored procedures

Author
Message
NineIron
NineIron
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2290 Visits: 707
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?
Elliott Whitlow
Elliott Whitlow
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23454 Visits: 5314
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
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12126 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Elliott Whitlow
Elliott Whitlow
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23454 Visits: 5314
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
NineIron
NineIron
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2290 Visits: 707
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?
NineIron
NineIron
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2290 Visits: 707
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?
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12126 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
NineIron
NineIron
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2290 Visits: 707
Thanx. I don't think we're being served very well....................
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12126 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Elliott Whitlow
Elliott Whitlow
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23454 Visits: 5314
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search