0

I have a script which contains some SQL code to create tables. this script contains something like this:

CREATE TABLE Customer
(
    CustomerID integer PRIMARY KEY,
    FirstName varchar(32) NOT NULL,
    LastName varchar(32) NOT NULL,
    Street varchar(32),
    Phone varchar(32)
)

CREATE TABLE Supplier   
(
    SupplierID varchar(8) PRIMARY KEY,
    Name varchar(32) NOT NULL,
    Street varchar(32),
    Phone varchar(32)  
)

I used this code to run this script:

try
{
    var content = File.ReadAllText("c:\\users\\vahid\\desktop\\DBAssignment5\\A3\\SchemaSetUp.sql");
    using (var command = new OracleCommand(content) { Connection = conn })
    {
        command.ExecuteNonQuery();
    }
}

catch (Exception ex)
{
    Console.WriteLine("Something's wrong\n");
    Console.WriteLine(ex.ToString());
}

When the script contains the code of creating just one table, it works well, but when I add codes to create more than one table, I face this exception:

enter image description here

Can anybody please tell me what's wrong and what should I do to fix this problem?

Thank you

4
  • 1
    Try to put a semicolon after the first CREATE TABLE command
    – Steve
    Nov 27, 2015 at 17:03
  • I tried that, but there is an exception which tells "Invalid character"
    – starrr
    Nov 27, 2015 at 17:04
  • possible duplicate see the question: stackoverflow.com/questions/17725379/…
    – are
    Nov 27, 2015 at 17:18
  • 2
    Split the file into seperate commands. OracleCommand probably can't handle more then one command at once. Nov 27, 2015 at 17:56

1 Answer 1

1

If your script contains only CREATE TABLE, CREATE VIEW or GRANT commands you can use CREATE SCHEMA command:

CREATE SCHEMA AUTHORIZATION husqvik
    CREATE TABLE Customer
    (
        CustomerID integer PRIMARY KEY,
        FirstName varchar(32) NOT NULL,
        LastName varchar(32) NOT NULL,
        Street varchar(32),
        Phone varchar(32)
    )

    CREATE TABLE Supplier   
    (
        SupplierID varchar(8) PRIMARY KEY,
        Name varchar(32) NOT NULL,
        Street varchar(32),
        Phone varchar(32)  
    );

Good thing on the fact that this is single atomic SQL command is that all tables/views/grants are created or none. Interesting is that SQL*Plus doesn't recognize this statement and executes only the second CREATE TABLE command.

Otherwise you need to split the script into multiple commands or execute as anonymous block:

BEGIN
    EXECUTE IMMEDIATE
        'CREATE TABLE Customer
        (
            CustomerID integer PRIMARY KEY,
            FirstName varchar(32) NOT NULL,
            LastName varchar(32) NOT NULL,
            Street varchar(32),
            Phone varchar(32)
        )';

    EXECUTE IMMEDIATE
        'CREATE TABLE Supplier   
        (       SupplierID varchar(8) PRIMARY KEY,
            Name varchar(32) NOT NULL,
            Street varchar(32),
            Phone varchar(32)  
        )';
END;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.