Solved

Using stored procedure return value in batch file

Posted on 2009-07-15
5
1,299 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
For loop to add numbers 3 85
Recursively Delete Files 5 94
Batch/VBScript : Disable Windows tasks 7 40
AS400 QAOSDIAJRN / APYJRNCHG Processing After Sync 3 9
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 …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

856 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