What I'd like to be able to do is retrieve the schema information for subprograms, functions, package specifications and package bodies from an Oracle 9i database so that I can present them to the user in a C# client using the classes in the System.Data.OracleClient namespace.
So far, I've been able to display the high level schema data far faster than Java applications can, but the packages and functions are beyond my grasp. I can show the columns, their types, the indexes, table- and column level comments, and all sorts of really useful information in really useful ways. Now, if I could just get to the procedures.
Query the data dictionary table ALL_SOURCE http://download.oracle.com/docs/cd/B10501_01/server.920/a96536/ch2124.htm#1300946
Does this help? Not clear whether you wanted to get this via System.Data.OracleClient or via SQL?
SELECT TEXT
FROM ALL_SOURCE
WHERE NAME = <proc_name>
AND OWNER = <schema>
Related
I have around 500 stored procedures that are used for our ETL process. I have been asked to identify all the source and target tables used by each stored procedure. So, a stored procedure could have a connection to an Oracle linked server, or another SQL Server. It could also be using an OPENQUERY to extract data from our transactional systems.
Since I have some basic .NET/C# programming chops, I was hoping to leverage the .NET RegEx class to get started. However, I am looking for suggestions on how I should approach this. I really don't have to reinvent the wheel if someone already has a solution for this.
As a context, we are working on implementing PowerDesigner to store metadata repository. So, we are looking to extract metadata from our BI reports (map reports to it's source tables/views) and our Informatica and T-SQL ETL scripts.
Thanks
I'd suggest a dual-approach. Firstly, I'd avoid using regex for something as complex as SQL Query parsing, especially since there are tools in place for this kind of thing.
https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.dependencywalker.aspx
The SMO library exposes a class that will let you connect to a server and retrieve a dependency tree for a given stored procedure. How to do this exactly is left as an exercise for the reader :)
However, this class won't pick up dependencies that are introduced via dynamic SQL or through OPENQUERY. If the number of procedures that do this are small, I'd recommend doing this manually, and then merging the results. You could use the SMO scripting capabilities to pick up all instances of either OPENQUERY or exec/sp_executesql; at least then you would have an idea of 'suspect' pieces of code.
Merging the results will be tricky. Not only do you have to manually update dependencies for procedures containing dynamic dependencies, but you have to update procedures that depend on procedures containing dynamic dependencies.
You can use a dynamic management view dm_sql_referenced_entities to get some dependency information from SQL Server itself but there are some limitations. Not sure if the Dependency Walker leverages this view, but the pros and cons are very similar.
The same main limitation that I know of and have experienced is that you won't get any dependency information for an object that is leveraged through dynamic sql. We have very contained usages of dynamic sql so I can feel pretty confident leveraging this DMV and manually accounting for the objects hit by those specific procs.
We don't do linked servers, but in my understanding is that those would show in this DMV. I don't know about the OPENQUERY ... I did a little bit of research but I did not test it out but I am guessing those would not be surfaced by the view. Like the previous poster said, you may need a two-pronged approach to get everything you're looking for.
And just for reference, a simple example of using that DMV:
SELECT DISTINCT
[database] = COALESCE(r.referenced_database_name, DB_NAME())
, [schema] = r.referenced_schema_name
, name = r.referenced_entity_name
, r.referenced_id
FROM sys.dm_sql_referenced_entities('dbo.procName_sp', 'OBJECT') AS r
WHERE r.referenced_id IS NOT NULL;
I wouldn't use C# for this. However, maybe something like this will do the job.
select *
from DatabaseName.information_schema.routines
where routine_type = 'PROCEDURE'
SELECT name, type
FROM dbo.sysobjects
WHERE type IN (
'P', -- stored procedures
'FN', -- scalar functions
'IF', -- inline table-valued functions
'TF' -- table-valued functions
)
ORDER BY type, name
Or, if you want SProcs and parameters:
select * from information_schema.parameters
Finally, this link looks pretty helpful for your situation.
http://blog.sqlauthority.com/2010/02/04/sql-server-get-the-list-of-object-dependencies-sp_depends-and-information_schema-routines-and-sys-dm_sql_referencing_entities/
I want to give user a choice to select database in our application where number of options will be available like sqlserver,MySql,Sqlite etc.When user selects the type of database,we will show window which will ask user to input the parameters required for DB connection.This will let us connect to user's DB.But Now we want to show all tables and columns from each table to user to select particular tables and columns from it.Is it possible to implement this behavior using NHibernate?Or do I need to use ADO.NET for it?
I have searched web to check if it is possible using NHibernate but I just got one answer here..
Using nHibernate to retrieve Database Schema
I am not able to understand if this answer will solve my problem.
Thanks in advance
Many database servers implement a schema called INFORMATION. This is part of the ANSI Standard for RDBMSs.
It contains data about the schema, tables, and views in the database. I don't know how consistent the INFORMATION structures are across different implementations (e.g. MS SQL, MySQL, etc), or how many of them actually implement it, but that would be the place to start.
I checked the ranking in DbEngine about 'Wide Column Store' database, the Cassandra seems to be the widest choice at present.
If I understood correctly, the so called 'Wide Column' means the columns for one row are dynamically, such as count and the name of columns, so it doesn't need the Schema stuffs.
But from most articles and documentations online, I found there is always 'CREATE TABLE (...)' CQL query executed firstly, then insert the data with this schema. From my understanding, it's the 'Static Columns' in Cassandra, which has a fixed schema defined. So how to insert data without creating the schema firstly?
And also, I found another item called 'Wide Row', what does it exactly mean, any relations with the 'Wide Column'?
Thanks a lot, the concepts puzzled me a lot.
There are 2 interfaces to access the data in Cassandra - Thrift and CQL.
Thrift is kinda low level and gives you access to "internal" rows (aka Wide rows), and also allows you to use schemaless (dynamic) tables/column families.
CQL tables are built on top of the internal rows, and can only be accessed via CQL. CQL tables allow you to use all modern features like collections, user-types and etc.
You can find more information there: http://www.datastax.com/dev/blog/thrift-to-cql3
How can I know in C# the list of Tables in database.
The list of columns each tables has with complete specification like column one is Id and it has data type of int(50), etc
Use the GetSchema method of the SqlConnection class:
DataTable t = _conn.GetSchema("Tables");
For more info read the MSDN article Retrieving Database Schema Information (ADO.NET). Note that this will get you the results without having to write or directly pass/execute any SQL.
Use the information schem views
http://msdn.microsoft.com/en-us/library/ms186778.aspx
Thy are the "standardized way to look at that and contrary to the sys tables quite guaranteed not to change (while the sys.* tables are an implementation detail).
We have previously used SQL Server Management objects with good success. SMO allows you to interact with the SQL Server quite easily thorugh C#. You just need to reference some DLLs and use the object model provided by them. For example, to list the tables you can use the foreach to iterate over the Database.Tables -property.
Codeproject has an article which goes through the basics: http://www.codeproject.com/KB/database/SMODemo.aspx
I have been asked to generate a Backup Script. Which comprises of a lot of insert statements.
However im not sure how to go about this with SQL to Linq, I have looked at the DataContext Mapping which is able to list all the tables. However I need to be able to then go through each row and grab the data, which will be wrote into the Backup Script.
Any advice would be appreciated.
Thank you
Is it really a requirement to use Linq2SQL. Otherwise I guess the following stored procedure will do?
This procedure generates INSERT statements using existing data from the given tables and views. Later, you can use these INSERT statements to generate the data. It's very useful when you have to ship or package a database application. This procedure also comes in handy when you have to send sample data to your vendor or technical support provider for troubleshooting purposes.
http://vyaskn.tripod.com/code.htm#inserts