-12

I have large table data and each table need to be end with the statement (nolock) at the end , please help me to find in all stored procedure.

Example:

if a store-procedure used two tables a and b and one table b doesn't end with (NOLOCK) then i need to return following details.

SP_Name,Table_name

10
  • 7
    Are you sure this is a good idea? Bad habits : Putting NOLOCK everywhere Jun 10, 2015 at 13:10
  • @MikaelEriksson: understand but i am in a situation to find it now
    – Rahul Hera
    Jun 10, 2015 at 13:16
  • 3
    @RahulHera Why? If you had read that page, you would have seen the alternatives suggested. Jun 10, 2015 at 13:23
  • 1
    What are you really going to do if you find all the procedures that miss a NOLOCK? Write a dynamic SQL script to update them adding NOLOCK? This is incredibly difficult as it requires parsing the SQL code. And what if the procedure uses dynamic SQL itself? Good luck with that. Jun 10, 2015 at 13:26
  • 3
    There is not built-in possibility for that and I don't think there are third-party tools that can do that. Even if you manage to automatically find all the places in your stored procedures where tables are used without WITH (NOLOCK), you will probably still have too add the hint manually. But look, the whole idea is really, really yucky. The article linked above is teaching you to try and avoid using NOLOCK at all, and here you are asking to add it everywhere. You should see why people are reluctant to help you.
    – Andriy M
    Jun 10, 2015 at 13:35

1 Answer 1

2

If I understand you correctly, you are looking for all the store procedure names that have the nolock keyword:

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%nolock%' 
    AND ROUTINE_TYPE='PROCEDURE'
9
  • i want to find the tables whose select statement need to be end with 'nolock'.
    – Rahul Hera
    Jun 10, 2015 at 13:17
  • 4
    Sorry I never work with 'NoLock'
    – Nazmul
    Jun 10, 2015 at 13:23
  • 1
    I think if you change like to not like you will have gave him the solution he was looking for......maybe Jun 10, 2015 at 13:33
  • 2
    @ChristianBarron: The "problem" is, even a single query can use many tables, not to mention the fact that a stored procedure can have many statements – thus, many queries. If at least one table across the entire procedure uses the hint and the others don't, then ROUTINE_DEFINITION NOT LIKE '%nolock%' will omit that procedure.
    – Andriy M
    Jun 10, 2015 at 13:47
  • 9
    @RahulHera: I may have understood your requirements well, but that doesn't mean I agree to help you on this problem, sorry. I don't know if you have come up with this yourself or you are doing what your boss has told you to do or you are helping a friend – in any event, adding NOLOCK everywhere is a bad idea to begin with, and being able to do that automatically would be even worse.
    – Andriy M
    Jun 10, 2015 at 14:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.