Solved

Using stored procedure return value in batch file

Posted on 2009-07-15
5
1,252 Views
Last Modified: 2012-06-27
Hi,

The below procedure returns the count of rows in a table. I need to call this Sprocedure from batch file. I need to use the value returned from the procedure in the batch file.

CREATE PROCEDURE get_count()
LANGUAGE SQL
P1:BEGIN ATOMIC
DECLARE CNT INTEGER;
select count(*) into CNT from administrator.test_title;
RETURN CNT;
END P1
@

Please give a solution for this. Thanks!.
0
Comment
Question by:jsaravana
  • 2
  • 2
5 Comments
 

Author Comment

by:jsaravana
ID: 24866693
Hi experts,

Could anybody please address this post.
0
 
LVL 6

Assisted Solution

by:tangchunfeng
tangchunfeng earned 200 total points
ID: 24867016
db2 "select count(*) into CNT from administrator.test_title"  > a.txt
var1=`grep '^     ' a.txt`
echo $var1
0
 
LVL 45

Accepted Solution

by:
Kdo earned 300 total points
ID: 24868537
Hi jsaravana,

Your example seems to be from a SQL Server system.  Then again, the SELECT ... INTO form in SQL Server writes to a table, not a variable.  SQL Server has a non-standard extension that allows stored procedures to return a value.  DB2 conforms to the more widely held standard where stored procedures do not return a value to the calling procedure, but can return values via the parameter list.  If you want to return a value you must write it as a function.

Tangchunfenq's example is fine.  :)  I've written quite a few like it.

If you want a "canned routine" that you can call, create a function and use it.

  CREATE FUNCTION get_count ()
  AS
  SELECT count(*) FROM administrator.test_title;

Then you can call the function

  db2 "SELECT get_count () FROM sysibm.sysdummy1" > a.txt
  var1=`grep '^   ' a.txt`

DB2 requires a table name on the SELECT statement, so you must query against sysibm.sysdummy1.


Good Luck,
Kent
0
 

Author Closing Comment

by:jsaravana
ID: 31604098
Thanks..
Both the logic was helpful for me to find the count. I wanted to call the stored procedure from windows batch file so grep would not have worked. As returning is not possible I stored the count of rows in a file. Then used 'find' and 'fc' command of batch.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 24877673
Ahh...

Windows batch scripting was tagged.  We all missed that part.

Apologies,
Kent
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now