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

Steve
  • 213,761
  • 22
  • 232
  • 286
starrr
  • 1,013
  • 1
  • 17
  • 48

1 Answers1

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;
Husqvik
  • 5,669
  • 1
  • 19
  • 29