View previous topic :: View next topic
|
Author |
Message |
abdul
EXPERT
Joined: 28 Jul 2003 Posts: 23 Location: Bangalore,India
|
|
|
|
Hi,
Can anyone help me with a method to check whether a single record exists in a DB2 Table without using COUNT(*).
Thanks
Abdul |
|
Back to top |
|
|
GG
New User
Joined: 10 Jul 2004 Posts: 1
|
|
|
|
EXEC SQL
SELECT 1
INTO :WS-COUNT
FROM <dbname>.<tbname>
WHERE ?
END-EXEC
IF SQLCODE EQUAL ROW-FOUND
DISPLAY 'SQLCODE = ROW FOUND'
ELSE
IF SQLCODE EQUAL ROW-NOT-FOUND
DISPLAY 'SQLCODE = ROW NOT FOUND'
ELSE
IF SQLCODE EQUAL MULTI-ROWS
DISPLAY 'SQLCODE = MULTIPLE ROWS FOUND'
ELSE
DISPLAY 'PGM T88TST - PG 3000 -'
' - BAD STUFF IS HAPPENING WITH THE SQLCODE '
DISPLAY 'SQLCODE ' SQLCODE
DISPLAY 'SQLERRMC ' SQLERRMC
MOVE Y TO TIME-TO-QUIT-FLAG
GO TO 3000-EXIT
.
The singleton does kick out an SQL Error code -811 when duplicates occur - the way it performs (SELECT 1), it only expects one row to be returned. Sometimes its nice to know if there are duplicates.
---------------------------------------------------------------------
EXEC SQL
SELECT 1
INTO :WS-COUNT
FROM SYSIBM.SYSDUMMY1 A
WHERE EXISTS
(SELECT 1
FROM <dbname>.<tbname> B
WHERE ?
AND A.IBMREQD = A.IBMREQD
)
END-EXEC
IF SQLCODE EQUAL ROW-FOUND
DISPLAY 'SQLCODE = ROW FOUND'
ELSE
IF SQLCODE EQUAL ROW-NOT-FOUND
DISPLAY 'SQLCODE = ROW NOT FOUND'
ELSE
DISPLAY 'PGM T88TST - PG 3000 -'
' - BAD STUFF IS HAPPENING WITH THE SQLCODE '
DISPLAY 'SQLCODE ' SQLCODE
DISPLAY 'SQLERRMC ' SQLERRMC
MOVE Y TO TIME-TO-QUIT-FLAG
GO TO 3000-EXIT
. |
|
Back to top |
|
|
|