Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

CASE clause statement in DB2

I have a table PROCESS . Currently it doesnt not have any records in it. I need to return one hardcoded row if the table doesnt have any record . I am doing a select when the primary key column "id" is null then i hard code the values and return it as below

SELECT CASE WHEN p.ID IS NULL THEN 1 ELSE p.ID END ,
CASE WHEN p.COMPANY IS NULL THEN 'COMP1' ELSE p.COMPANY END
FROM PROCESS p

I took reference from the below link If-else statement in DB2/400

But it always returns me an empty row in DB2 database and not the hardcoded values used in select statement. 08:50:27 SUCCESS SELECT 0.307 0.301 0 Empty result set fetched 08:50:29 FINISHED 0.307 0.301 0 Success: 1 Failed: 0

Please help me on this

question from:https://stackoverflow.com/questions/65912496/case-clause-statement-in-db2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

no way to do in this way, since a primary key could never be null. and select * from empty table return no row (0 row) it do not return null.

you can do it like that:

select ID, COMPANY from PROCESS
UNION ALL
select 1 as ID, 'COMP1' as COMPANY from sysibm.sysdummy1 where (select count(*) from PROCESS) = 0;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...