473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Manually Add a Row to a Resultset

I am querying a DB2 database via Sequelink and I want to manually add a
row to my recordset. In SQL Server, I would use a UNION clause like
this:

SELECT
Field1 as ValueField
,Field2 as DescField
FROM TableA
UNION
SELECT
Null as ValueField
,'All' as DescField

Is there similar syntax for DB2?

Thanks in advance.

Dec 19 '05 #1
8 3343
Potter wrote:
I am querying a DB2 database via Sequelink and I want to manually add a
row to my recordset. In SQL Server, I would use a UNION clause like
this:

SELECT
Field1 as ValueField
,Field2 as DescField
FROM TableA
UNION
SELECT
Null as ValueField
,'All' as DescField

Is there similar syntax for DB2?

Thanks in advance.


That should not work as second SELECT does not have FROM clause.

However - in DB2 I could use this construct:

SELECT
field1 AS ValueField,
field2 AS DescField
FROM
tablea
UNION
SELECT
99 AS ValueField,
'All' AS DescField
FROM
sysibm.sysdummy1;

I could also use this:

SELECT
field1 AS ValueField,
field2 AS DescField
FROM
tablea
UNION
VALUES
99,
'All';

Jan M. Nelken
Dec 19 '05 #2
"Potter" <dr********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I am querying a DB2 database via Sequelink and I want to manually add a
row to my recordset. In SQL Server, I would use a UNION clause like
this:

SELECT
Field1 as ValueField
,Field2 as DescField
FROM TableA
UNION
SELECT
Null as ValueField
,'All' as DescField

Is there similar syntax for DB2?

Thanks in advance.


Yes, you but you need a FROM table-name in the 2nd part of the UNION.
Assuming that you want only one row in the second part of the UNION, then
use the table SYSIBM.SYSDUMMY1 (which is guaranteed to only contain one
row). You also need to cast the null as the same data type as ValueField.

Assuming that ValueField is a decimal, then it would be:

SELECT
Field1 as ValueField
,Field2 as DescField
FROM TableA
UNION
SELECT
cast (null as decimal) as ValueField
,'All' as DescField
FROM SYSIBM.SYSDUMMY1

Keep in mind that UNION will eliminate duplicates, and UNION ALL should be
used if you know there are no duplicate rows in the result set.
Dec 19 '05 #3

"Jan M. Nelken" <Un**********@Invalid.Domain> wrote in message
news:qK******************************@rogers.com.. .
Potter wrote:
I am querying a DB2 database via Sequelink and I want to manually add a
row to my recordset. In SQL Server, I would use a UNION clause like
this:

SELECT
Field1 as ValueField
,Field2 as DescField
FROM TableA
UNION
SELECT
Null as ValueField
,'All' as DescField

Is there similar syntax for DB2?

Thanks in advance.


That should not work as second SELECT does not have FROM clause.

However - in DB2 I could use this construct:

SELECT
field1 AS ValueField,
field2 AS DescField
FROM
tablea
UNION
SELECT
99 AS ValueField,
'All' AS DescField
FROM
sysibm.sysdummy1;

I could also use this:

SELECT
field1 AS ValueField,
field2 AS DescField
FROM
tablea
UNION
VALUES
99,
'All';

Jan M. Nelken


For a null try the following:

SELECT
cast (null as decimal) as ValueField
,'All' as DescField
FROM SYSIBM.SYSDUMMY1

In the "null as decimal" you should use the same datatype (decimal, integer)
as exists in the table.
Dec 19 '05 #4
Jan,

Thank you for the help. My sample SELECT without a FROM clause is
valid SQL Server syntax. The necessity of having a FROM clause in DB2
is what I was trying to work around.

I used the VALUES function and was able to achieve what I was after. I
do not have rights to query the SYS tables, but Value worked out
perfect.

Thanks for your time and help.

Andy

Dec 19 '05 #5
Potter wrote:
Jan,

Thank you for the help. My sample SELECT without a FROM clause is
valid SQL Server syntax. The necessity of having a FROM clause in DB2
is what I was trying to work around.

I used the VALUES function and was able to achieve what I was after. I
do not have rights to query the SYS tables, but Value worked out
perfect.

Thanks for your time and help.

Andy

Andy,

Note that unlike Sybase/SQL Server VALUES allows you to add more than
one row. So there is no need to add more than one UNION [ALL]

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Dec 19 '05 #6
Serge,

Can you post a sample of generating multiple rows using VALUES rather
than multiple UNIONS?

Thank you,

Andy

Dec 19 '05 #7
DECLARE GLOBAL TEMPORARY TABLE A (A INT, B INT)
INSERT INTO SESSION.A VALUES (1,2), (3,4)
SELECT A, B FROM SESSION.A UNION VALUES (5,6), (7,8), (9,0) ORDER BY 1
DROP TABLE SESSION.A
COMMIT

HTH,
B.

Dec 19 '05 #8
Potter wrote:
Serge,

Can you post a sample of generating multiple rows using VALUES rather
than multiple UNIONS?

Thank you,

Andy

SELECT x, y FROM T
UNION [ALL]
VALUES (1, 2), (3, 4), (5, 6)

You can use multi row VALUES anywhere you wish, including INSERT or a
JOIN (such as to PIVOT columns into rows)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Dec 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RoyalScorpion | last post by:
hi guys, i get an updatable resultset from a connection then used it to insert a new row but the result set doesn't chane after insertion, i mean the no of rows before insertion is the same after...
2
by: Savas Ates | last post by:
I have a stored procedure below.. When I run it with a well parameter in query analyser 3 of select statements return me. (i named select statements 1,2,3) But in asp page when i call this...
7
by: Gandalf | last post by:
Hi all, I am connecting to DB2 8.1 on Solaris through JDBC type 4 drivers (driver: com.ibm.db2.jcc.DB2Driver). Problem is that when a cursored query is done, the resultset gets closed after...
2
by: Twan Kennis | last post by:
Question: How do I pass a returning resultset from a nested Stored Procedure (which opens a cursor including option "WITH RETURN TO CALLER") as a returning resultset from it's own? When I...
4
by: _link98 | last post by:
Problem: java ResultSet cursor from SQL/PL stored-procedure is FORWARD_ONLY. Is it possible to have ResultSet cursors from SQL/PL procedures to scroll forward and backwards? Perhaps I am missing...
12
by: robertino | last post by:
Hi all, I've put together a few SPs to produce a BOM (bill of materials) listing, which together use a couple of global temp tables, and return the results from a cursor. Here's the code: ...
0
by: David Linsin | last post by:
I created a simple test case to reproduce the problem (also check Bug #15500): import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;...
13
by: ajos | last post by:
hi frnds, im doing some db related operations in my web project,im using mysql 5.0,i have the following piece of code in my action class- for(resultset =...
5
by: lost1 | last post by:
I am having trouble adding a result set to a scroll pane. no matter what I do using getText I get an error. Can someon give me a clue? import java.util.*; import java.sql.*; import java.awt.*;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.