db2

how to transform comma separated column into multiples rows in db2

爱⌒轻易说出口 提交于 2019-12-28 18:43:41
问题 I have the following table (the number of the references is variable): Id | FK_ID| Reference | ----------------------- 1 2100 GI2, GI32 2 2344 GI56 And I need the following result: Id | FK_ID| Reference | ----------------------- 1 2100 GI2 2 2100 GI32 3 2344 GI56 Is there any short way to transform the data like this using DB2? 回答1: You really should not be storing data like this. Fortunately, there is a way to undo the damage with recursive SQL, something along these lines: WITH unpivot (lvl

How can I Pivot a table in DB2? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-28 18:05:55
问题 This question already has answers here : Pivoting in DB2 (3 answers) Closed 2 years ago . I have table A, below, where for each unique id, there are three codes with some value. ID Code Value --------------------- 11 1 x 11 2 y 11 3 z 12 1 p 12 2 q 12 3 r 13 1 l 13 2 m 13 3 n I have a second table B with format as below: Id Code1_Val Code2_Val Code3_Val Here there is just one row for each unique id. I want to populate this second table B from first table A for each id from the first table.

How can I Pivot a table in DB2? [duplicate]

雨燕双飞 提交于 2019-12-28 18:05:26
问题 This question already has answers here : Pivoting in DB2 (3 answers) Closed 2 years ago . I have table A, below, where for each unique id, there are three codes with some value. ID Code Value --------------------- 11 1 x 11 2 y 11 3 z 12 1 p 12 2 q 12 3 r 13 1 l 13 2 m 13 3 n I have a second table B with format as below: Id Code1_Val Code2_Val Code3_Val Here there is just one row for each unique id. I want to populate this second table B from first table A for each id from the first table.

How to AUTO_INCREMENT in db2?

亡梦爱人 提交于 2019-12-28 05:56:12
问题 I thought this would be simple, but I can't seem to use AUTO_INCREMENT in my db2 database. I did some searching and people seem to be using "Generated by Default", but this doesn't work for me. If it helps, here's the table I want to create with the sid being auto incremented. create table student( sid integer NOT NULL <auto increment?> sname varchar(30), PRIMARY KEY (sid) ); Any pointers are appreciated. 回答1: You're looking for is called an IDENTITY column: create table student ( sid integer

db2 常见错误以及解决方案

送分小仙女□ 提交于 2019-12-27 01:05:32
操作数据库流程中,遇到许多疑问,很多都与SQL CODE和SQL State有关,现在把一个完整的SQLCODE和SQLState不正确信息和有关解释作以下说明,一来可以自己参考,对DB2不正确自行找出原由 (声明:这是搜集网上的资料得来的,细致出处不记得了) sqlcode sqlstate 说明 格式说明:ErrorCode:-302 SQLState:22001 ------------------------------------------- 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互联系的引用 +098 01568 动态SQL语句用分号结束 +100 02000 没有找到满足SQL语句的行 +110 01561 用DATA CAPTURE定义的表的更新操作不能发送到原来的子系统 +111 01590 为2型索引配置了SUBPAGES语句 +117 01525 要插入的值的个数不等于被插入表的列数 +162 01514 指定的表空间被置为检查挂起状态 +203 01552 运用非唯一的名字来处理命名的限定列 +204 01532 命名的对象未在DB2中定义 +206 01533 命名的列不在SQL语句中指定的任何表中存在 +218 01537 因为SQL语句引用一个远程对象

JDBC 连接DB2时候报错UnsatisfiedLinkError:

我只是一个虾纸丫 提交于 2019-12-26 20:30:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在我从win8降级到win7之后,运行代码,发现报错: com.ibm.db2.jcc.am.SqlException: [jcc][10389][12245][4.19.26] 装入本机库 db2jcct2, java.lang.UnsatisfiedLinkError: no db2jcct2 in java.library.path 时产生故障 奇怪了,重装系统以前明明是好的,怎么现在不行了? 谷歌一下有了解到,是调用了本地动态链接库。可是为什么会调用本地的东西呢?一般连数据库不就是url加驱动就OK了么 继续谷歌,原来是连接DB2又分Type2、Type4等方式,Type2方式需要调用本地链接库,而Type4不需要。 这样,基本上问题的原因定位到了,再看一下自己的Datasource配置,发现 HikariDataSource ds = new HikariDataSource(); ds.setDataSourceClassName("com.ibm.db2.jcc.DB2DataSource"); String jdbcUrl = String.format("jdbc:db2://%s:%s/%s", dbIP, dbPort, dbName); logger.debug("解析jdbcUrl:{

DB2中一些常用sql函数

三世轮回 提交于 2019-12-26 17:00:34
1.merge into ...using ...when matched then ... 应用场景:此函数一般用于表与表之间字段的更新,判断B表和A表是否满足ON中条件,如果满足则用B表去更新A表,如果不满足,则将B表数据插入A表,是有有很多可选项。 用例:有一个表T,有两个字段a、b,我们想在表T中做Insert/Update,如果条件满足,则更新T中b的值,否则在T中插入一条记录。 merge into 目标表 t1 using 源表 t2 on(t1.条件字段1 = t2.条件字段1 and t1.条件字段2 = t2.条件字段2 ……) when matched then update set t1.更新字段 = t2.字段 when not macthed then insert into t1(字段1, 字段2 ……)values(值1, 值2 ……) 2.replace update [table_name] set [column_name] = replace([column_name], '被替换的数据', '替换的数据') 3.case when ... end case when 条件 then 结果1 else 结果2 end 其中when可以重复多次 示例: select case when 条件2 then 结果1 when 条件2 then

DB2分页

戏子无情 提交于 2019-12-26 16:02:07
前言 最近在做一个DB2的项目,遇到分页处理的设计时开始犯难。以前一直采用MySQL作为项目数据库,其中的Limit关键字非常人性化,MySQL把分页的处理逻辑封装到了数据库的核心中,使得做查询设计时,根本不用过多的考虑分页的问题。 可是DB2却把这个难题推到了我们面前。其实不止DB2如此,很多大型的数据库例如MS SQL Server也不支持分页关键字。当然,DB2中提供了RowNumber函数,同Oracle有一些类似。有很多解决方案都是由此关键字得来的。 为了解决这个问题,互联网上提供了如下几个解决方案: 方案一:利用JDBC2的数据集。JDBC2数据集中提供了absolute方法,用来在查询的结果集中进行定位,数据集保存在内存中,你必须告诉JDBC你想定位的绝对位置,这个位置由你来计算。对于海量的数据集,这种方式效率并不高。 方案二:利用DB2自带的函数 ROWNUMBER() OVER(ORDER BY sort-key),这里的RowNumber函数是通过排序计算出来的行的顺序号。根据这个原理,可以先SELECT满足WHERE条件的所有记录,然后用ORDER BY排序,在行号的基础上,结合子查询的组合得出查询结果。例如: select * from ( select rownumber() over(order by foo.bar, foo.baz) as

how to break the loop in db2 database

别等时光非礼了梦想. 提交于 2019-12-25 20:45:05
问题 i have created 1 procedure and used 1 cursor, using this cursor i have used to loop but that loop fetching from cursor is going to infinite loop. below my code for procedure. db2 "CREATE OR REPLACE PROCEDURE NEWEMPSELECT7 RESULT SETS 1 LANGUAGE SQL BEGIN DECLARE OUTRATE VARCHAR(50); DECLARE C1 CURSOR for SELECT Emp_name FROM NEWEMP WHERE Emp_id=100; OPEN C1; LOOP FETCH FROM C1 INTO OUTRATE; CALL DBMS_OUTPUT.PUT_LINE(OUTRATE); END LOOP; CLOSE C1; END" The result is going to infinite loop. even

DB2 “Triggers” on actions beyond update, insert and delete

吃可爱长大的小学妹 提交于 2019-12-25 18:56:07
问题 After researching triggers, I've only come up with thing showing how to update, insert and delete. It seems like that's even part of the syntax itself. DB2 Docs on Triggers Is there any kind of trigger, or something similar, which would let me track a larger set of actions, things like SELECT and ALTER TABLE ? We (unfortunately) share a database with some teams which we don't strictly trust to not do things like run insane SELECT statements (locking up the databases) or ALTER TABLE without us