Utility to create sql statements - sql

This is a question about Sql generation, not sql creating sql nor ORM.
Is their any cross database tools that will enable the creation of insert statements, e.g. for all the tables in particular schema or namespace. Say the namespace/schema is Aqua in Sql Server 2008, and your utility comes along and generates all possible insert statements for that namespace/schema. And it works on Oracle/MySql/Postgres/db2 etc.
Thanks.
Bob

ANSI SQL provides for a standard set of views under the schema INFORMATION_SCHEMA to provide metadata for just this purpose.
For generating simple table insert statement templates, all the information you really need to generate an insert statement for a given table is to execute this query:
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG = <my-db-name>
and TABLE_SCHEMA = <table-owner-schema>
and TABLE_NAME = <table-name>
order by ORDINAL_POSITION
in any database that supports the ANSI information schema views. That will give you one row for every column in the specified table, in the expected sequence.
Outside of the above, since no two vendors support the set of system tables with metadata, your pretty much SOL for a cross-database solution. And sadly, I don't believe the Oracle supports the ANSI information schema views.
Though you might look at Red Gate's product family: http://www.red-gate.com/

Related

I need to compare column structure of two tables in different databases but on the same instance in SQL Server 2016

I need to compare column structure of two tables in different databases but on the same instance in SQL Server 2016!
Use System Information Schema Views. This should get you started:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
Use this query to get information of column structure in a database and compare against another db
use DB
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='YourTableName'
This is external software, but unless you have Visual Studio premium where the functionality is built in, you can use something like Red Gate Schema Compare as a trial version to compare and generate a script to synchronise your tables, stored procedures etc.

postgresql list all table fields information within a server

i'm in need to create VIEWS in postresql 9, to mimic oracle's col table,
basically it should display ALL table fields information from ALL table and ALL database on that server.
Can someone please point me a way? thanks.
Unlike Oracle, PostgreSQL implements the ANSI information_schema.
So Oracle's ALL_TAB_COLUMNS view corresponds to information_schema.columns
But this is only restricted to the current database. It is not possible to get this information for all databases - which is the same for Oracle, ALL_TAB_COLUMNS only shows the columns for the current database (=instance)
More details about the information_schema can be found in the manual
http://www.postgresql.org/docs/current/static/information-schema.html
I don't think is possible to get metadata information from a different database of that you've working right now.
To extract metadata from the current database, take a look here: http://www.alberton.info/postgresql_meta_info.html

CREATE TABLE reverse engineering in Oracle

In Oracle SQL Developer, there's a "SQL" tab for each table. This tab contains most of the SQL code (CREATE TABLE, CREATE TRIGGER, etc) that's needed to recreate the table.
Is this information available programatically from the database system, or is this an application feature of SQL Developer? If the former, what commands/statements would I need to run to retrieve this information? If the later, are there any clever ways to get SQL Developer to export these statements?
If you are using Oracle 9i+ then you are looking for the DBMS_METADATA package. http://download.oracle.com/docs/cd/B10500_01/appdev.920/a96612/d_metada.htm. It will allow you to extract whatever DDL you want.
If you are looking for more specific information there is a whole host of views you can access for specific data elements similar to the ones given by #Quassnoi.
There are lots of information, but here are the main queries:
SELECT *
FROM dba_tables
SELECT *
FROM dba_tab_columns
SELECT *
FROM dba_ind_columns
To see what SQL Developer actually outputs, enable trace for all sessions with a LOGON TRIGGER and look into the trace file created by SQL Developer's internal session.
You are looking for the DDL of your database objects.
You can use the Oracle bundled DBMS_METADATA package to get it, from any PL/SQL prompt, with the GET_DDL function.
I use TOAD vs Oracle SQL Developer.
When I click on a "Script" tab when viewing an object (like a table) TOAD executes a whole host of queries and then compiles the "script" from the output of all of these queries.
dba_tables
dba_tab_columns
dba_ind_columns
...
I think replicating this functionality would be a tedious task.

Listing all tables in a database

Is there a SQL command that will list all the tables in a database and which is provider independent (works on MSSQLServer, Oracle, MySQL)?
The closest option is to query the INFORMATION_SCHEMA for tables.
SELECT *
FROM INFORMATION_SCHEMA.Tables
WHERE table_schema = 'mydatabase';
The INFORMATION_SCHEMA is part of standard SQL, but not all vendors support it. As far as I know, the only RDBMS vendors that support it are:
MySQL
PostgreSQL
Microsoft SQL Server 2000/2005/2008
Some brands of database, e.g. Oracle, IBM DB2, Firebird, Derby, etc. have similar "catalog" views that give you an interface where you can query metadata on the system. But the names of the views, the columns they contain, and their relationships don't match the ANSI SQL standard for INFORMATION_SCHEMA. In other words, similar information is available, but the query you would use to get that information is different.
(footnote: the catalog views in IBM DB2 UDB for System i are different from the catalog views in IBM DB2 UDB for Windows/*NIX -- so much for the Universal in UDB!)
Some other brands (e.g. SQLite) don't offer any queriable interface for metadata at all.
No. They all love doing it their own little way.
No, the SQL standard does not constrain where the table names are listed (if at all), so you'll have to perform different statements (typically SELECT statements on specially named tables) depending on the SQL engine you're dealing with.
If you are OK with using a non-SQL approach and you have an ODBC driver for the database and it implements the SQLTables entry-point, you possibly might get the information you want!
pjjH
details on the API at:
http://msdn.microsoft.com/en-us/library/ms711831.aspx

How to generate sql scripts from a query

Does anyone know how to generate SQL scripts from a query?
For example,
Script some tables.
Do custom action 1.
Script the views.
Do custom action 2.
Etc.
It sounds like you want to write a cursor to execute custom SQL. This is common and easy to do. What you need to do is specify a few things to help us more completely answer your question:
What type of SQL server are you using? (MSSQL, Oracle, MySQL)
What language are you writing in? (Java, C++, PL/SQL, TSQL)
You can either write code (Java / C++) to generate SQL from a query, or possibly use a cursor to iterate over recordsets (PL/SQL / TSQL). You can use the results to give you information that can then be executed as SQL via an exec (of some kind depending on the language).
... but please investigate SQL injection before implementing dynamic SQL. Look into Parameterized Queries...
With Microsoft Sql Server, the best way to script database objects is to use SMO. Sql Management Objects is a c# api, but you could always execute t-sql scripts from c# using a SqlClient.
You could want something like
select 'UPDATE '+table_name+ ' SET description=''(new!) ''+description WHERE description_date>''2008-11-01'''
from information_schema.tables where table_name like '%Description'
(this query generates queries which prepend value of description column with '(new!) ' for each recent row in each table which name ends with 'Description' in a fictional database).
The system view INFORMATION_SCHEMA.TABLES contains data about all database tables, there are also INFORMATION_SCHEMA.VIEWS, INFORMATIONS_CHEMA.COLUMNS and other system views in INFORMATION_SCHEMA table schema.
Hope this will help.