列如 表USERA  NAME AGE
     表USERB  NAME AGE ADDRESS插入语句using (OracleConnection connection = new OracleConnection(WebConfigHelper.ConnectionString))
            {
                connection.Open();
                OracleTransaction transaction = connection.BeginTransaction();
                try
                {
                    string strSql = "INSERT INTO USERA(NAME,AGE) VALUES('{0}','{1}') INSERT INTO USERB(NAME ,AGE, ADDRESS) VALUES('{2}','{3}','{4}')";
                    strSql = string.Format(strSql, "小明", "12", "小红", "15", "北京市");
                    OracleHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql);
                    transaction.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    LogHelper.WriteLog("执行方法AddCiname时出错,异常详细:" + e.Message);
                    return false;
                }
            }想用 一个strSql 字符串完成两条Sql语句 求高手解答我这样写行不行,有其他方法嘛? 本人新手没用过

解决方案 »

  1.   

    最简单的,一条SELECT结果向多个表中插入: insert all
    into test1(id,name,sex,cj)
    into test2(id,name,sex,cj)
    into test3(id,name,sex,cj)
    select id,name,sex,cj from test ;  依据条件实现: insert all
    when id <10 then
    into test1(id,name,sex,cj)
    when id >10 and id<50  then
    into test2(id,name,sex,cj)
    when id >50 and id<1000  then
    into test3(id,name,sex,cj)
    select id,name,sex,cj from test
      

  2.   

    http://blog.csdn.net/zftang/article/details/6208357
      

  3.   

    我懂了
    我的sqlstring strSql = "insert all
    INTO USERA(NAME,AGE) VALUES('{0}','{1}') 
    INTO USERB(NAME ,AGE, ADDRESS) VALUES('{2}','{3}','{4}')";
                        strSql = string.Format(strSql, "小明", "12", "小红", "15", "北京市");
    这样吧
      

  4.   

    还是不行 的需要Select  可我要Select 没有用啊  等待高手
      

  5.   

    我觉得 你不要用占位符了 ,方法还是 1楼的方法 strSql直接赋值下面的语句,然后 执行 应该就可以 ,你试试吧    INSERT ALL WHEN ADDRESS IS NULL THEN INTO USERA
      VALUES
        (NAME,
         AGE,
         ADDRESS
         ) WHEN ADDRESS IS NOT NULL  THEN INTO USERB
       VALUES
       (NAME,
         AGE,
         ADDRESS 
       )
       SELECT  '小明' NAME, '12' AGE ,'' ADDRESS FROM DUAL  
       UNION ALL 
       SELECT  '小红' NAME , '15' AGE , '北京市' ADDRESS  FROM DUAL 
     
      

  6.   

    语句有一点问题 , 应该改成 
     
     INSERT ALL WHEN ADDRESS IS NULL THEN INTO USERA
      VALUES
        (NAME,
         AGE
         ) WHEN ADDRESS IS NOT NULL  THEN INTO USERB
       VALUES
       (NAME,
         AGE,
         ADDRESS 
       )
       SELECT  '小明' NAME, '12' AGE ,'' ADDRESS FROM DUAL  
       UNION ALL 
       SELECT  '小红' NAME , '15' AGE , '北京市' ADDRESS  FROM DUAL ;