将SELECT查询更改为UPDATE查询(解析JSON)

时间:2017-08-08 13:03:00

标签: json sql-server-2017

我正在处理一些API信息收集,并且我已经编写了一个程序来读取域并查找API信息,在找到信息之后,它将整个JSON文本放入列中Microsoft SQL Server表JSONDATA中的[dbo].[NoveDomeneAUG]。域列表存储在名为DomainsNew

的列名称下的同一个表中

我有这样的陈述,我需要更改为UPDATE查询或使其作为UPDATE查询工作,这意味着它将从JSONDATA列读取数据并将解析后的数据存储在新列中。

 SELECT * 
 , JSON_VALUE(JSONDATA, '$.domain') AS Domain,
 , JSON_VALUE(JSONDATA, '$.name') AS CompanyName
 , JSON_VALUE(JSONDATA, '$.legalName') AS legalName
 , JSON_VALUE(JSONDATA, '$.category.sector') AS CategorySector
 , JSON_VALUE(JSONDATA, '$.category.industryGroup') AS CategoryIndustryGroup
 , JSON_VALUE(JSONDATA, '$.category.industry') AS CategoryIndustry
 , JSON_VALUE(JSONDATA, '$.category.subIndustry') AS CategorySubIndustry
 , JSON_VALUE(JSONDATA, '$.category.sicCode') AS CategorySicCode
 , JSON_VALUE(JSONDATA, '$.category.naicsCode') AS CategoryNaicsCode
 , JSON_VALUE(JSONDATA, '$.metrics.employees') AS EmployeesNumber
 , JSON_VALUE(JSONDATA, '$.metrics.employeesRange') AS EmployeesRange
 , JSON_VALUE(JSONDATA, '$.metrics.marketCap') AS MarketCap
 , JSON_VALUE(JSONDATA, '$.metrics.annualRevenue') AS AnnualRevenue
 , JSON_VALUE(JSONDATA, '$.parent.domain') AS ParentDomain
   FROM dbo.NoveDomeneAUG;

SELECT查询生成的列可以复制到同一个表或另一个表中。此查询将使我需要填写的所有列[NoveDomeneAUG]

ALTER TABLE [Domainbank].[dbo].[NoveDomeneAUG] ADD
    Domain nvarchar(50),
    CompanyName nvarchar(50),
    legalName nvarchar(50),
    CategorySector nvarchar(50),
    CategoryIndustryGroup nvarchar(50),
    CategoryIndustry nvarchar(50),
    CategorySubIndustry nvarchar(50),
    CategorySicCode nvarchar(50),
    CategoryNaicsCode nvarchar(50),
    EmployeesNumber nvarchar(50),
    EmployeesRange nvarchar(50),
    MarketCap nvarchar(50),
    AnnualRevenue nvarchar(50),
    ParentDomain nvarchar(50);

