When I take the code from my earlier post "Using Oracle combine three tables to one with PIVOT" and hit "Run Script" in SQL Developer everything works just perfectly but when I try to execute same script from PHP file I get "ORA-00900 Invalid SQL Statement" -error. SQL Developers "Run Statement" fails as well to execute the code. It seems my code isn't in the "SQL Statement" syntax?
My PHP code for putting sql script in the variable:
$sql = "variable x REFCURSOR
DECLARE
exam_ids VARCHAR2(255);
BEGIN
SELECT
LISTAGG(''''
|| exam_id
|| ''' AS \"'
|| exam_name
|| '\"',',') WITHIN GROUP(
ORDER BY
exam_id ASC
)
INTO exam_ids
FROM
exam;
OPEN :x FOR 'SELECT
*
FROM
(
SELECT
u.user_id,
u.user_name,
e.exam_id,
eu.exam_date
FROM
users u
LEFT JOIN exam_user eu ON u.user_id = eu.user_id
LEFT JOIN exam e ON e.exam_id = eu.exam_id
ORDER BY
u.user_id
)
PIVOT ( MAX ( exam_date )
FOR exam_id
IN ( ' || EXAM_IDS || ' )
)
ORDER BY
1';
END;
/
print x";
Then I pass the $sql variable to function for the results:
function getSQLResult($sql, $conn) {
$stmt = OCIParse($conn, $sql);
if( $stmt === false ) {
errorShutdown(__('...'), __('...'));
die();
} else {
//Executes a statement
if (OCIExecute($stmt)) {
return $stmt;
}
else {
$err = oci_error($stmt);
echo '<pre>';
print_r($err);
echo '</pre>';
return false;
}
}
}
So can anyone show me how to refactor the code?
Thanks for any help!