如何从数据库中获取存储过程代码?

时间:2015-08-17 20:02:04

标签: sql-server schemacrawler

是否可以使用SchemaCrawler来检索存储过程的代码?

我尝试了Routine.getDefinition(),但它返回了一个空字符串。

更新:这是我的代码:

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.bridge.SLF4JBridgeHandler;
import schemacrawler.schema.Catalog;
import schemacrawler.schema.Column;
import schemacrawler.schema.Routine;
import schemacrawler.schema.Schema;
import schemacrawler.schema.Table;
import schemacrawler.schema.View;
import schemacrawler.schemacrawler.DatabaseConnectionOptions;
import schemacrawler.schemacrawler.RegularExpressionInclusionRule;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.utility.SchemaCrawlerUtility;

/**
 * @author Gili Tzabari
 */
public class ExportScripts {

    public static void main(String[] args) throws SchemaCrawlerException, SQLException {
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();

        // Create a database connection
        final DataSource dataSource = new DatabaseConnectionOptions("jdbc:sqlserver://myDatabase;appName=SchemaCrawler;useCursors=true");
        final Connection connection = dataSource.getConnection("username", "password");

        // Create the options
        final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        // Set what details are required in the schema - this affects the
        // time taken to crawl the schema
        options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
        options.setRoutineInclusionRule(new RegularExpressionInclusionRule("ContentGeneration\\.dbo.*"));
        options.setSchemaInclusionRule(new RegularExpressionInclusionRule("ContentGeneration\\.dbo.*"));

        // Get the schema definition
        final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection,
                options);

        for (final Schema schema : catalog.getSchemas()) {
            System.out.println(schema);
            for (final Routine routine : catalog.getRoutines()) {
                System.out.println("r--> " + routine);
                System.out.println("definition: " + routine.getDefinition());
            }
            for (final Table table : catalog.getTables(schema)) {
                System.out.print("o--> " + table);
                if (table instanceof View) {
                    System.out.println(" (VIEW)");
                } else {
                    System.out.println();
                }

                for (final Column column : table.getColumns()) {
                    System.out.println("     o--> " + column + " ("
                            + column.getColumnDataType() + ")");
                }
            }
        }
    }
}

0 个答案:

没有答案