我有两个名为test1
和test2
的表我希望将数据从test1移动到test2,就像条件匹配更新数据一样,否则插入到数据库中。我已成功完成了我发布的oracle查询。我必须完成另外两项任务
** 1>我必须将操作移到控制台c#application
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);
答案 0 :(得分:1)
您可以创建控制台应用程序并使用ADO.Net执行查询
使用Oracle中的Trim
函数删除前导空格。
以下是代码(未经测试,因为我没有Oracle DB)
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;
}
}
}
}
}
}
参考文献