将表行转换为json格式

时间:2018-08-01 06:37:21

标签: sql sql-server

我的表中有1500行。

通过

将表行转换为json时

查询 select * from tblbook for json auto;

只有一行转换为json。

请帮助我。评论被赞赏

样本json数据:

[    {
      "bid":0,
      "book_type":1,
      "book_id":"426128_1_En",
      "book_s50_due_dt":"2017-08-02T00:00:00",
      "book_s50_finish_dt":"2017-08-02T00:00:00",
      "book_s600_recd_dt":"2017-08-29T00:00:00",
      "book_s600_due_dt":"2017-08-28T00:00:00",
      "book_s600_finish_dt":"2017-08-29T00:00:00",
      "book_s650_recd_dt":"2017-08-29T00:00:00",
      "book_s650_due_dt":"2017-08-29T00:00:00",
      "book_s650_finish_dt":"2017-08-29T00:00:00",
      "book_cover_recd_dt":"2018-07-02T11:59:00",
      "book_cover_due_dt":"2018-07-05T11:59:00",
      "book_cover_recd_flag":false,
      "book_status_remarks":" 10-Succ 7\/20",
      "stage":"s200",
      "issue_pages":0,
      "book_ms_pages":0,
      "book_cover_pages":2,
      "book_blank_pages":0,
      "book_ce_pages":0,
      "book_inputrecdType":"Pdf",
      "book_processType":"Stapp",
      "book_copubname":"",
      "book_authcorr_status":0,
      "book_totalservice":2,
      "book_receiptType":"",
      "book_shortcutwf":1,
      "book_trichy_PE_tag":0,
      "book_trichy_CE_tag":0,
      "book_trichy_location":0,
      "book_trichy_art_location":0,
      "book_FastTrack":false,
      "book_Hold":false,
      "book_Withdrawn":false,
      "book_Project_Manager":"",
      "book_Spl_Instructions":true,
      "book_CreditLine":"",
      "book_Cust_Type":"LNCS ENGG",
      "book_arabic_pages":0,
      "book_roman_pages":0,
      "book_fm_elements":"",
      "book_bm_elements":"",
      "book_bw_figures_count":0,
      "book_color_figures_count":0,
      "book_scan_status":0

这是上面提到的查询的输出结果。

1 个答案:

答案 0 :(得分:0)

我已经在测试表中尝试了该代码,并且工作得很好,我想您的行太多了,因此请确保可以尝试以下方法:

鉴于“出价”列是主键且没有重复项,您可以尝试运行以下命令:

Select 'Select TOP 1 * from tblbook where bid = ' + cast(bid as varchar) + ' for json auto;' from tblbook

该代码将生成查询,并且每个查询都会为表的每一行转换一行,您要做的就是复制粘贴并执行,在执行之前,请确保切换到“文本错误”(快捷键Ctrl + T SQL Server,然后按Ctrl + D切换回网格),这应该给出类似的内容(示例与我的测试表一起使用,是的,它在frensh中):

JSON_F52E2B61-18A1-11d1-B105-00805F49916B
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"Nom":1,"ProdId":100}]

(1 ligne affectée)

JSON_F52E2B61-18A1-11d1-B105-00805F49916B
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"Nom":1,"ProdId":300}]

(1 ligne affectée)

JSON_F52E2B61-18A1-11d1-B105-00805F49916B
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"Nom":1,"ProdId":500}]

(1 ligne affectée)

JSON_F52E2B61-18A1-11d1-B105-00805F49916B
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"Nom":2,"ProdId":600}]

(1 ligne affectée)

您可以使用替换功能将其复制到记事本++之类的东西,以删除不需要的重复文本(例如(1 ligne affectée)和破折号)。

需要很多工作,但我希望这可以帮助您摆脱困境。