UKIT
asked on
Script changes to stored procedures
Hi,
I have around 40 stored procedures all of them need to be amended, I will need to amend the select statement , my question is how to script the changes I.e. how to automate the changes as the first amendment will be to point to test data then all of them need to be amended again to point to live data.
appreciate your help in advance!
I have around 40 stored procedures all of them need to be amended, I will need to amend the select statement , my question is how to script the changes I.e. how to automate the changes as the first amendment will be to point to test data then all of them need to be amended again to point to live data.
appreciate your help in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The change I' seeing highlighted is to add a field to the projection of the SELECT and a new inner join.
If you want to automate that, I suggest dynamic SQL statements. In that way, you change these 40 procedures once and will never worry about.
If you want to automate that, I suggest dynamic SQL statements. In that way, you change these 40 procedures once and will never worry about.
I would suggest creating a view that points to the table on the foreign server. Modify all of your stored procedures to use this new view. Then when you are ready to switch to the production table, just modify the view to point to the correct table.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think this code should be able to generate the new code for you. Then copy it from the output window to a query window to run. Or, if you need it to run automatically without checking it, let me know and we can add code for that.
--press Ctrl-T (text output) before running; press Ctrl-D to go back to grid output after running.
SET NOCOUNT ON
SELECT REPLACE(REPLACE(REPLACE(pr oc_code,
'CREATE PROCEDURE', 'ALTER PROCEDURE'),
', BB.Mf as OMf', ', BB.Mf as OMf, NMF as ANMF'),
'from CM', 'from CM INNER JOIN ' +
'[server].[dbname].[dbo].[ table] ' +
'on cm.[MRf]=[omc] collate database_default ') + '
GO' + '
'
FROM sys.objects o
CROSS APPLY (
SELECT OBJECT_DEFINITION(o.object _id) AS proc_code
) AS assign_alias_names
WHERE
o.type = 'P' AND
(proc_code LIKE '%, BB.Mf as OMf%' OR
proc_code LIKE '%from CM[^#0-9a-z]%')
SET NOCOUNT OFF
--press Ctrl-T (text output) before running; press Ctrl-D to go back to grid output after running.
SET NOCOUNT ON
SELECT REPLACE(REPLACE(REPLACE(pr
'CREATE PROCEDURE', 'ALTER PROCEDURE'),
', BB.Mf as OMf', ', BB.Mf as OMf, NMF as ANMF'),
'from CM', 'from CM INNER JOIN ' +
'[server].[dbname].[dbo].[
'on cm.[MRf]=[omc] collate database_default ') + '
GO' + '
'
FROM sys.objects o
CROSS APPLY (
SELECT OBJECT_DEFINITION(o.object
) AS assign_alias_names
WHERE
o.type = 'P' AND
(proc_code LIKE '%, BB.Mf as OMf%' OR
proc_code LIKE '%from CM[^#0-9a-z]%')
SET NOCOUNT OFF
Ukit, do you still need help with this question?
ASKER
This solution was the best for my case, thank you so much for the swift help and sorry for the delay response, much appreciated guys
ASKER
this is before
ALTER PROCEDURE [dbo].[XXXXX]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
BB.MID
, BB.CD
, BB.Mf as OMf
from CM
inner join dbo.ST
on cm.STID = BB.STID
left join dbo.TT
on cm.STTID = tt.TTID
AFTER
ALTER PROCEDURE [dbo].[XXXXX]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
BB.MID
, BB.CD
, BB.Mf as OMf
======xxxx=====
,NMF as ANMF
=======xxxx=====
from CM
=================This is the change that has to be scripted ==Here=
INNER JOIN
[server].[dbname].[dbo].[t
on cm.[MRf]=[omc] collate database_default
==========================
inner join dbo.ST
on cm.STID = BB.STID
left join dbo.TT
on cm.STTID = tt.TTID
the changes are in between the ====== signs