Announcement

Collapse
No announcement yet.

insert data from another table with looping

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • insert data from another table with looping

    Hallo the master
    I have difficulties to insert data into TABLE-B using existing data in TABLE-A and is repeated as many as the number of variables that exist.

    TABLE-A
    - name
    - Cb
    - Cbd
    - Date

    TABLE-B
    - Name
    - Cb
    - Cbd
    - Date
    - Month
    - Year
    The following is a script that I use in key processes
    ######
    /variable dari database
    sc_lookup(qdefault, "select Date,Cb,Cbd from table-a where person = '{person}'");
    $dt = {qdefault[0][0]};
    $cb = {qdefault[0][1]};
    $cbd = {qdefault[0][2]};

    //variable dari kontrol
    $x1= {sbln};//start month
    $x2= {ebln};//end month
    $yr= {year};//defined year by user
    $pers = {person};
    $user = [usr_login];

    //proses insert ke table-b
    if({person}<>""){
    for ($x = $x1; $x <= $x2; $x++) {
    sc_exec_sql("insert into table-b(Name,Date,Month,Year,Cb,Cbd)
    values($pers,$dt,$x,$yr,$cb,$cbd)");
    }//this query is running well
    }
    else//in this query I want to insert data from a table-a with the amount of data 100, to the table-b but with repetition as much as $ x, year = $ yr with a different month figures.
    for ($x = $x1; $x <= $x2; $x++) {
    sc_exec_sql("insert into table-b(Name,Date,Month,Year,Cb,Cbd)
    SELECT Name,Date,$x,Year,Cb,Cbd from table-a");
    }// this query error
    ######


    how to solve it

  • #2
    Gabung Variable dengan Perintah Macro

    Originally posted by mumtaz View Post
    Hallo the master
    I have difficulties to insert data into TABLE-B using existing data in TABLE-A and is repeated as many as the number of variables that exist.

    TABLE-A
    - name
    - Cb
    - Cbd
    - Date

    TABLE-B
    - Name
    - Cb
    - Cbd
    - Date
    - Month
    - Year
    The following is a script that I use in key processes
    ######
    /variable dari database
    sc_lookup(qdefault, "select Date,Cb,Cbd from table-a where person = '{person}'");
    $dt = {qdefault[0][0]};
    $cb = {qdefault[0][1]};
    $cbd = {qdefault[0][2]};

    //variable dari kontrol
    $x1= {sbln};//start month
    $x2= {ebln};//end month
    $yr= {year};//defined year by user
    $pers = {person};
    $user = [usr_login];

    //proses insert ke table-b
    if({person}<>""){
    for ($x = $x1; $x <= $x2; $x++) {
    sc_exec_sql("insert into table-b(Name,Date,Month,Year,Cb,Cbd)
    values($pers,$dt,$x,$yr,$cb,$cbd)");
    }//this query is running well
    }
    else//in this query I want to insert data from a table-a with the amount of data 100, to the table-b but with repetition as much as $ x, year = $ yr with a different month figures.
    for ($x = $x1; $x <= $x2; $x++) {
    sc_exec_sql("insert into table-b(Name,Date,Month,Year,Cb,Cbd)
    SELECT Name,Date,$x,Year,Cb,Cbd from table-a");
    }// this query error
    ######


    how to solve it

    coba dg ini :
    $sql="insert into table-b(Name,Date,Month,Year,Cb,Cbd)
    SELECT Name,Date,".$x.",Cb,Cbd from table-a";
    sc_exec_sql($sql);

    Comment


    • #3
      Siip sdh oke

      ternyata tinggal di kasih kutip satu saja di variable loopnya '$x'...
      Matur nuwun mas yono

      Originally posted by suyonob View Post
      coba dg ini :
      $sql="insert into table-b(Name,Date,Month,Year,Cb,Cbd)
      SELECT Name,Date,".$x.",Cb,Cbd from table-a";
      sc_exec_sql($sql);

      Comment

      Working...
      X