Solved

Using stored procedure return value in batch file

Posted on 2009-07-15
5
1,319 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:
Kent Olsen 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:Kent Olsen
ID: 24877673
Ahh...

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

Apologies,
Kent
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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