RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 925752
Accepted
nick_n_a
nick_n_a
Asked:2020-12-26 20:31:02 +0000 UTC2020-12-26 20:31:02 +0000 UTC 2020-12-26 20:31:02 +0000 UTC

MSSQL Rest API 查询

  • 772

是否可以通过MSSQL对给出表格的url进行请求,以表格的形式显示出来?

declare @xmlhttp int
declare @hr int
declare @text char(8000)
exec sp_OACreate 'Msxml2.XMLHTTP', @xmlhttp out
exec @hr=sp_OAMethod @xmlhttp, 'open', NULL, 'GET', 
      'http://10.0.0.0:8080/API/flat?token=c57aa1', false
if @hr=0  exec @hr = sp_OAMethod @xmlhttp,
  'setRequestHeader', null, 'Content-Type', 'text/html'
if @hr=0 exec @hr = sp_OAMethod @xmlhttp, 'send', NULL, null
-- тут hr равен нолю - без ошибок
if @hr=0 exec @hr = sp_OAGetProperty @xmlhttp, 
'responseText',  @text out;
--- тут возникают ошибки "конвертации"

当responseText超过 8000 个字符时出现问题 - 然后无法读取它(MSSQL 对字符串大小的限制)。如果其他方式从另一台服务器获取 json 表并在 MSSQL 中处理?

UPD:而且......就像......不可能将文本转换为表格(之前我通过xml编写了一个变态,它通过xml工作)。写作...

SELECT 
cast((select value from OPENJSON(value) 
  where [key]='id') as int) id,
cast((select value from OPENJSON(value) 
  where [key]='name') as varchar(32)) name
FROM OPENJSON( (select x from @tab)  )

看起来没问题,我把它放在存储中并执行一个 exec 然后......

消息 208,级别 16,状态 1,过程 update_rest2,第 32 行 [批处理开始第 2 行] 对象名称“OPENJSON”无效。

json视图[{"id":18120,"name":"Smart"},{"id":18124,"name":"Smart4"}]

有没有人遇到过这样的笑话?不能在存储中工作,但在没有存储的情况下工作(服务器 2016)

sql-server
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    nick_n_a
    2020-12-27T16:44:57Z2020-12-27T16:44:57Z

    最后变成了这样

    declare @xmlhttp int
    declare @hr int
    exec sp_OACreate 'WinHttp.WinHttpRequest.5.1', @xmlhttp out
    EXEC @hr=sp_OAMethod @xmlhttp, 'open', NULL, 'GET', 'http://API/flat', false
    if @hr=0 exec @hr = sp_OAMethod @xmlhttp, 'send', NULL, null
    declare @tab table (x varchar(max))
    if @hr=0 insert into @tab exec @hr = sp_OAGetProperty @xmlhttp, 'responseText';
    exec  @hr = sp_OADestroy @xmlhttp;
    

    [{"id":18120,"name":"Smart"},{"id":18124,"name":"Smart4"}]并像这样解析查询

    select * FROM OPENJSON ((select x from @tab))
    with  ( id int  '$.id', name varchar(32) '$.name' )  
    

    OPENJSON从2016服务器开始有效,早期版本可以通过xml解析,但是如果OPENJSON在2016+版本不支持,需要ALTER DATABASE YourBase SET COMPATIBILITY_LEVEL = 130在不支持的账号下运行请求。

    通过 xml 变体(2005 年工作)

    declare @q xml
    set @q= cast(replace(replace( (select x from @tab),'{','<q>'),'}','</q>') as xml)      
    select substring(id,1, charindex(',',id)-1) id,  
        substring(name,2, charindex('",',name)-2) name
     from (    select substring(q,charindex('"id":',q)+5,100) id, 
              substring(q,charindex('"name":',q)+7,100) name,  q 
    from ( select  T.c.value('.','char(128)') q  from @q.nodes('/q') T(c) ) t)t        
    

    链接

    • JSON http://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql
    • 超过 8K 的行http://stackoverflow.com/questions/14259630/how-to-call-webservice-from-tsql-sql-server-2000
    • WinHttpRequest http://docs.microsoft.com/en-us/windows/desktop/winhttp/winhttprequest
    • OLE 配置http://docs.microsoft.com/en-us/sql/database-engine/configure-windows/ole-automation-procedures-server-configuration-option
    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5