I
I
Igor Braduloff2018-08-28 11:58:11
Yii
Igor Braduloff, 2018-08-28 11:58:11

How to call MS SQL stored procedure in Yii2?

Good afternoon! Tell me what I'm doing wrong when I call a stored procedure in Yii2? In MS SQL itself I call the procedure and it works: EXEC sp_GetRecipientPayments @user = 1, @sd= '2018-08-01', @ed='2018-08-03 ' . The @user data
types are int, @sd and @ed are datetime.
I write in the controller

$user = 1;
        $sd = '2018-08-01';
        $ed = '2018-08-03';

        $command = Yii::$app->db->createCommand("CALL sp_GetRecipientPayments(:p_user,:p_start,:p_end)");
        $command->bindParam(":p_user", $user, PDO::PARAM_INT);
        $command->bindParam(":p_start", $sd, PDO::PARAM_STR);
        $command->bindParam(":p_end", $ed, PDO::PARAM_STR);
        $records = $command->queryAll();

        return $this->render('index',compact('records'));

and
throws Database Exception - yii\db\Exception
SQLSTATE[HY000]: General error: 20018 Incorrect syntax near '1'. [20018] (severity 15) [CALL sp_GetRecipientPayments(1,'2018-08-01','2018-08-03')]
The SQL being executed was: CALL sp_GetRecipientPayments(1,'2018-08-01',' 2018-08-03')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question