Easy way to convert exec sp_executesql to a normal query?

后端 未结 10 1252
一整个雨季
一整个雨季 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:58

    I am not aware of an existing Add-In that can do this. But you could create one :)

    A few regular expressions and some string concatenation and after that sell it to Vinko and other souls looking for this functionality.

    If you're feeling like diving into this, here is some information on creating an SSMS addin: http://sqlblogcasts.com/blogs/jonsayce/archive/2008/01/15/building-a-sql-server-management-studio-addin.aspx

    0 讨论(0)
  • 2021-01-31 10:00

    You can use this Azur data studio extension. it based on @Matt Roberts repo. https://github.com/PejmanNik/sqlops-spexecutesql-to-sql/releases/tag/0.0.1

    0 讨论(0)
  • 2021-01-31 10:03

    I was looking for something similar so I use this in LinqPad, just copy sp_executesql statement to the clipboard and run the code in LinqPad. It outputs the SQL statement.

    void Main()
    {
        ConvertSql(System.Windows.Forms.Clipboard.GetText()).Dump();
    }
    
    private static string ConvertSql(string origSql)
    {
      string tmp = origSql.Replace("''", "~~");       
      string baseSql;
      string paramTypes;
      string paramData = "";
      int i0 = tmp.IndexOf("'") + 1;
      int i1 = tmp.IndexOf("'", i0);
      if (i1 > 0)
      {
          baseSql = tmp.Substring(i0, i1 - i0); 
          i0 = tmp.IndexOf("'", i1 + 1);
          i1 = tmp.IndexOf("'", i0 + 1);
          if (i0 > 0 && i1 > 0)
          {
              paramTypes = tmp.Substring(i0 + 1, i1 - i0 - 1);
              paramData = tmp.Substring(i1 + 1);
          }
      }
      else
      {
          throw new Exception("Cannot identify SQL statement in first parameter");
      }
    
      baseSql = baseSql.Replace("~~", "'");  
      if (!String.IsNullOrEmpty(paramData))  
      {
          string[] paramList = paramData.Split(",".ToCharArray());
          foreach (string paramValue in paramList)
          {
              int iEq = paramValue.IndexOf("=");
              if (iEq < 0)
                  continue;
              string pName = paramValue.Substring(0, iEq).Trim();
              string pVal = paramValue.Substring(iEq + 1).Trim();
              baseSql = baseSql.ReplaceWholeWord(pName, pVal);
          }
      }
    
      return baseSql;
    }
    
    public static class StringExtensionsMethods
    {
       /// <summary>
       /// Replaces the whole word.
       /// </summary>
       /// <param name="s">The s.</param>
       /// <param name="word">The word.</param>
       /// <param name="replacement">The replacement.</param>
       /// <returns>String.</returns>
       public static String ReplaceWholeWord(this String s, String word, String replacement)
       {
           var firstLetter = word[0];
           var sb = new StringBuilder();
           var previousWasLetterOrDigit = false;
           var i = 0;
           while (i < s.Length - word.Length + 1)
           {
               var wordFound = false;
               var c = s[i];
               if (c == firstLetter)
                   if (!previousWasLetterOrDigit)
                       if (s.Substring(i, word.Length).Equals(word))
                       {
                           wordFound = true;
                           var wholeWordFound = true;
                           if (s.Length > i + word.Length)
                           {
                               if (Char.IsLetterOrDigit(s[i + word.Length]))
                                   wholeWordFound = false;
                           }
    
                           sb.Append(wholeWordFound ? replacement : word);
    
                           i += word.Length;
                       }
    
               if (wordFound) continue;
    
               previousWasLetterOrDigit = Char.IsLetterOrDigit(c);
               sb.Append(c);
               i++;
           }
    
           if (s.Length - i > 0)
               sb.Append(s.Substring(i));
    
           return sb.ToString();
       }
    }
    
    0 讨论(0)
  • 2021-01-31 10:06

    I spent a little time making an simple script that did this for me. It's a WIP, but I stuck a (very ugly) webpage in front of it and it's now hosted here if you want to try it:

    http://execsqlformat.herokuapp.com/

    Sample input:

    exec sp_executesql 
              N'SELECT * FROM AdventureWorks.HumanResources.Employee 
              WHERE ManagerID = @level',
              N'@level tinyint',
              @level = 109;
    

    And the output:

    BEGIN
    DECLARE @level tinyint;
    
    SET @level = 109;
    
    SELECT * FROM AdventureWorks.HumanResources.Employee  
              WHERE ManagerID = @level
    END
    

    The formatting of the actual SQL statement once I've plucked it from the input is done using the API at http://sqlformat.appspot.com

    0 讨论(0)
提交回复
热议问题