码迷,mamicode.com
首页 > 数据库 > 详细

VBA POST 调用网页API 格式化SQL语句

时间:2017-12-26 14:38:41      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:start   inter   sql语句   encode   parse   format   xmlhttp   enc   style   

工作中遇到一个场景,需要用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 = 200 Then

    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语句

标签:start   inter   sql语句   encode   parse   format   xmlhttp   enc   style   

原文地址:https://www.cnblogs.com/dl-ekong/p/8117068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!