2015-04-22 159 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 –

相關問題