2016-10-18 146 views
2

我一直在撞牆,因爲有些東西可能相當明顯,但沒有任何數量的谷歌搜索爲我提供了答案或提示我需要。希望這裏的天才可以幫助我:)現有列中的SQL Server 2016 JSON

我有一個表,看起來有點像這樣:

enter image description here 的JSON已經在我的SQL Server表,基本上是一籃子的產品內容。當前行是整個購買的交易,JSON是每個產品及其各種屬性的另一個子集。

這裏有2行中的JSON字符串作爲例子:

[{"id":"429ac4e546-11e6-471e","product_id":"dc85bff3ecb24","register_id":"0adaaf5c4a65e37c7","sequence":"0","handle":"Skirts","sku":"20052","name":"Skirts","quantity":1,"price":5,"cost":0,"price_set":1,"discount":-5,"loyalty_value":0.2,"tax":0,"tax_id":"dc85058a-a69e-11e58394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":5,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]}] 

[{"id":"09237884-9713-9b6751fe0b85ffd","product_id":"dc85058a-a66b4c06702e13","register_id":"06bf5b9-31e2b4ac9d0a","sequence":"0","handle":"BricaBrac","sku":"20076","name":"Bric a Brac","quantity":1,"price":7,"cost":0,"price_set":1,"discount":-7,"loyalty_value":0.28,"tax":0,"tax_id":"dc85058a-2-54f20388394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":7,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]},{"id":"09237884-9713-9b6","product_id":"dc85058a-a6fe112-6b4bfafb107e","register_id":"06bf537bf6b9-31e2b4ac9d0a","sequence":"1","handle":"LadiesTops","sku":"20040","name":"Ladies Tops","quantity":1,"price":10,"cost":0,"price_set":1,"discount":-10,"loyalty_value":0.4,"tax":0,"tax_id":"dc85058a-a690388394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":10,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]},{"id":"09237884-9713-9b52007fa6c7d","product_id":"dc85058a-a6fa-b4c06d7ed5a","register_id":"06bf537b-cf6b9-31e2b4ac9d0a","sequence":"2","handle":"DVD","sku":"20077","name":"DVD","quantity":1,"price":3,"cost":0,"price_set":1,"discount":-3,"loyalty_value":0.12,"tax":0,"tax_id":"dc85058a-e5-e112-54f20388394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":3,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]}] 

所以我想實現在該列的數據創建一個新表。 (然後我可以通過ID字段中的唯一字符串將產品表加入到第一個表中)。

是否可以使用sql2016中的新本機JSON來完成此操作。

我的替代方案是通過SSIS使用插件來完成,但如果我可以使用SQL Server本身內的存儲過程來完成,它會更乾淨。

提前致謝!

+0

您可以添加預期輸出 –

回答

0

由於Prdp的迴應,是指導我的答案,whic h如下。

SELECT a.ID, b.* -- select ID from original table for proofing, and all from table b 
FROM reporttest a -- table name with alias 
CROSS apply Openjson([register_sale_products]) -- column name 
    WITH (
    id nvarchar(200) '$.id', 
    product_id nvarchar(200) '$.product_id', 
    register_id nvarchar(200) '$.register_id', 
    sequence nvarchar(200) '$.sequence', 
    handle nvarchar(200) '$.handle', 
    sku nvarchar(200) '$.sku', 
    name nvarchar(200) '$.name', 
    quantity nvarchar(200) '$.quantity', 
    price nvarchar(200) '$.price', 
    cost nvarchar(200) '$.cost', 
    price_set nvarchar(200) '$.price_set', 
    discount nvarchar(200) '$.discount', 
    loyalty_value nvarchar(200) '$.loyalty_value', 
    tax nvarchar(200) '$.tax', 
    tax_id nvarchar(200) '$.tax_id', 
    tax_name nvarchar(200) '$.tax_name', 
    --No Tax nvarchar(200) '$.No Tax', 
    tax_rate nvarchar(200) '$.tax_rate', 
    tax_total nvarchar(200) '$.tax_total', 
    price_total nvarchar(200) '$.price_total', 
    display_retail_price_tax_inclusive nvarchar(200) '$.display_retail_price_tax_inclusive', 
    status nvarchar(200) '$.status', 
    CONFIRMED nvarchar(200) '$.CONFIRMED', 
    attributes nvarchar(200) '$.attributes', 
    name nvarchar(200) '$.name', 
    line_note nvarchar(200) '$.line_note', 
    value nvarchar(200) '$.value'  
     ) b -- alias the "with" section as table b 
1

下面是一個使用OPENJSON提取從ID一種方式你JSON

SELECT id 
FROM Yourtable 
CROSS apply Openjson([register_sale_products]) 
WITH (id varchar(500) 'lax $.id') 

有在OPENJSON

  1. 陳吉偉兩個新的路徑模式
  2. 鬆懈

Strict:會當property不在path

lax發現升拋出一個異常:這將返回NULLpropertypath沒有找到。如果你沒有提到任何模式然後Lax將默認使用

可以使用上述模式根據您的需要

DEMO:

架構設置

CREATE TABLE json_test 
    (
    json_col VARCHAR(8000) 
) 

樣本數據

INSERT INTO json_test 
VALUES  ('[{"id":"429ac4e546-11e6-471e","product_id":"dc85bff3ecb24","register_id":"0adaaf5c4a65e37c7","sequence":"0","handle":"Skirts","sku":"20052","name":"Skirts","quantity":1,"price":5,"cost":0,"price_set":1,"discount":-5,"loyalty_value":0.2,"tax":0,"tax_id":"dc85058a-a69e-11e58394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":5,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]}]'), 
      ('[{"id":"09237884-9713-9b6751fe0b85ffd","product_id":"dc85058a-a66b4c06702e13","register_id":"06bf5b9-31e2b4ac9d0a","sequence":"0","handle":"BricaBrac","sku":"20076","name":"Bric a Brac","quantity":1,"price":7,"cost":0,"price_set":1,"discount":-7,"loyalty_value":0.28,"tax":0,"tax_id":"dc85058a-2-54f20388394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":7,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]},{"id":"09237884-9713-9b6","product_id":"dc85058a-a6fe112-6b4bfafb107e","register_id":"06bf537bf6b9-31e2b4ac9d0a","sequence":"1","handle":"LadiesTops","sku":"20040","name":"Ladies Tops","quantity":1,"price":10,"cost":0,"price_set":1,"discount":-10,"loyalty_value":0.4,"tax":0,"tax_id":"dc85058a-a690388394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":10,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]},{"id":"09237884-9713-9b52007fa6c7d","product_id":"dc85058a-a6fa-b4c06d7ed5a","register_id":"06bf537b-cf6b9-31e2b4ac9d0a","sequence":"2","handle":"DVD","sku":"20077","name":"DVD","quantity":1,"price":3,"cost":0,"price_set":1,"discount":-3,"loyalty_value":0.12,"tax":0,"tax_id":"dc85058a-e5-e112-54f20388394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":3,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]}]') 

查詢

SELECT id 
FROM json_test 
CROSS apply Openjson(json_col) 
     WITH (id varchar(500) 'lax $.id') 

結果:

╔═══════════════════════════════╗ 
║    id    ║ 
╠═══════════════════════════════╣ 
║ 429ac4e546-11e6-471e   ║ 
║ 09237884-9713-9b6751fe0b85ffd ║ 
║ 09237884-9713-9b6║ 
║ 09237884-9713-9b52007fa6c7d ║ 
║ 429ac4e546-11e6-471e   ║ 
║ 09237884-9713-9b6751fe0b85ffd ║ 
║ 09237884-9713-9b6║ 
║ 09237884-9713-9b52007fa6c7d ║ 
╚═══════════════════════════════╝ 
+0

謝謝!非常感謝。那讓我成爲那裏的一部分。不過,我想以表格的形式返回JSON字符串的所有部分,而不僅僅是ID列。那可能嗎? – Maz

+0

我想你給我的東西就足以讓我創造一個解決方案!現在正在處理它,如果它能正常工作,它會發回。 – Maz

+0

所以,我有解決方案,謝謝你的幫助。將發佈答案來幫助他人。 – Maz

0

只需簡單的sql查詢,你就會到達那裏。

使這個查詢作爲存儲過程並在需要時調用它..

編輯根據您的要求這個查詢。

更改 '%ID 「:」' 到 '%anything_inside_the_string',你將獲得價值.. :)

DECLARE @LOOP_1 INT=1,@NAME NVARCHAR (MAX),@LEFT NVARCHAR(MAX),@loop_2 int=0 
SET @NAME='[{"id":"429ac4e546-11e6-471e","product_id":"dc85bff3ecb24","register_id":"0adaaf5c4a65e37c7","sequence":"0","handle":"Skirts","sku":"20052","name":"Skirts","quantity":1,"price":5,"cost":0,"price_set":1,"discount":-5,"loyalty_value":0.2,"tax":0,"tax_id":"dc85058a-a69e-11e58394d","tax_name":"No Tax","tax_rate":0,"tax_total":0,"price_total":5,"display_retail_price_tax_inclusive":"1","status":"CONFIRMED","attributes":[{"name":"line_note","value":""}]}]' 

-- First loop started to find where 'id":"' is located 
WHILE @LOOP_1!=(SELECT LEN(@NAME)) 
BEGIN 
    SET @LEFT=(LEFT(@NAME,@LOOP_1)) 
    IF @LEFT LIKE '%id":"' -------- Change '%id":"' to '%product_id":"' and you will get the value.. :) 
    BEGIN 

     set @NAME=(right(@NAME,len(@name)[email protected]_1)) 

     -- Second loop started to find where ',' is located after '"id":"' 
     WHILE @loop_2!=(SELECT LEN(@NAME)) 
     BEGIN 

      SET @LEFT=(LEFT(@NAME,@loop_2)) 
      IF @LEFT LIKE '%,' 
      BEGIN 
       if left(@name,@loop_2-1)like '%"%' 
       SELECT left(@name,@loop_2-2) 
       else 
       SELECT left(@name,@loop_2-2) 
       set @loop_2=(SELECT LEN(@NAME)-1) 
       set @[email protected]_2 

      END 
     SET @[email protected]_2+1 
     END 

    END 
    SET @[email protected]_1+1 
END 
+0

當你有本地支持時,你爲什麼要這麼做 –

+0

@prdp我試圖根據Maz的要求來完成這項任務。他願意嘗試sp而不是原生JSON的解決方案。 –