Answer the question
In order to leave comments, you need to log in
How to execute such query in MSSQL Server from Laravel?
Good day! I ran into such a problem:
There is a remote SQL Server, like 2012 (but this is not accurate), in which there is a stored procedure that must be used for authorization in a Laravel application, an example query:
declare @Entity int, @CompName varchar(250)
exec [foo].[prcBar] 'userlogin', 'userpass', @Entity output, @CompName output
select @Entity, @CompName
$test = DB::transaction(function(){
DB::statement('declare @Entity int, @CompName varchar(250)');
DB::statement('exec [foo].[prcBar] ?, ?, @Entity output, @CompName output', ['userlogin', 'userpass']);
return DB::select('select @Entity, @CompName');
});
SQLSTATE[HY000]: General error: 20018 Must declare the scalar variable "@Entity". [20018] (severity 15) [exec [foo].[prcBar] 'userlogin', 'userpass', @Entity output, @CompName output] (SQL: exec [foo].[prcBar] userlogin, userpass, @Entity output, @CompName output)",
$test = DB::select(DB::raw('declare @Entity int, @CompName varchar(250)
exec [foo].[prcBar] ?, ?, @Entity output, @CompName output
select @Entity, @CompName'), ['userlogin', 'userpass']);
$db = DB::connection()->getPdo();
$stmt = $db->prepare("
declare @Entity int, @CompName varchar(250)
exec [foo].[prcBar] 'userlogin', 'userpass', @Entity output, @CompName output
select @Entity, @CompName
");
$stmt->execute();
$test = $stmt->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
Answer the question
In order to leave comments, you need to log in
In general, after several hours of kicking the sqlsrv driver built into laravel, I came to the conclusion that the problem is in the driver itself. I went poking around and looking for solutions to work through odbc and found the following package: techscope/laravel-sqlserver
When installing by composer, an error occurs, they say ext-odbc is not installed, installed with the command (ubuntu)
Further, my problem remained, but ext-odbc was definitely installed, because. displayed in the list of connected. I wrote off the package for bagul and installed it with the command
After I corrected the sqlsrv section in config/database.php by adding
/* ... */
'sqlsrv' => [
...
'odbc_driver' => '{ODBC Driver 17 for SQL Server}',
'TrustServerCertificate' => 'yes',
...
],
/* ... */
test = DB::select("declare @Entity int, @CompName varchar(250)
exec [foo].[prcBar] 'testlogin', 'testpass', @Entity output, @CompName output
select @Entity as entity, @CompName as comp_name
");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question