Author |
Topic |
byecoliz
Starting Member
6 Posts |
Posted - 2012-07-25 : 12:51:19
|
I need to develop a DDL generator.the source is the modeling tool which should out put DDL scripts of a specified platform.with MS SQL server one can generate scripts. my problem is how to do this. please help.thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-25 : 12:55:22
|
what will user input for this?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-07-25 : 13:07:47
|
try this, it is a work in progress. I have not figured out how to exclude phantom tables and system tables. Also it does not handle ntext and xml data types which will require you to clean the script up. Of course test always on a local db, unless your local db is your production db which in that case you need some medication :)select so.name, 'create table [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' ENDfrom sysobjects socross apply (SELECT ' ['+column_name+'] ' + data_type + case data_type when 'sql_variant' then '' when 'text' then '' when 'decimal' then '(' + cast(numeric_precision_radix as varchar) + ', ' + cast(numeric_scale as varchar) + ')' else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' + case when exists ( select id from syscolumns where object_name(id)=so.name and name=column_name and columnproperty(id,name,'IsIdentity') = 1 ) then 'IDENTITY(' + cast(ident_seed(so.name) as varchar) + ',' + cast(ident_incr(so.name) as varchar) + ')' else '' end + ' ' + (case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + case when COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ COLUMN_DEFAULT ELSE '' END + ', ' from information_schema.COLUMNS where [TABLE_NAME] NOT LIKE 'vw%' and [TABLE_NAME] = so.name order by ordinal_position FOR XML PATH('')) o (list)left join information_schema.table_constraints tcon tc.Table_name = so.NameAND tc.Constraint_Type = 'PRIMARY KEY'cross apply (select '[' + Column_Name + '], ' FROM information_schema.key_column_usage kcu WHERE kcu.Constraint_Name = tc.Constraint_Name ORDER BY ORDINAL_POSITION FOR XML PATH('')) j (list)where xtype = 'U'AND name NOT IN ('dtproperties')and so.Category <> 2 <><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-07-25 : 13:09:55
|
this is for dropping the tables (user carefully)select 'IF OBJECT_ID(''' + so.name + ''', ''U'') IS NOT NULL BEGIN DROP TABLE dbo.' + so.name + ' END'from sysobjects sowhere so.name NOT LIKE 'sys%'and so.xtype = 'U' <><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
byecoliz
Starting Member
6 Posts |
Posted - 2012-07-26 : 04:22:20
|
quote: Originally posted by yosiasz try this, it is a work in progress. I have not figured out how to exclude phantom tables and system tables. Also it does not handle ntext and xml data types which will require you to clean the script up. Of course test always on a local db, unless your local db is your production db which in that case you need some medication :)select so.name, 'create table [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' ENDfrom sysobjects socross apply (SELECT ' ['+column_name+'] ' + data_type + case data_type when 'sql_variant' then '' when 'text' then '' when 'decimal' then '(' + cast(numeric_precision_radix as varchar) + ', ' + cast(numeric_scale as varchar) + ')' else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' + case when exists ( select id from syscolumns where object_name(id)=so.name and name=column_name and columnproperty(id,name,'IsIdentity') = 1 ) then 'IDENTITY(' + cast(ident_seed(so.name) as varchar) + ',' + cast(ident_incr(so.name) as varchar) + ')' else '' end + ' ' + (case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + case when COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ COLUMN_DEFAULT ELSE '' END + ', ' from information_schema.COLUMNS where [TABLE_NAME] NOT LIKE 'vw%' and [TABLE_NAME] = so.name order by ordinal_position FOR XML PATH('')) o (list)left join information_schema.table_constraints tcon tc.Table_name = so.NameAND tc.Constraint_Type = 'PRIMARY KEY'cross apply (select '[' + Column_Name + '], ' FROM information_schema.key_column_usage kcu WHERE kcu.Constraint_Name = tc.Constraint_Name ORDER BY ORDINAL_POSITION FOR XML PATH('')) j (list)where xtype = 'U'AND name NOT IN ('dtproperties')and so.Category <> 2 <><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion
Dear Yosiasz,This is nice to give me a hint. but it will give script for only one RDBMS.Remember i need a generic script generator,say if user specifies paameter @RBDMS then DDL script out put should be for that RDBMS. More help please.thanks |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-07-26 : 12:37:21
|
got it in that case you will probably need another scripting language such as powershell to do that maybe. since each RDBMS has it's own syntax the above was specific to ms sql server. I am not sure if other RDBMSs have information_schema tables that keep tracks of database's schema information.<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
byecoliz
Starting Member
6 Posts |
Posted - 2012-07-26 : 15:13:24
|
Hi Yosiasz,I created mapping tables to capture these different RDBMS syntax,please help me to think how can use the same technique you used for script generation in sql server using these mapping tables in addition to information schema.thanks a lot.quote: Originally posted by yosiasz got it in that case you will probably need another scripting language such as powershell to do that maybe. since each RDBMS has it's own syntax the above was specific to ms sql server. I am not sure if other RDBMSs have information_schema tables that keep tracks of database's schema information.<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion
|
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-07-27 : 11:55:32
|
sure I will help you think. what are you RDBMs types?<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
byecoliz
Starting Member
6 Posts |
Posted - 2012-08-02 : 04:50:02
|
Hi yosiasz,Sorry for delaying to reply.Currently I considered Oracle,mysql,ms sql syntaxes and data types.But the script has to be generic in that if RDBMS is now DB2,then no need to change the script but instead add more information to the mapping tables.I hope you understand what I mean please. Thanks a lot.quote: Originally posted by yosiasz sure I will help you think. what are you RDBMs types?<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion
|
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-08-08 : 15:58:42
|
not impossible but a huge task because these rdmbs might/will have different syntax. You will have to build a DDL definition syntax dictionary , map it to rdbms table, that way you can have a many to many relationship between rdbms_type table and DDLCommand Table. It will require some manual input.<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
byecoliz
Starting Member
6 Posts |
Posted - 2012-08-16 : 10:13:41
|
thanks yosiasz fro that idea.quote: Originally posted by yosiasz not impossible but a huge task because these rdmbs might/will have different syntax. You will have to build a DDL definition syntax dictionary , map it to rdbms table, that way you can have a many to many relationship between rdbms_type table and DDLCommand Table. It will require some manual input.<><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion
|
 |
|
|