Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using stored procedure return value in batch file

Posted on 2009-07-15
5
Medium Priority
?
1,438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 total points
ID: 24867016
db2 "select count(*) into CNT from administrator.test_title"  > a.txt
var1=`grep '^     ' a.txt`
echo $var1
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1200 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 46

Expert Comment

by:Kent Olsen
ID: 24877673
Ahh...

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

Apologies,
Kent
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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