这是表格在运行更新语句后的样子(缺少域列,因为这是较旧的图片  Picture example: 数据类型都可以设置为varcharnvarchar

这是其中一个JSON输出的外观:

    {
  "id": "3e3562a7-b160-4fd8-a190-8ca0a5288794",
  "name": "Garmin",
  "legalName": "Garmin Ltd.",
  "domain": "garmin.com",
  "domainAliases": [
    "garmin.de",
    "garmin.se",
    "garmin.si",
    "garmin.nl",
    "garmin.dk",
    "garmin.it",
    "garmin.fi",
    "garmin.no",
    "garmin.hr",
    "garmin.at",
    "garmin.pl",
    "garmin.cl",
    "garmin.com.au",
    "garmin.co.uk",
    "garmin.be",
    "garminconnect.com",
    "garmin.com.mx",
    "garminservice.de",
    "garmin.es",
    "garminasus.com",
    "garminonline.de",
    "opencaching.com",
    "garmin.com.br",
    "garmin.pt",
    "garminfrance.com",
    "garmin.ro",
    "garmin.com.hr",
    "garmin.com.ar",
    "garmin.ca",
    "inreachdelorme.com"
  ],
  "site": {
    "title": "Garmin International | Home",
    "h1": "NEW VIRB® 360",
    "metaDescription": "Delivering innovative GPS technology across diverse markets, including aviation, marine, fitness, outdoor recreation, tracking and mobile apps.",
    "metaAuthor": "Garmin",
    "phoneNumbers": [
      "+1 913-397-8200"
    ],
    "emailAddresses": [
      "domain.admin@garmin.com",
      "productsupport.privacy@garmin.com",
      "webmaster@garmin.com",
      "garminsecurity@garmin.com",
      "copyright@garmin.com"
    ]
  },
  "category": {
    "sector": "Information Technology",
    "industryGroup": "Software & Services",
    "industry": "Internet Software & Services",
    "subIndustry": "Internet Software & Services",
    "sicCode": "3812",
    "naicsCode": null
  },
  "tags": [
    "Technology",
    "E-commerce",
    "Enterprise",
    "B2B",
    "B2C",
    "Consumer Discretionary",
    "Consumer Electronics"
  ],
  "description": "Delivering innovative GPS technology across diverse markets, including aviation, marine, fitness, outdoor recreation, tracking and mobile apps.",
  "foundedYear": 1989,
  "location": "Olathe, KS 66062, USA",
  "timeZone": "America/Chicago",
  "utcOffset": -5,
  "geo": {
    "streetNumber": null,
    "streetName": null,
    "subPremise": null,
    "city": "Olathe",
    "postalCode": "66062",
    "state": "Kansas",
    "stateCode": "KS",
    "country": "United States",
    "countryCode": "US",
    "lat": 38.8271089,
    "lng": -94.7898731
  },
  "logo": "https://logo.clearbit.com/garmin.com",
  "facebook": {
    "handle": "garmin",
    "likes": 1542748
  },
  "linkedin": {
    "handle": "company/garmin-international"
  },
  "twitter": {
    "handle": "Garmin",
    "id": "15324722",
    "followers": 136976,
    "following": 812,
    "location": "at Garmin HQ just outside KC",
  },
  "crunchbase": {
    "handle": "organization/garmin"
  },
  "emailProvider": false,
  "type": "public",
  "ticker": "GRMN",
  "phone": "+41 52 630 16 00",
  "metrics": {
    "alexaUsRank": 1094,
    "alexaGlobalRank": 1535,
    "googleRank": null,
    "employees": 10000,
    "employeesRange": "1000+",
    "marketCap": 9800000000,
    "raised": null,
    "annualRevenue": 3018665000,
    "fiscalYearEnd": 12
  },
  "indexedAt": "2017-07-06T02:54:05.626Z",
  "tech": [
    "centos",
    "akamai_dns",
    "apache",
    "outlook",
    "microsoft_office_365",
    "google_analytics",
    "microsoft_exchange_online",
    "debian",
    "tealium",
    "youtube",
    "recaptcha"
  ],
  "similarDomains": [
    "cerner.com",
    "delorme.com",
    "fitbit.com",
    "google.com",
    "gpscity.com",
    "lowrance.com",
    "magellangps.com",
    "novatel.com",
    "polar.com",
    "suunto.com",
    "thegpsstore.com",
    "trimble.com"
  ]
}

^^这是存储在[JSONDATA]中的数据类型,我们将从列中获取该数据,并将数据存储在我们之前使用ALTER TABLE查询创建的新列中。

P.S。任何人都可以解释我最简单的方法来记住并学习如何在将来自己做这种修改吗?

  

我试过但没有运气:

 WITH newvals AS (
      SELECT * ,
           JSON_VALUE([JSONDATA], '$.domain') AS Domain,
             JSON_VALUE([JSONDATA], '$.name') AS CompanyName,
             JSON_VALUE([JSONDATA], '$.legalName') AS legalName,
             JSON_VALUE([JSONDATA], '$.category.sector') AS CategorySector,
             JSON_VALUE([JSONDATA], '$.category.industryGroup') AS CategoryIndustryGroup,
             JSON_VALUE([JSONDATA], '$.category.industry') AS CategoryIndustry,
             JSON_VALUE([JSONDATA], '$.category.subIndustry') AS CategorySubIndustry,
             JSON_VALUE([JSONDATA], '$.category.sicCode') AS CategorySicCode,
             JSON_VALUE([JSONDATA], '$.category.naicsCode') AS CategoryNaicsCode,
             JSON_VALUE([JSONDATA], '$.metrics.employees') AS EmployeesNumber,
             JSON_VALUE([JSONDATA], '$.metrics.employeesRange') AS EmployeesRange,
             JSON_VALUE([JSONDATA], '$.metrics.marketCap') AS MarketCap,
             JSON_VALUE([JSONDATA], '$.metrics.annualRevenue') AS AnnualRevenue,
             JSON_VALUE([JSONDATA], '$.parent.domain') AS ParentDomain
      FROM [Domainbank].[dbo].[NoveDomeneAUG] nda
     )
UPDATE ad
    SET CompanyName = newvals.CompanyName,
        legalName = newvals.legalName,
      CategorySector = newvals.CategorySector,
      CategoryIndustryGroup = newvals.CategoryIndustryGroup,
      CategoryIndustry = newvals.CategoryIndustry,
      CategorySubIndustry = newvals.CategorySubIndustry,
      CategorySicCode = newvals.CategorySicCode,
      CategoryNaicsCode = newvals.CategoryNaicsCode,
      EmployeesNumber = newvals.EmployeesNumber,
      EmployeesRange = newvals.EmployeesRange,
      MarketCap = newvals.MarketCap,
      AnnualRevenue = newvals.AnnualRevenue,
      ParentDomain = newvals.ParentDomain
  FROM [Domainbank].[dbo].[NoveDomeneAUG] ad
  INNER JOIN newvals
  ON ad. = newvals. ;
     

对于这一个我创建了一个新表[Domenetest],但仍然没有运气

INSERT INTO [Domainbank].[dbo].[Domenetest]
  SELECT JSON_VALUE([JSONDATA], '$.domain') AS Domain,
             JSON_VALUE([JSONDATA], '$.name') AS CompanyName,
             JSON_VALUE([JSONDATA], '$.legalName') AS legalName,
             JSON_VALUE([JSONDATA], '$.category.sector') AS CategorySector,
             JSON_VALUE([JSONDATA], '$.category.industryGroup') AS CategoryIndustryGroup,
             JSON_VALUE([JSONDATA], '$.category.industry') AS CategoryIndustry,
             JSON_VALUE([JSONDATA], '$.category.subIndustry') AS CategorySubIndustry,
             JSON_VALUE([JSONDATA], '$.category.sicCode') AS CategorySicCode,
             JSON_VALUE([JSONDATA], '$.category.naicsCode') AS CategoryNaicsCode,
             JSON_VALUE([JSONDATA], '$.metrics.employees') AS EmployeesNumber,
             JSON_VALUE([JSONDATA], '$.metrics.employeesRange') AS EmployeesRange,
             JSON_VALUE([JSONDATA], '$.metrics.marketCap') AS MarketCap,
             JSON_VALUE([JSONDATA], '$.metrics.annualRevenue') AS AnnualRevenue,
             JSON_VALUE([JSONDATA], '$.parent.domain') AS ParentDomain
      FROM [Domainbank].[dbo].[AugustDomain];

1 个答案:

答案 0 :(得分:0)

这是我正在寻找的查询:

UPDATE [Domainbank].[dbo].[NoveDomeneAUG]
SET 
"CompanyName" = JSON_VALUE("JSONDATA", '$.name'), 
"legalName" = JSON_VALUE("JSONDATA", '$.legalName'),
"CategorySector" = JSON_VALUE("JSONDATA", '$.category.sector'),
"CategoryIndustryGroup" = JSON_VALUE("JSONDATA", '$.category.industryGroup'),
"CategoryIndustry" = JSON_VALUE("JSONDATA", '$.category.industry'),
"CategorySubIndustry" = JSON_VALUE("JSONDATA", '$.category.subIndustry'),
"CategorySicCode" = JSON_VALUE("JSONDATA", '$.category.sicCode'),
"CategoryNaicsCode" = JSON_VALUE("JSONDATA", '$.category.naicsCode'),
"EmployeesNumber" = JSON_VALUE("JSONDATA", '$.metrics.employees'),
"EmployeesRange" = JSON_VALUE("JSONDATA", '$.metrics.employeesRange'),
"MarketCap" = JSON_VALUE("JSONDATA", '$.metrics.marketCap'),
"AnnualRevenue" = JSON_VALUE("JSONDATA", '$.metrics.annualRevenue'),
"ParentDomain" = JSON_VALUE("JSONDATA", '$.parent.domain');