[英]Sequelize: Bulk update or insert into MSSQL DB
I have spent the last 24 hours combing the internet for a solution and have yet to find one.我花了过去 24 小时在互联网上寻找解决方案,但还没有找到。 I am trying to do a bulk upsert (update or insert) of a single model into a mssql db.
我正在尝试将单个模型批量更新插入(更新或插入)到 mssql 数据库中。 bulkCreate with updateOnDuplicate:true does not work with mssql.
带有 updateOnDuplicate:true 的 bulkCreate 不适用于 mssql。 Is there any other solution?
还有其他解决方案吗? This is dataset is anywhere between 1-50k rows.
这是数据集在 1-50k 行之间的任何位置。 Any help would be greatly appreciated!
任何帮助将不胜感激!
Convert your data into a table using OpenJson and merge with the source table as done below:使用 OpenJson 将您的数据转换为表并与源表合并,如下所示:
const upsertedData = [
{
id: "123",
value: "Value 2"
},
{
id: "124",
value: "Value 1"
}
]
const upsert = async (data) => {
await sequelize.query(
`BEGIN
DECLARE @json NVARCHAR(MAX);
SET @json = :dataToBeUpserted
MERGE INTO dbo.tableToBeUpserted AS Target
USING (SELECT * from OpenJson(@json) WITH (
id nvarchar(32),
value nvarchar(32),
)) AS Source
ON (Target.id = Source.id)
WHEN MATCHED THEN
UPDATE SET
Target.value = Source.value
WHEN NOT MATCHED THEN
INSERT (id, value)
VALUES (Source.id, Source.value);
END`,
{
replacements: {
dataToBeUpserted: JSON.stringify(data)
},
}
)
}
await upsert(upsertedData)
For SQL 2016+ you can simply pass your bulk data as JSON using a NVarchar(max) parameter, and parse and load it on the server.对于 SQL 2016+,您可以简单地使用 NVarchar(max) 参数将批量数据作为 JSON 传递,然后在服务器上解析和加载它。 Your query would look something like this:
您的查询将如下所示:
Insert into TargetTable(Number,Date,Customer,Quantity,[Order])
SELECT Number,Date,Customer,Quantity,[Order]
FROM OPENJSON ( @json )
WITH (
Number varchar(200) '$.Order.Number',
Date datetime '$.Order.Date',
Customer varchar(200) '$.AccountNumber',
Quantity int '$.Item.Quantity',
[Order] nvarchar(MAX) AS JSON
);
And you would bind the @json parameter with a NVarchar(max) (string) value like:您可以将 @json 参数与 NVarchar(max) (字符串)值绑定,例如:
[
{
"Order": {
"Number":"SO43659",
"Date":"2011-05-31T00:00:00"
},
"AccountNumber":"AW29825",
"Item": {
"Price":2024.9940,
"Quantity":1
}
},
{
"Order": {
"Number":"SO43661",
"Date":"2011-06-01T00:00:00"
},
"AccountNumber":"AW73565",
"Item": {
"Price":2024.9940,
"Quantity":3
}
}
]
An update would look something like:更新将类似于:
with q as
(
select t.Number, t.Quantity oldQuantity, j.Quantity newQuantity
from OPENJSON ( @json )
WITH (
Number varchar(200) '$.Order.Number',
Date datetime '$.Order.Date',
Customer varchar(200) '$.AccountNumber',
Quantity int '$.Item.Quantity',
[Order] nvarchar(MAX) AS JSON
) j
join TargetTable t
on j.Number = t.Number
)
update q set oldQuantity = newQuantity
See: https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql请参阅: https : //docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql
If you can't bind a parameter, then in a pinch you an include the JSON document as a literal string in your TSQL batch.如果您无法绑定参数,那么在紧要关头,您可以将 JSON 文档作为文字字符串包含在您的 TSQL 批处理中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.