【问题标题】:Dynamic Query in SpannerSpanner 中的动态查询
【发布时间】:2020-11-18 08:30:32
【问题描述】:

我正在实现一个搜索屏幕,并且有一些可选参数即将出现。在 oracle 中,我曾经为 Spanner 不支持的可选参数提供 true 或 1=1 条件。 我们如何在 Spanner SQL 中实现同样的功能?

Sample Query 
select mark.* from  
abc mark join xyz mchhier on  mark.X=mchhier.X where 
--Mandatory
mark.X=123 and 
--Below Params are Optional
mchhier.G in (null) and mchhier.C (null) and mchhier.D in (null) 

【问题讨论】:

    标签: google-cloud-spanner


    【解决方案1】:

    Cloud Spanner 还支持 WHERE TrueWHERE 1=1 等条件,因此应该可以使用与 Oracle 中相同的策略。

    以下是一个有效的 Spanner 查询示例:

    select mark.*
    from abc mark
    join xyz mchhier on mark.X=mchhier.X
    where 
    --Mandatory
    mark.X=123
    --@SomeParam could be NULL
    AND CASE
      -- If param is null, the condition will always be true
      WHEN @SomeParam IS NULL THEN TRUE
      ELSE mchhier.G in (@SomeParam)
    END
    

    【讨论】:

    • 当我尝试在 in 子句中给出 true 或 1=1 时,我得到“第 15 行第 21 列的参数类型 INT64 和 {BOOL} 的运算符 IN 没有匹配签名”
    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2021-11-11
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多