Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Syntax for 'Select count(*) into' for dynamic SQL

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
vd123

New User


Joined: 30 Jul 2009
Posts: 7
Location: Pune

PostPosted: Thu Dec 30, 2010 9:04 pm    Post subject: Syntax for 'Select count(*) into' for dynamic SQL
Reply with quote

Hi
I have to get total records for a dynamic sql. I tried all combination with count(*) but the program fails while preparing the dynamic sql statement with -104.(or while fetching the data it goes with this error)
e.g
Select count(*) into ?
from ...
where...
(? is a parameter marker which I tried passing as :W-count) but it failed.

I tried giving below combination also but failed.
Select count(*) into :?
from ...
where...

Can anyone please suggest me the syntax to get the count in dynamic sql
Alias is not allowed to use here so I need to get the data in variable to send ahead.

Thanks
Back to top
View user's profile Send private message

Ronald Burr

Active User


Joined: 22 Oct 2009
Posts: 293
Location: U.S.A.

PostPosted: Fri Dec 31, 2010 4:35 am    Post subject:
Reply with quote

From the DB2 Application Programming and SQL Guide:

"Dynamic SELECT statements cannot use INTO. Therefore, you must use a cursor to put the results into host variables."
Back to top
View user's profile Send private message
vd123

New User


Joined: 30 Jul 2009
Posts: 7
Location: Pune

PostPosted: Fri Dec 31, 2010 10:54 am    Post subject: Reply to: Syntax for 'Select count(*) into' for dynamic SQL
Reply with quote

could you please give an example how to do it?
As when I am fetching the cursor I am getting -313 error which says that the there is mismatch in host variables and the parameter markers(?) in the prepared sql statement
Back to top
View user's profile Send private message
vd123

New User


Joined: 30 Jul 2009
Posts: 7
Location: Pune

PostPosted: Fri Dec 31, 2010 11:09 am    Post subject: Reply to: Syntax for 'Select count(*) into' for dynamic SQL
Reply with quote

Also I am trying to get the count from SQLERRD(3) will this be correct
as my prepared sql statement looks like this
SELECT COUNT(*)
From Tab1, tabl2
Where tab1.field 1= tab1.field 1 and
tab1.field 2= tab1.field 1 and
tab1.field 3 = ? and
tab2.field 2 = ?

Fetch::
Exec SQL
fetch cursor1
end exec
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6970
Location: porcelain throne

PostPosted: Fri Dec 31, 2010 2:15 pm    Post subject:
Reply with quote

vd123,

since we are approaching a few days of holiday
(which means you have some extra time to spend learning things
that you should have learned before)
here are a few links
to provide you some insight into the normally acquired skills of a software engineer:

DB2 on-campus videos
IBM Developerworks suggest that you become a member, it is free, and there is a lot of information available at this website.

IBM Redbooks search for dynamic sql, there are several Redbooks that discuss and provide examples of dynamic sql.
Back to top
View user's profile Send private message
ajeshrn

New User


Joined: 25 Mar 2009
Posts: 78
Location: India

PostPosted: Wed Jan 05, 2011 12:31 pm    Post subject: Reg : Dynamic SQL
Reply with quote

Hi vd123,

Dynamic SQL when used in COBOL program, to get the count I would suggest the below code.

Code:

01 VAR-1            X(20)
     "SELECT COUNT(*) INTO"
01 COUNT-1        S9(4)
01 VAR-1            X(20)
     "from ... "




In run time , COUNT-1 will be populated with the value.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Wed Jan 05, 2011 9:28 pm    Post subject:
Reply with quote

Hello,

Did you actually test this before posting. . .?

Is the manual wrong?
Quote:
From the DB2 Application Programming and SQL Guide:

"Dynamic SELECT statements cannot use INTO. Therefore, you must use a cursor to put the results into host variables."
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts ISMF Difference between volume count ... upendrasri IBM Tools 2 Tue Dec 05, 2017 12:40 pm
No new posts VBS and VB, COBOL syntax is the same ... natt.sut COBOL Programming 3 Sun Nov 12, 2017 6:36 am
No new posts How to write Rexx program to size and... sreejeshcs CLIST & REXX 14 Thu Oct 12, 2017 7:26 am
No new posts column with count of rows within dist... ronald wouterson DB2 4 Sun Sep 17, 2017 9:48 pm
No new posts Updating the Trailer count in variabl... satheshbabur DFSORT/ICETOOL 6 Wed Aug 30, 2017 9:49 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us