VBA POST 調用網頁API 格式化SQL語句
阿新 • • 發佈:2017-12-26
att func get string call http fun mic class
工作中遇到一個場景,需要用VBA把SQL語句重新美觀格式化一下,本來想直接調用本地的SQLWORKBENCH工具來實現這一功能,無奈找不到command參數,不能被VBA直接調用,作罷.
網上找到可以直接調用網站的API用VBA 調用http post可以來實現.
網頁API說明:
https://github.com/sqlparser/sql-pretty-printer/wiki/SQL-FaaS-API-manual
裏面必選的參數 rqst_input_sql,傳入帶轉化的SQL語句.
Response裏面關註的參數,rspn_output_fmt 為轉化完之後的SQL語句.
VBA函數代碼如下:
Function CallSQLFormat(ByVal SQL As String) Dim http Set http = CreateObject("Microsoft.XMLHTTP") http.Open "POST", "http://www.gudusoft.com/format.php", False http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" http.send "rqst_input_sql=" & SQL If http.Status = 200Then Resp_text = http.responseText istart = InStr(Resp_text, "rspn_formatted_sql") iend = InStr(Resp_text, "}") CallSQLFormat = Replace(Mid(Resp_text, istart + 18 + 2 + 1, iend - istart - 22), "\n", vbLf) Else CallSQLFormat = SQL End If End Function
VBA POST 調用網頁API 格式化SQL語句