将 SELECT 查询更改为 UPDATE 查询(解析 JSON)
Changing SELECT query into UPDATE query (Parsing JSON)
我正在收集一些 API 信息,我编写了一个程序来读取域并查找 API 信息,在找到信息后,它将整个 JSON 它在 Microsoft SQL 服务器 table [dbo].[NoveDomeneAUG]
中制作成列 JSONDATA
的文本。域列表存储在名为 DomainsNew
的列名下的相同 table 中
我有这样的声明,我需要更改为 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 查询生成的列可以复制到同一个 table 或另一个 table。此查询将使我需要填写的所有列 [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);
这就是 table 在更新语句 运行 之后应该如何看待(由于这是一张较旧的图片,因此缺少域列
数据类型都可以设置为 varchar
或 nvarchar
这是 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。任何人都可以解释一个最简单的方法让我记住并学习如何在未来自己进行这种修改吗?
I tried with but no luck:
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. ;
For this one I made a new table [Domenetest]
, still no luck
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];
这是我正在寻找的查询:
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');
我正在收集一些 API 信息,我编写了一个程序来读取域并查找 API 信息,在找到信息后,它将整个 JSON 它在 Microsoft SQL 服务器 table [dbo].[NoveDomeneAUG]
中制作成列 JSONDATA
的文本。域列表存储在名为 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 查询生成的列可以复制到同一个 table 或另一个 table。此查询将使我需要填写的所有列 [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);
这就是 table 在更新语句 运行 之后应该如何看待(由于这是一张较旧的图片,因此缺少域列
varchar
或 nvarchar
这是 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。任何人都可以解释一个最简单的方法让我记住并学习如何在未来自己进行这种修改吗?
I tried with but no luck:
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. ;
For this one I made a new table
[Domenetest]
, still no luckINSERT 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];
这是我正在寻找的查询:
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');