How do I find where a query is used?

  • I have to add a column to a table. No big deal, right? Except I've got to find where there are production queries that might crash when this column is added. Queries such as

    Insert Into Some_Table

    Select * from Changing_Table

    What is the best way to find the source of a particular query? Would it be to search query plans using DMVs (I tried that but didn't find a DMV that gave me what I need)? Or would it be to set up a trace for, say, 24 hours looking at StatementCompleted? How could I find the queries I'm looking for in the trace results (I've been trying to look for a string in the textdata column )?

  • Before deciding on how to retrieve the queries, we would need to determine how queries are written against your database.

    Does your application build dynamic strings which are then executed as queries against your database (adhoc queries from the app)?

    Does your application strictly call stored procedures from the database?

    Or is there a mix of adhoc and procs?

    If the application is building dynamic adhoc queries, you need to go chat with your developers to find all of the potential places a query could hit your database and fail due to the new changes.

    In the case of stored procedures and triggers - you can query the definitions of those to find anything in the database that may need to be changed due to your pending change. There are several scripts on SSC for doing that type of search to make it easier.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • As a follow up to Jason's excellent advice, when you chat with those developers you need to slap them around if they are using queries like that in production. What you are experiencing is the reason that select * should not be allowed in production code (unless it is used as an exists or count).

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • You said it, Sean. I'm going to get out my whoopbutt stick when I find out who coded something like "Select * into..."

    Jason, it's a mix of adhoc and procs. I'm doing string searches on code libraries to find .Net programs that might use queries that cause problems. You're right about the sprocs. I used a SQL Search SSMS add-on to review those.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply