2016-10-18 59 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的解决方案。 –