Home > database >  How to use "SELECT * INTO ..." using laravel
How to use "SELECT * INTO ..." using laravel

Time:11-15

I'm new to laravel and I cant seem to run this query in laravel.

SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset;

I have tried the following without any success

$archive_db = "SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset" ;
config(['database.connections.sqlsrv.database' => 'tmp']);
DB::connection('sqlsrv')->raw($archive_db);

Any help would be highly apreciated.

CodePudding user response:

With the following code you will get a PDO instance:

DB::connection('sqlsrv')->getPDO();

So you can execute a raw query with the following code:

$sql = "SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset;";
DB::connection('sqlsrv')->getPDO()->query($sql);

Another approach is DB::statement($sql);

$sql = "SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset;";
DB::connection('sqlsrv')->statement($sql);
  • Related