How to concatenate SQL statements?

Asked

Viewed 102 times

0

$tarefa1 = "ALTER TABLE imovel DROP COLUMN CAMPONULL"; 
$tarefa2 = "ALTER TABLE imovel ADD ENDERECO VARCHAR(300)"; 
$tarefa3 = "ALTER TABLE imovel ADD COMPLEMENTO VARCHAR(3000)";

$pdo->exec($tarefa1, $tarefa2, $tarefa3); <----------

I know that where the arrow is incorrect, how can I proceed to concatenate several SQL statements causing them to be executed simultaneously?

1 answer

1


According to Mysql documentation (http://dev.mysql.com/doc/refman/5.1/en/alter-table.html), you cannot ADD and DROP in the same function, however you can split all DROP and ADD.

Basically the following:

   for($i = 0; $i < count($nome_add); $i++){  //puramente exemplo, pois não disse de onde vinha o nome!
        $add .= "ADD ".$nome_add." VARCHAR(300),";   // ADD Coluna1 VARCHAR(300),ADD Coluna2 VARCHAR(300),ADD Coluna3 VARCHAR(300),  
    }

    for($i = 0; $i < count($nome_drop); $i++){  //puramente exemplo, pois não disse de onde vinha o nome!
        $drop .= $nome_drop.",";         // = Coluna1,Coluna2,Coluna3
    }



    $pdo->exec("ALTER TABLE imovel ".trim($add, ",")); //ALTER TABLE imovel ADD Coluna1 VARCHAR(300),ADD Coluna2 VARCHAR(300),ADD Coluna3 VARCHAR(300)
    $pdo->exec("ALTER TABLE imovel DROP COLUMN ".trim($drop, ",")); //ALTER TABLE imovel DROP COLUMN Coluna1,Coluna2,Coluna3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.