SQL 2012 - replace (nolock) with with (nolock)

3

I need help solving an incompatibility problem. The company I'm working on is migrating from a SQL Server 2008 to 2012 Bank, but we've identified some issues.

The main one is the declaration of (NOLOCK) without WITH in VIEWs, FUNCs and PROCs, when using linked server, it presents error and we must include WITH .

Created objects will often have the SELECT statement where an "alias" is mentioned for the table in question. Ex: Select * from customer as c or Select * from customer c

In these cases to make matters worse, (NOLOCK) was included between the table and the alias. ex:

Select * form customer '(NOLOCK)' as c

In a mass change, such as REPLACE (NOLOCK) for WITH (NOLOCK), it will look like this:

Select * from customer 'WITH(NOLOCK)' as c

Presenting error in running / compiling.

But how to do this in a "fast" way when we have more than 900 objects between those mentioned above?

    
asked by anonymous 13.08.2015 / 14:18

1 answer

3

Attention: Make a backup before your database . Ideally, you should run this on a test basis.

By the SQL Management Studio graphical interface:

  • Right-click on the database you want;
  • Select the Generate Scripts option.
  • Next and select for which object types you want to generate the script.
  • On the screen where you want to export, click the Advanced button and check the DROP and CREATE Script option for the same name option.
  • Exporting;

  • Once you have done this, open the generated file and with an editor like Notepad ++ replace (NOLOCK) with WITH (NOLOCK)

  • Load this file into SQL Management Studio and run it.

As I said, run tests before running into production. If you can not do it on a test basis, then export only two stored procedures and see if it works.

Update

The DROP Script and CREATE option guarantees that a DROP will be generated for each object, just by giving DROP and creating again you will be able to perform this operation. You could use ALTER, but there is no such option in the script generator (at least in my version).

Another thing you need to check is whether the script generator will write to the file respecting the dependencies between the objects. This is important because you can not drop a function that is being used in a Stored Procedure. So, you need to drop SP first, then drop Function.

Update 2 (According to question update):

Regular expression in Notepad ++ version 6.4.5

In Notepad ++ there is the option of finding and replacing a text with regular expressions. To do this, press CTRL-F and use the Regular Expression option:

Step 1

Look for this:

\(NOLOCK\)

Replace with this:

WITH \(NOLOCK\)

2nd Step

Look for this:

(WITH\s+\(NOLOCK\))\s+(AS\s+[A-Z]+)

Replace with this:

 

Test :

In the 1st step, this:

Select * from QUOTE (NOLOCK) AS TB

It's transformed into this:

Select * from QUOTE WITH (NOLOCK) AS TB

That in step 2 is transformed into this:

Select * from QUOTE AS TB WITH (NOLOCK)

Explanation about Regular Expression :

(WITH\s+\(NOLOCK\))\s+(AS\s+[A-Z]+)

First let's break it into three pieces:

(WITH\s+\(NOLOCK\))
\s+
(AS\s+[A-Z]+)

Note that the 1st and 3rd pieces are delimited by paranteses. The 1st piece is called \ 1 and the 3rd piece is called \ 2. Record this.

1st piece:

WITH
\s+
\(NOLOCK\)

This piece is looking for the WITH string anywhere on the line. After that, all whitespace spaces (\ s +) are consumed until you find the string (NOLOCK). The backslashes are necessary to escape the character paranteses, since it is a reserved character.

2nd piece:

The second piece (\ s +) is just a way to traverse all spaces between (NOLOCK) and the AS word.

3rd piece:

AS
\s+
[A-Z]+

The 3rd piece finds the word AS that must necessarily come after 1 or several blanks (since it comes after the 2nd piece).

Once this is done, one or more white spaces are traversed until a word that has one or more characters (from A-Z) is found. This is the table name that comes after AS.

Replacement

As explained above, the regular expression is divided into two groups (the 1st and 3rd pieces). So the substitution is simply to rewrite the 3rd piece + a blank + 1st piece, like this:

 
    
13.08.2015 / 14:35