2015-04-22 150 views
0

我有一个名为test1test2的两个表,我想以某种方式将数据从test1移动到test2,就像条件匹配更新数据,否则插入到数据库中。完成Oracle查询我贴就弄伤必须实现两个任务如何将数据从一个表移动到另一个表#

** 1>我要到控制台C#应用程序

2移动操作>我要删除前导空格为条目t2_fNAME和ACCOUNT_NUMBER ** 如何才能实现此任务我需要做ado.net c#代码如果是的话怎么做

merge into test2 a 
using test1 b 
    on (a.t2_NAME = b.t1_NAME) 
when matched then update 
    set a.t2_fNAME = b.t1_fNAME, 
     a.ACCOUNT_NUMBER = b.ACCOUNT_NO, 

when not matched then 
insert (t2_slno,t2_NAME,t2_fNAME,ACCOUNT_NUMBER) 
values (t2_NODE_SEQ.NEXTVAL, b.t1_NAME,b.t1_fNAME,b.ACCOUNT_NO); 
+1

您可以使用上述查询创建存储过程,并从C#应用程序中调用该过程。 –

+0

如何删除前导空格?或修剪条目 – peter

+0

@Selva TS我会照你说的做,但如何在插入或更新时从t2_fNAME,ACCOUNT_NUMBER开始删除空格 – peter

回答

1
  1. 您可以创建一个控制台应用程序并使用ADO.Net在甲骨文执行查询

  2. 使用Trim功能删除前导空格。

下面是代码(未测试,因为我没有Oracle数据库)

using System; 
using System.Data; 
using System.Data.OracleClient; 

namespace TestApp 
{ 
    class Program 
    { 
     static void Main() 
     { 
      string connectionString = "Data Source=ThisOracleServer;Integrated Security=yes;"; 
      string queryString = @"merge into test2 a 
            using test1 b 
             on (a.t2_NAME = b.t1_NAME) 
            when matched then update 
             set a.t2_fNAME = TRIM(b.t1_fNAME), 
              a.ACCOUNT_NUMBER = TRIM(b.ACCOUNT_NO), 

            when not matched then 
            insert (t2_slno,t2_NAME,t2_fNAME,ACCOUNT_NUMBER) 
            values (t2_NODE_SEQ.NEXTVAL, b.t1_NAME,TRIM(b.t1_fNAME),TRIM(b.ACCOUNT_NO));"; 

      using (OracleConnection connection = new OracleConnection(connectionString)) 
      { 
       using (OracleCommand command = connection.CreateCommand()) 
       { 
        command.CommandText = queryString; 

        try 
        { 
         connection.Open(); 
         command.ExecuteScalar(); 
        } 
        catch (Exception ex) 
        { 
         //Log Exception here; 
         throw; 
        } 
       } 
      } 
     } 
    } 
} 

参考

  1. MSDN
  2. Oracle TRIM Function
+0

这是最好的selva,硬编码还是创建存储过程? – peter

+1

我更喜欢Stored Procedure,但是在SP SP内联查询中有很多讨论。请参阅http://dba.stackexchange.com/questions/44544/stored-procedures-vs-inline-sql http://stackoverflow.com/questions/15142/what-are-the-pros-and-cons-to- keep-sql-in-stored-procs-versus-code –

+1

在google中搜索https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=inline%20query %20vs%20stored%20procedure –

相关问题