我的列表框中有表名,以检查相同的表是否具有相同的列。例如
listBox3 有 EMPLOYEE 表名我的查询必须检查 EMPLOYEE 表是否有不同的列。
这个查询的工作方式例如:userA 有 Employee 表,而 UserB 也有 Employee 表。在比较了两个用户的 Employee 表之后,我得到了关于 RichTextBox1 的 Alter 语句。
取决于我的 EMPLOYEE 表,我想看到如下结果;
更改表 EMPLOYEE 添加说明 VARCHAR2 (15) ;
更改表 EMPLOYEE 添加 CITY VARCHAR2 (10) ;
但只看到:
更改表 EMPLOYEE 添加说明 VARCHAR2 (15) ;
foreach (string Items in listBox3.Items)
{
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 = '" + Items + "' and owner='" + txtSrcUserID.Text + "'), 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 = '"+Items+"' and owner='" + txtDesUserID.Text + "'), 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))
{
var result = crtCommand.ExecuteScalar();
if (result != null)
{
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText(result.ToString() + ";");
richTextBox1.AppendText(Environment.NewLine);
}
else
{
continue;
}
}
}