在 Db2 中处理 BOOLEAN 值的最佳方法是什么? [英] What is the best way to handle BOOLEAN values in Db2?

查看:65
本文介绍了在 Db2 中处理 BOOLEAN 值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库

Db2

场景

我有一列需要为真或假.我找到了两个指向如何实现这一目标的来源;但是,当我把它们放在一起时,我得到了一个错误.

I have a column that needs to be true or false. I have found two sources that point to how to achieve this; however, when I bring them together I get an error.

  1. 布尔值
  2. 数据类型之间的转换

当前解决方案

CREATE TABLE USERS
(
   ID INT NOT NULL,
   .
   .
   .
   IS_LOCKED SMALLINT NOT NULL WITH DEFAULT 0,
   PRIMARY KEY(ID)
);

SELECT U.ID, CAST(U.IS_LOCKED AS BOOLEAN) as IS_LOCKED FROM USERS U

错误:数据类型为SYSIBM.SMALLINT"的值不能强制转换为类型SYSIBM.BOOLEAN"

问题

如何在 Db2 中使用 BOOLEAN?

How can I use BOOLEANs in Db2?

推荐答案

Linux/Unix/Windows 上的 Db2 V11.1 支持 BOOLEAN 作为列数据类型,这样的列可以在结果集中返回.这是一个使用命令行处理器的示例(在 bash shell 中):

Db2 V11.1 on Linux/Unix/Windows supports BOOLEAN as a column data type and such columns can be returned in a result set. Here is an example using the command-line-processor (at the bash shell):

create table mytable( id integer, mybool boolean with default true )
DB20000I  The SQL command completed successfully.

insert into mytable(id, mybool) values (1, false), (2, true), (3, false)
DB20000I  The SQL command completed successfully.

select id,mybool from mytable order by 1

ID          MYBOOL
----------- ------
          1      0
          2      1
          3      0

  3 record(s) selected.

但是,虽然普通 DDL 和 CLP for SQL DML 支持布尔值,但请考虑使用 Db2 列数据类型布尔值对应用程序的影响.检查 PHP、Python、Java、.net 等如何根据用于访问数据库的任何语言来操作此数据类型.

However, while plain DDL and the CLP for SQL DML support boolean, consider the impact on the applications of using the Db2 column-datatype boolean. Check how PHP , Python, Java, .net, etc can manipulate this datatype according to whatever languages are used to access your databases.

提示:在寻求有关 Db2 的帮助时,明智的做法是始终提及您的 Db2 版本和运行 Db2 服务器的操作系统(即 z/os、iSeries、linux/unix/windows)并相应地标记您的问题.

Tip: when asking for help about Db2, it is wise to always mention your Db2-version and the operating-system that runs the Db2-server (i.e. z/os , iSeries, linux/unix/windows) and tag your question accordingly.

这篇关于在 Db2 中处理 BOOLEAN 值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