开发者

Replacing a string across procedures in SQL Server

开发者 https://www.devze.com 2023-03-28 02:24 出处:网络
Is there a way to make a procedure to s开发者_如何转开发earch and replace a specific string in all procedures?Maybe it would be easier to fix the application. Why does the software care about comments

Is there a way to make a procedure to s开发者_如何转开发earch and replace a specific string in all procedures?


Maybe it would be easier to fix the application. Why does the software care about comments? Does it also barf on extended properties?

Assuming the comment is always the same, run this in Management Studio with results to text:

SET NOCOUNT ON;

SELECT 'GO
    ' + REPLACE([definition], '/* offending comment */', '')
    FROM sys.sql_modules
    WHERE [definition] LIKE '%/* offending comment */%';

This will yield a series of CREATE PROCEDURE commands separated by GO. You can't necessarily do a search/create for CREATE PROCEDURE and change it to ALTER in case you have actual CREATE PROCEDURE strings within the stored procedure body. So instead you can pull these results into your favorite text editor and do a search and replace for:

GO
CREATE PROCEDURE

Replacing it with:

GO
ALTER PROCEDURE

(Though this assumes that you don't have comments before the CREATE PROCEDURE line. If you do, you'll need to get more creative with your search and replace.)

If you don't need to worry about procedures that contain a valid string CREATE PROCEDURE - and assuming that your syntax is actually CREATE PROCEDURE and not CREATE PROC or random spacing between CREATE and PROC and that your system isn't case sensitive and you used create proc or Create Proc...

SET NOCOUNT ON;

SELECT 'GO
    ' + REPLACE(REPLACE([definition], '/* offending comment */', ''),
    'CREATE PROCEDURE', 'ALTER PROCEDURE')
    FROM sys.sql_modules
    WHERE [definition] LIKE '%/* offending comment */%';

If you don't care about the existing tangible things associated with your stored procedures, e.g. permissions, you could also do it this way:

SELECT 'DROP PROCEDURE ' 
    + QUOTENAME(OBJECT_SCHEMA_NAME([object_id])) 
    + '.' + QUOTENAME(OBJECT_NAME([object_id])) + ';
    ' + 'GO
    ' + REPLACE([definition], '/* offending comment */', '')
FROM sys.sql_modules
WHERE [definition] LIKE '%/* offending comment */%';

This will generate a script that is much closer to ready to go, since it just drops the procedure and re-creates it (so doesn't need to change CREATE to ALTER), but it is not very common that you can just drop and re-create objects because of permissions and/or dependencies.

CREATE OR REPLACE or something similar in DDL would make this much easier to script out. If you feel this would be a valuable addition to SQL Server, please vote for the following suggestion (and add your use case, or why you think this would be valuable, to the comments):

http://connect.microsoft.com/SQLServer/feedback/details/127219/create-or-replace


How about Generating the Scripts of ALL those stored procedure as a .sql file and then replacing the comment in that generated file with the help of CTRL + H. Isn't that the easier option?

0

精彩评论

暂无评论...
验证码 换一张
取 消