How to concatenate SQL statements?

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 is incorrect, how can I concatenate multiple SQL statements by having them run simultaneously?

    
asked by anonymous 26.12.2014 / 16:27

1 answer

1

According to the MySQL documentation ( link ), you can not 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
    
26.12.2014 / 17:04