Easy way to convert exec sp_executesql to a normal query?

后端 未结 10 1255
一整个雨季
一整个雨季 2021-01-31 09:37

When dealing with debugging queries using Profiler and SSMS, its pretty common for me to copy a query from Profiler and test them in SSMS. Because I use parameterized sql, my q

10条回答
  •  盖世英雄少女心
    2021-01-31 09:57

    Another solution which replaces the parameter values directly in the query (not exactly what you asked for but it might prove useful to others):

    https://code.msdn.microsoft.com/windowsdesktop/spExecuteSql-parser-1a9cd7bc

    I goes from:

    exec sp_executesql N'UPDATE Task SET Status = @p0, Updated = @p1 WHERE Id = @p2 AND Status = @p3 AND Updated = @p4',N'@p0 int,@p1 datetime,@p2 int,@p3 int,@p4 datetime',@p0=1,@p1='2015-02-07 21:36:30.313',@p2=173990,@p3=2,@p4='2015-02-07 21:35:32.830'
    

    to:

    UPDATE Task SET Status = 1, Updated = '2015-02-07 21:36:30.313' WHERE Id = 173990 AND Status = 2 AND Updated = '2015-02-07 21:35:32.830'
    

    which makes it easier to understand.

    The console application on that page can be used by passing a file parameter or copying the sp_executesql in the clipboard, running the app and then pasting the resulting SQL from the clipboard.

    Update:

    An SQL formatter can also be added to that solution for easier readability:

    http://www.nuget.org/packages/PoorMansTSQLFormatter/

    newSql = ConvertSql(Clipboard.GetText());
    var formattedSql = SqlFormattingManager.DefaultFormat(newSql);
    Clipboard.SetText(formattedSql);
    

提交回复
热议问题