Link to home
Start Free TrialLog in
Avatar of UKIT
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!
ASKER CERTIFIED SOLUTION
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UKIT
UKIT

ASKER

Sure,

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].[table]
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
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.
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(proc_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
Ukit, do you still need help with this question?
Avatar of UKIT

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