?
Solved

Read Stored procedure text from java code.

Posted on 2004-10-26
8
Medium Priority
?
3,245 Views
Last Modified: 2009-07-29
Hello all,
Is there an API available in java, to read the contents of a Stored proc in an SQL server DB? We have about 3000 stored procs and want to create a backup of all of them.
Thanks in advance
0
Comment
Question by:spankenstien
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:spankenstien
ID: 12415011
forgot to mention this. we are using JSQLConnect to connect to SQL server DB
0
 
LVL 6

Expert Comment

by:Duane Lawrence
ID: 12415110
This may not be the java answer you want, but it is a lot easier.  

In enterprise manager, go to the desired database, expand the stored procedure catigory, right click on a stored proc, go to "all tasks", then to "Generate SQL Script", click on the show all button and put a check mark next to "all stored procedures"

Duane
0
 
LVL 15

Assisted Solution

by:SRigney
SRigney earned 75 total points
ID: 12415570
the system stored procedure sp_helptext takes a stored procedure name, so for example you could use

sp_helptext sp_CustomerGetByName

And the output would be the text that is in the stored procedure sp_CustomerGetByName.

This works for views and triggers as well.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:spankenstien
ID: 12415721
Duane,
The reason I need to access the text in java is to look for at particular string and see it is commented.i should given more details. currently im calling "sp_helpText SPName" , looping thru the text and calculating based on the occurences of "/*" and "*/" , relative to the String i am looking for.
0
 
LVL 6

Accepted Solution

by:
Duane Lawrence earned 300 total points
ID: 12416274
I do what I said above and just copy it all to a text file, then search all I want.  But if you have to do it with a program see below.



create proc foo
as
--1.
    select o.id, o.name, o.uid, user_name(o.uid) as username into #temptable
    from dbo.sysobjects o
    where o.type = 'P'

--2. cursor to iterate through all proc names and put the text into another table.

--3. use dts to export to a file

go

Duane
0
 
LVL 34

Expert Comment

by:arbert
ID: 12418153
I prefer not to use the system tables.  You can look at information_schema.routines:

select routine_name,routine_definition from information_schema.routines

You can use Charindex and Substring to find your comment marks....NOW, the only caveat I would give about the information_schema views for the routines is they only return 4000bytes for the routine.  So, if it's over 4000bytes, you do have to use the system tables.

Brett
0
 

Author Comment

by:spankenstien
ID: 12421573
sample code:

        String[][] spNames = new String[1][1];
        String[] spName = new String[1];
        try {
            spNames = dbConn.executeSQL("select distinct so.name from syscomments sc \n" +
                    "\tinner join sysobjects so on sc.id = so.id \n" +
                    "\twhere xtype = 'P' \n" +
                    "\tAND charindex('psg_prog_alleg_id', text) > 0");
        } catch (Exception e) {
            e.printStackTrace();
        }

    outer: for(int i = 0 ;i <spNames.length ; i++){
            spName = spNames[i];

            String[][] sp = new String[1][1];
            try {
                sp = dbConn.executeSQL("sp_helptext " + spName[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String allText = "";
            for (int k = 0; k < sp.length; k++) {
                allText += sp[k][0];
            }
            int psgIndex = allText.lastIndexOf("psg_prog_alleg_id");
            if(psgIndex == -1 )
                psgIndex = allText.lastIndexOf("Psg_prog_alleg_id");
            if(psgIndex == -1 )
                psgIndex = allText.lastIndexOf("PSG_PROG_ALLEG_ID");
            if(psgIndex == -1 )
                psgIndex = allText.lastIndexOf("Psg_Prog_Alleg_id");
            if(psgIndex == -1){
                System.out.println("\n\n\n " + spName[0] + "\n\n\n");
                return;
            }

            String firstHalf = allText.substring(0,psgIndex);
            String secondHalf = allText.substring(psgIndex,allText.length());
            int firstHalfCS = firstHalf.lastIndexOf("/*");
            int firstHalfCE = firstHalf.lastIndexOf("*/");
            if(firstHalfCE < firstHalfCS){
                System.out.println("commented" + spName[0]);
                continue outer;
            }

            PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("C:\\backup\\Allegation\\10-26\\dev\\"+spName[0] +".txt",true)));
            for (int j = 0; j < sp.length; j++) {
                String[] strings = sp[j];
                out1.println(strings[0]);
                //System.out.println("value: " + strings[0]);
            }
            out1.close();

        }
0
 
LVL 34

Expert Comment

by:arbert
ID: 12421604
That might not totally work with SQL2005....
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

579 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