Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:24:13+00:00 2026-05-30T21:24:13+00:00

We have some views and stored procedures which have some data hard-coded in their

  • 0

We have some views and stored procedures which have some data hard-coded in their definitions.

CASE WHEN sprLatestSPR2.StatusID IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20)

I create a table that holds all global variables. For example, I define STATUS_SET1 to 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20 in the table.

Now I need to write a script to replace all hard-coded data in the definitions of views & store procedures with queries. For example I need replace above clause with

CASE WHEN sprLatestSPR2.StatusID IN (SELECT STATUS_SET1 FROM myGlobalVariables)

How do I write a script to search the definitions of the views & stored procedures and do the replacement?

Update:

I have the query to find out the first part of my question, search the definition of view/store procedure for a specific sentence.

SELECT Name 
FROM sys.procedures 
WHERE OBJECT_DEFINITION(object_id) LIKE '%StatusID<21%'

Now my question is how to update view/store procedure to replace all the occurrences of StatusID<21 with some global variable?

New Update:

I run following script in the SSMS,

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) LIKE ‘%V.EmailStatus=”GP”%’

SELECT STUFF(REPLACE(definition, ‘V.EmailStatus=”GP”’, ‘V.EmailStatus=”RL”’), 1, 6, ‘ALTER’)
+ CHAR(13) + CHAR(10) + ‘GO’
FROM sys.sql_modules WHERE definition LIKE ‘%V.EmailStatus=”GP”%’

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) LIKE ‘%V.EmailStatus=”GP”%’

I got the same result from the third SELECT as the first SELECT, which is should be nothing, since I replaced GP with RL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:24:14+00:00Added an answer on May 30, 2026 at 9:24 pm

    Assuming the procedure starts with CREATE exactly, you can say:

    SELECT STUFF(REPLACE(definition, '1, 2, ...', 'SELECT ...'), 1, 6, 'ALTER') 
        + CHAR(13) + CHAR(10) + 'GO'
    FROM sys.sql_modules WHERE definition LIKE '%(1, 2, ...')%';
    

    If it can have leading spaces, comments before CREATE, etc. then it can get a bit more complex.

    You can also just return the scripts themselves, or script them out to files, then do your own search and replace within SSMS or your favorite text editor.

    EDIT note that this will just produce the script to alter your objects. You’ll want to verify them, backup your database first, then copy the script and run it.

    EDIT 2 explaining code based on question

    sys.sql_modules.definition contains a CREATE script that represents your procedure/function body. It does not include the GO required between altering modules, nor is there a way to have it output an ALTER command instead (I’ve asked for it in the past but I’ll have to agree with them I’d rather see CREATE OR REPLACE syntax).

    STUFF in this case takes the output (which will be CREATE PROCEDURE ... after the 1, 2, ... has been replaced) and, starting with the first character, replaces the first 6 characters (CREATE) with ALTER – since the assumption is that you will want to run these as alters as opposed to drop / create (so that you don’t lose permissions / dependencies).

    The GO adds a batch separator between each body, since ALTER PROCEDURE must be in its own batch. So instead of…

    CREATE PROCEDURE dbo.foo
    AS
    BEGIN
        ...
    END
    
    CREATE PROCEDURE dbo.bar
    AS
    BEGIN
        ...
    END
    

    …which will generate errors, you’ll have…

    CREATE PROCEDURE dbo.foo
    AS
    BEGIN
        ...
    END
    GO
    CREATE PROCEDURE dbo.bar
    AS
    BEGIN
        ...
    END
    GO
    

    …which will ensure that each stored procedure is handled in its own batch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a set of tables and views that merely store some config data
I have a series of NSFetchedResultsControllers powering some table views, and their performance on
I have 5 views in my ASP.NET MVC application. Some data were captured from
I have a GridView which retrieves data from a database. Users have roles, some
I'm looking for some Best Practices for automating the deployment of Stored Procedures/Views/Functions/Table changes
I have some stored procedures that have a WHERE clause like this: WHERE dbo.Users.DatePaidUpTo
I have a table that contains some statistic data which is collected per hour.
I have a SQL Stored Procedure which is never ending because of some values
I am doing some T-SQL programming and I have some Views defines on my
I have to do some different views containing 72 LED lights. I built an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.