using (OracleConnection conn1 = new OracleConnection(oradb1))
{
conn1.Open();
using (OracleCommand crtCommand = new OracleCommand("WITH src
AS (SELECT src.table_name src_table_name,
src.column_name src_col_name,
src.data_type src_data_type,
src.data_length src_data_len,
src.data_precision src_data_precision,
src.data_scale src_data_scale,
src.nullable src_nullable,
Decode(T.constraint_type, 'P', 'Primary Key',
'U', 'Unique',
'') AS src_cons
FROM all_tab_columns src
left join (SELECT Cc.column_name,
Uc.constraint_type
FROM user_cons_columns cc,
user_constraints uc
WHERE Cc.constraint_name = Uc.constraint_name
AND Cc.table_name = Uc.table_name) t
ON T.column_name = Src.column_name
WHERE table_name = 'INSTRUCTOR'
AND owner = 'ERHAN'),
tgt
AS (SELECT tgt.table_name tgt_table_name,
tgt.column_name tgt_col_name,
tgt.data_type tgt_data_type,
tgt.data_length tgt_data_len,
tgt.data_precision tgt_data_precision,
tgt.data_scale tgt_data_scale,
tgt.nullable tgt_nullable,
Decode(T.constraint_type, 'P', 'Primary Key',
'U', 'Unique',
'') AS tgt_cons
FROM all_tab_columns tgt
left join (SELECT Cc.column_name,
Uc.constraint_type
FROM user_cons_columns cc,
user_constraints uc
WHERE Cc.constraint_name = Uc.constraint_name
AND Cc.table_name = Uc.table_name) t
ON T.column_name = tgt.column_name
WHERE table_name = 'INSTRUCTOR'
AND owner = 'SARIGUL'),
col_details
AS (SELECT src.src_table_name,
Nvl(tgt.tgt_table_name, First_value(tgt_table_name)
over(
ORDER BY tgt_table_name nulls last))
tgt_table_name,
src.src_col_name,
src.src_data_type,
src.src_data_len,
src.src_data_precision,
src.src_data_scale,
src.src_nullable,
src_cons,
tgt.tgt_col_name,
tgt.tgt_data_type,
tgt.tgt_data_len,
tgt.tgt_data_precision,
tgt.tgt_data_scale,
tgt.tgt_nullable,
tgt_cons
FROM src
full outer join tgt
ON ( src.src_col_name = tgt.tgt_col_name ))
SELECT *
FROM (SELECT CASE
WHEN tgt_data_type != src_data_type
OR tgt_data_len != src_data_len
OR tgt_data_precision != src_data_precision
OR tgt_data_scale != src_data_scale
OR tgt_nullable != src_nullable THEN
'alter table '
|| tgt_table_name
|| ' modify '
|| tgt_col_name
|| ' '
|| src_data_type
|| ' '
|| CASE
WHEN src_data_type IN
( 'DATE'
) THEN NULL
ELSE
CASE
WHEN src_data_type
IN (
'VARCHAR',
'VARCHAR2'
) THEN ' ('
||
Nvl(To_char(src_data_len), ' ')
|| ') '
ELSE Decode(Nvl(src_data_precision, -1), -1, NULL,
Nvl(To_char(src_data_precision), ' ')
|| ', '
|| Nvl(To_char(src_data_scale), ' ')
|| ')')
END
END
|| CASE
WHEN tgt_nullable = 'Y' THEN ' null '
ELSE ' not null '
END
|| tgt_cons
WHEN tgt_col_name IS NULL THEN 'alter table '
|| tgt_table_name
|| ' add '
|| src_col_name
|| ' '
|| ' '
|| ' '
|| src_data_type
|| ' '
|| CASE
WHEN src_data_type IN (
'DATE' )
THEN NULL
ELSE
CASE
WHEN src_data_type IN (
'VARCHAR', 'VARCHAR2' )THEN
'('
||
Nvl(To_char(src_data_len), ' ')
||
') '
ELSE Decode(Nvl(src_data_precision, -1
), -1, NULL,
Nvl(To_char(src_data_precision),
' ')
|| ', '
|| Nvl(To_char(src_data_scale),
' ')
|| ')')
END
END
|| tgt_cons
WHEN src_col_name IS NULL THEN 'alter table '
|| tgt_table_name
||' drop '
||tgt_col_name
END alter_statement
FROM col_details)
WHERE alter_statement IS NOT NULL; ", conn1))
{
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText(crtCommand.ExecuteNonQuery().ToString());
richTextBox1.AppendText(Environment.NewLine);
}
}
質問する
125 次