I am using DB2 and Oracle SQL Developer.
How to get the CREATE TABLE Statements from the existing tables?
There are too many tables and it will be a very lengthy process to do manually.
There is a special db2look utility for DDL extraction in Db2. You may refer to its options and their meaning at this link.
If you want SQL access to its capabilities, you may use the SYSPROC.DB2LK_GENERATE_DDL stored procedure supporting most of the utility's options. The routine has an output parameter getting "invocation number" int value after its call.
In case of a single table:
CALL SYSPROC.DB2LK_GENERATE_DDL ('-e -noview -t MY_SCHEMA.MY_TABLE', ?);
SELECT SQL_STMT
FROM SYSTOOLS.DB2LOOK_INFO_V
WHERE OP_TOKEN = <value_of_output_parameter_from_call_above>
ORDER BY OP_SEQUENCE;
In SQLDeveloper if you can see the table there's the initial Create Table Statement in the SQL Tab
You should do that for each table, this is a way to do it but I'm not sure it's fast enough for you.
Related
Is it possible to add description for column while creating table in MSSQL QUERY. I am in need to add description for columns in table while moving data to the table.
It's clunky in SQL Server. For some reason, they've never adopted the COMMENT syntax, and you can't add the comments directly in the CREATE TABLE statement.
After your CREATE statement, run the system stored procedure sp-addextendedproperty
There's an extended conversation on the topic under this question: SQL Comments on Create Table on SQL Server 2008
I want to run unit tests by generating all tables in HSQLDB, present in my oracle database.
For that I want to export all DDL create table statements from oracle tables.
I tried export database, but along with create table sql I am getting lot other SQLs like,
" PARTITION BY RANGE ("CREATION_DATE") " etc.
How do I export all oracle tables(schema) to HSQLDB? is there any better way?
You can use the DBMS_METADATA.GET_DDL() function to get the table definition, and modify what is included with the SET_TRANSFORM_PARAM() options, specifically in this case the PARTITIONING parameter.
There are lots of examples for you to search for, but here's one that shows the DDL being simplified with similar transformations.
It's some work, but you can implement your own tool to create the DDL.
All you need is stored in the Oracle database catalogue.
To create just tables (without index and constraints) you need these 2 tables:
USER_TAB_COLUMNS
USER_TABLES
You will find a detailed documentation of these tablese here:
Oracle Database Reference
Other usefull Oracle tables are
USER_CONSTRAINTS
USER_INDEXES
I have a set of 10 tables in a database (DB1). And there are 10 tables in another database (DB2) with exact same schema on the same SQL Server 2008 R2 database server machine.
The 10 tables in DB1 are frequently updated with data.
I intend to write a stored procedure that would run once every day for synchronizing the 10 tables in DB1 with DB2. The stored procedure would make use of the MERGE statement.
Now, my aim is to make this as generic and parametrized as possible. That is, accommodate for more tables down the line... and accommodate different source and target DB names. Definitely no hard coding is intended.
This is my algorithm so far:
Have the database names as parameters
Have the first query within the stored procedure... result in giving the names of the 10 tables from a lookup table (this can be 10, 20 or whatever)
Have a generic MERGE statement that does the sync for each of the above set of tables (based on primary key?)
This is where I need more inputs on.
What is the best way to achieve this stored procedure? SQL syntax would be helpful.
I had to do something similar, to do that i used a string with a "skeleton" for the merge statement, then i retrieved the list of columns and the pks with a simple query on the sys views.
you could do something similar to build your merge statement, here's a sketch i wrote now as an example (I know it's horrible but i'm not going to write something decent at this hour, and it should give you a hint anyway :P )
SQLFiddle
then you just need to execute it with the usual sp_executesql stored procedure
by the way, always pay attention when building command strings this way, it's not that secure
Sorry if already asked, but I can't find anything on this.
I am moving something over from MySQL to SQL Server I want to have a .sql file create a database and tables within the database. After working out syntax kinks I have gotten the files to work (almost).
If I run
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
it works fine, and if I run
CREATE TABLE dbname.dbo.TABLE1 (
);
...
CREATE TABLE dbname.dbo.TABLEN (
);
it also works fine. But, if I run them in the same file I get this error
Database 'dbname' does not exist
Right now, the CREATE TABLE statements are not within the IF statement, which I would like, but I also cannot seem to find the syntax for that. ( { } does not work?)
So my big question is, how do I ensure a particular command in a .sql file is completed before another in SQL Server?
My second question is, how do I include multiple instructions within an IF clause?
To be clear, I have been running this into sqlcmd.
Put a GO command between queries.
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
GO
CREATE TABLE dbname.dbo.TABLE1 (
);
CREATE TABLE dbname.dbo.TABLEN (
);
As for putting the table statements in the IF, you wouldn't be able to because of the GO command. You could create additional IF statements afterwards, to check for each tables pre-existence.
The syntax for a block if is:
IF condition
BEGIN
....
....
END
Between creating the database and creating the tables you will need a USE statement.
USE dbname
This way the tables will be created in the correct place, without having to specify the DB name on everything.
Also, GO and BEGIN...END like everyone else is saying.
You have to separate the statements with the GO keyword:
sql query
GO
another sql query
GO
and so on
By placing a GO between statements (to create separate batches of statements)
Is it possible to create more than one table at a time using single create table statement.
For MySQL, you can use multi-query to execute multiple SQL statements in a single call. You'd issue two CREATE TABLE statements separated by a semicolon.
But each CREATE TABLE statement individually can create only one table. The syntax supported by MySQL does not allow multiple tables to be created simultaneously.
#bsdfish suggests using transactions, but DDL statements like CREATE TABLE cause implicit transaction commits. There's no way to execute multiple CREATE TABLE statements in a single transaction in MySQL.
I'm also curious why you would need to create two tables simultaneously. The only idea I could come up with is if the two tables have cyclical dependencies, i.e. they reference each other with foreign keys. The solution to that is to create the first table without that foreign key, then create the second table, then add the foreign key to the first table with ALTER TABLE ADD CONSTRAINT. Dropping either table requires a similar process in reverse.
Not with MS SQL Server. Not sure about mysql.
Can you give more info on why you'd want to do this? Perhaps there's an alternative approach.
I don't know, but I don't think you can do that. Why you want to do this?
Not in standard SQL using just the 'CREATE TABLE' statement. However, you can write multiple statements inside a CREATE SCHEMA statement, and some of those statements can be CREATE TABLE statements. Next question - does your DBMS support CREATE SCHEMA? And does it have any untoward side-effects?
Judging from the MySQL manual pages, it does support CREATE SCHEMA as a synonym for CREATE DATABASE. That would be an example of one of the 'untoward side-effects' I was referring to.
(Did you know that standard SQL does not provide a 'CREATE DATABASE' statement?)
I don't think it's possible to create more than one table with a 'CREATE TABLE' command. Everything really depends on what you want to do. If you want the creation to be atomic, transactions are probably the way to go. If you create all your tables inside a transaction, it will act as a single create statement from the perspective of anything going on outside the transaction.