1

我正在收集一些 API 信息,我编写了一个程序来读取域并查找 API 信息,并在找到信息后将其制作的整个 JSON 文本放入JSONDATAMicrosoft SQL server table 的列中[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);

这是运行更新语句后表的外观(缺少域列,因为这是一张较旧的图片 图片示例: 数据类型都可以设置为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 查询创建的新列中。

PS任何人都可以解释我自己记住和学习将来如何进行这种修改的最简单方法吗?

我试过但没有运气:

 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];
4

1 回答 1

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');
于 2017-08-08T14:04:09.483 回答