1

I'm trying to execute a simple .sql file that resets my database. It works fine when I run it in sqlplus with @, but returns ORA-00922: missing or invalid option, when I run it using php like that:

$query = file_get_contents($path);
$stid = oci_parse($con, $query);
$r = oci_execute($stid);

Here is the .sql file itself, it's quite simple and I simply can't find anything that is wrong with the commands:

ALTER SESSION SET NLS_LANGUAGE = AMERICAN;
ALTER SESSION SET NLS_TERRITORY = AMERICA;

DROP TABLE users;
DROP TABLE friends;
DROP TABLE replies;
DROP TABLE tweets;
DROP TABLE retweets;
DROP TABLE mentions;
DROP TABLE hashtags;

CREATE TABLE users (
    id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
    name VARCHAR2(40) NOT NULL,
    screen_name VARCHAR2(20) NOT NULL,
    followers NUMBER(9) NOT NULL,
    friends NUMBER(9) NOT NULL
);

CREATE TABLE friends (
    id1_str VARCHAR2(20) NOT NULL,
    id2_str VARCHAR2(20) NOT NULL,
    CONSTRAINT friendship PRIMARY KEY (id1_str, id2_str)
);

CREATE TABLE replies (
    id1_str VARCHAR2(20) NOT NULL,
    id2_str VARCHAR2(20) NOT NULL,
    CONSTRAINT reply PRIMARY KEY (id1_str, id2_str)
);

CREATE TABLE tweets (
    id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
    user_id_str VARCHAR2(20) NOT NULL,
    created DATE NOT NULL
);

CREATE TABLE retweets (
    user_id_str VARCHAR2(20) NOT NULL,
    tweet_id_str VARCHAR2(20) NOT NULL,
    CONSTRAINT retweet PRIMARY KEY (user_id_str, tweet_id_str)
);

CREATE TABLE hashtags (
    tweet_id_str VARCHAR2(20) NOT NULL,
    text VARCHAR2(140) NOT NULL,
    CONSTRAINT hashtag PRIMARY KEY (tweet_id_str, text)
);

CREATE TABLE mentions (
    user_id_str VARCHAR2(20) NOT NULL,
    tweet_id_str VARCHAR2(20) NOT NULL,
    CONSTRAINT mention PRIMARY KEY (user_id_str, tweet_id_str)
);

COMMIT;

I know it's probably silly, but I've spent hours looking for the solution to no avail and would appreciate any tips (even a different method than loading this file).

4
  • 2
    Do you need the commit at the end when running with php? Commented Jan 27, 2020 at 18:16
  • Actually, I don't think I do, but removing it didn't help Commented Jan 27, 2020 at 18:17
  • 1
    I would try to run with begin / end before and after the script. Another try would be to run each statement in different oci_execute call. Not sure if multiple statements is possible with oci and php. Commented Jan 27, 2020 at 18:35
  • 1
    Using Commit is redundant. DDL statements do not need it as Felippe pointed out. Commented Jan 27, 2020 at 18:54

1 Answer 1

3

The problem is simple. Your file contains multiple commands, and oci_parse is meant to work with single statements.

You've got two options:

  1. Read the file, parse it for semi-colons, and pass each resulting string (minus the semi-colon) to oci_parse/oci_execute, or
  2. Change your file so it's a valid PL/SQL block, with BEGIN at the beginning and END; at the end, with each of the DDL statements you want to execute in an EXECUTE IMMEDIATE as shown below:
    BEGIN
      EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_LANGUAGE = AMERICAN';
      EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = AMERICA';

      EXECUTE IMMEDIATE 'DROP TABLE users';
      EXECUTE IMMEDIATE 'DROP TABLE friends';
      EXECUTE IMMEDIATE 'DROP TABLE replies';
      EXECUTE IMMEDIATE 'DROP TABLE tweets';
      EXECUTE IMMEDIATE 'DROP TABLE retweets';
      EXECUTE IMMEDIATE 'DROP TABLE mentions';
      EXECUTE IMMEDIATE 'DROP TABLE hashtags';

      EXECUTE IMMEDIATE 'CREATE TABLE users (
          id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
          name VARCHAR2(40) NOT NULL,
          screen_name VARCHAR2(20) NOT NULL,
          followers NUMBER(9) NOT NULL,
          friends NUMBER(9) NOT NULL
      )';

      EXECUTE IMMEDIATE 'CREATE TABLE friends (
          id1_str VARCHAR2(20) NOT NULL,
          id2_str VARCHAR2(20) NOT NULL,
          CONSTRAINT friendship PRIMARY KEY (id1_str, id2_str)
      )';

      EXECUTE IMMEDIATE 'CREATE TABLE replies (
          id1_str VARCHAR2(20) NOT NULL,
          id2_str VARCHAR2(20) NOT NULL,
          CONSTRAINT reply PRIMARY KEY (id1_str, id2_str)
      )';

      EXECUTE IMMEDIATE 'CREATE TABLE tweets (
          id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
          user_id_str VARCHAR2(20) NOT NULL,
          created DATE NOT NULL
      )';

      EXECUTE IMMEDIATE 'CREATE TABLE retweets (
          user_id_str VARCHAR2(20) NOT NULL,
          tweet_id_str VARCHAR2(20) NOT NULL,
          CONSTRAINT retweet PRIMARY KEY (user_id_str, tweet_id_str)
      )';

      EXECUTE IMMEDIATE 'CREATE TABLE hashtags (
          tweet_id_str VARCHAR2(20) NOT NULL,
          text VARCHAR2(140) NOT NULL,
          CONSTRAINT hashtag PRIMARY KEY (tweet_id_str, text)
      )';

      EXECUTE IMMEDIATE 'CREATE TABLE mentions (
          user_id_str VARCHAR2(20) NOT NULL,
          tweet_id_str VARCHAR2(20) NOT NULL,
          CONSTRAINT mention PRIMARY KEY (user_id_str, tweet_id_str)
      )';
    END;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.