[英]Using java Boolean class as boolean type in SQL native query
I have a use-case of getting the records based on the active flag in Postgres db.我有一个基于 Postgres db 中的活动标志获取记录的用例。
In the rest api,在 rest api 中,
In api the 'active' flag as Boolean class.在 api 中,“活动”标志为 Boolean class。 Using native query for querying, but unable to form the query使用本机查询进行查询,但无法形成查询
select * from records r where r.country='India' and (IF(active IS NULL ) BEGIN '' END ELSE BEGIN r.active = :active END)
But query is failing..但查询失败..
SQL does not support "IF" constructs, moreover you are attempting to create a transaction (begin...) within the select statement. SQL 不支持“IF”结构,此外,您正在尝试在 select 语句中创建事务(开始...)。 It does not work that way.它不是那样工作的。 I suggest you spend a little with the Postgres documentation manual beginning with SQL syntax the or a beginning SQL tutorial/book.我建议您花一点时间阅读以SQL 语法开头的 Postgres 文档手册,或者开始 SQL 教程/书。 As for this in particular it is a pretty basic query:特别是这个,这是一个非常基本的查询:
select r.*
from records r
where is_active_parameter is null
or r.is_active = is_active_parameter;
Passing the parameter depends upon your interface platform (psql, dbeaver, pgAdmin, ...).传递参数取决于您的接口平台(psql、dbeaver、pgAdmin、...)。 I created a demonstration fiddle .我创建了一个演示小提琴。 It defines a Postgres SQL function (to facilitate passing a parameter, but the query runs standalone equally as well).它定义了一个 Postgres SQL function(以方便传递参数,但查询同样独立运行)。 You didn't provide table definition for records so I just made my own.你没有为记录提供表定义,所以我自己做了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.