是否可以使用 SpannerConnection.CreateUpdateCommand 根据 where 参数更新 Spanner 记录

问题描述 投票:0回答:1

我目前得到了以下代码

var cmdUpdate _spannerConnection.CreateUpdateCommand(databaseTable, updateColumns);
await cmdUpdate.ExecuteNonQueryAsync();

其中更新列是表中要使用相应值更新的列的列表。主键也包含在列列表中,因为我认为它必须足够聪明才能以某种方式使用它。

它必须产生类似

update A = 'Z', B = 'Y' where PrimaryKeyId = {primaryKeyId}
的东西,这很好地更新了我的记录

但我现在想做如下的事情

update A = 'Z', B = 'Y' where PrimaryKeyId = {primaryKeyId} and ModifiedDate = '010124'

我最初想使用

CreateDMLCommand
但阅读了它说的文档

CreateUpdateCommand(string, SpannerParameterCollection)etc) 是首选,因为它们更高效

参考: https://github.com/googleapis/google-cloud-dotnet/blob/main/apis/Google.Cloud.Spanner.Data/Google.Cloud.Spanner.Data/SpannerConnection.cs#L587

那么,在使用

CreateUpdateCommand
方法时是否可以添加额外的 where 参数,还是我被迫使用
CreateDMLCommand
方法?

c# google-cloud-spanner
1个回答
0
投票

Spanner 支持两种更新数据的方式:Mutations 和 DML

CreateUpdateCommand
使用 Mutations 来更新数据。这比使用 DML 稍微高效一些,但有一个明确的限制:它仅支持根据主键更新行。因此不支持使用像
where PrimaryKeyId = {primaryKeyId} and ModifiedDate = '010124'
这样的表达式。

相反,您需要使用 DML 语句。

© www.soinside.com 2019 - 2024. All rights reserved.