Solved

Using stored procedure return value in batch file

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL400 max size 5 95
filter only by one character 4 43
DB2 - LOG FILES. 4 51
Help with simple script. 8 35
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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

822 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