I am trying to create empty tables on Oracle as a part of initial setup for one of the internal tools. Before, I had a SQL script that had the create statements separated using semi-colon and was executing them without checking for existence as follows:
Setup.sql:
CREATE TABLE ex.employees (
empID NUMBER,
firstName VARCHAR(255),
lastName VARCHAR(255),
deptID NUMBER
)
COMPRESS
NOLOGGING;
CREATE TABLE ex.departments (
deptID NUMBER,
deptDesc VARCHAR(255)
)
COMPRESS
NOLOGGING;
Code Block that parses the file:
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
asQueries = ReadFile("Setup.sql").ToString.Split(";")
For Each sQuery In asQueries
If sQuery.Trim <> "" Then
command.CommandText = sQuery.Trim
command.CommandType = CommandType.Text
command.Connection = conn
command.ExecuteNonQuery()
End If
Next
This code worked fine but I wanted to add logic so that I could check the existence of the table & if it already exists, dropt it before I recreate it. So I made the following changes:
setup.sql:
DECLARE
v_sQuery VARCHAR2(1000);
v_iTableCount INTEGER;
BEGIN
SELECT COUNT(1)
INTO v_iTableCount
FROM all_tables
WHERE UPPER(owner) = 'EX'
AND UPPER(table_name) = 'EMPLOYEES';
IF v_iTableCount > 0 THEN
v_sQuery := 'DROP TABLE EX.EMPLOYEES';
EXECUTE IMMEDIATE v_sQuery;
END IF;
v_sQuery := ' CREATE TABLE ex.employees (
empID NUMBER,
firstName VARCHAR(255),
lastName VARCHAR(255),
deptID NUMBER
)
COMPRESS
NOLOGGING';
EXECUTE IMMEDIATE v_sQuery;
END;
#
DECLARE
v_sQuery VARCHAR2(1000);
v_iTableCount INTEGER;
BEGIN
SELECT COUNT(1)
INTO v_iTableCount
FROM all_tables
WHERE UPPER(owner) = 'EX'
AND UPPER(table_name) = 'DEPARTMENTS';
IF v_iTableCount > 0 THEN
v_sQuery := 'DROP TABLE EX.DEPARTMENTS';
EXECUTE IMMEDIATE v_sQuery;
END IF;
v_sQuery := ' CREATE TABLE ex.departments (
deptID NUMBER,
deptDesc VARCHAR(255)
)
COMPRESS
NOLOGGING';
EXECUTE IMMEDIATE v_sQuery;
END;
Code block that parses Setup.sql:
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
/*Separating using # because ; is used within the queries now*/
asQueries = ReadFile("Setup.sql").ToString.Split("#")
For Each sQuery In asQueries
If sQuery.Trim <> "" Then
command.CommandText = sQuery.Trim
command.CommandType = CommandType.Text
command.Connection = conn
command.ExecuteNonQuery()
End If
Next
However, now I am receiving the following Exception:
ORA-06550: line 1, column 8:
PLS-00103: Encountered the symbol "" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The symbol "" was ignored.
ORA-06550: line 2, column 27:
PLS-00103: Encountered the symbol "" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The weird thing is that if I run the query directly in PL/SQL developer, it runs fine. I was wondering if it was because of the quotation marks that holds the query string but that should have affected the earlier version of Setup.sql as well. Is it a weirdness in Oracle Data Access Client or am I making a rookie mistake somewhere that I can't see?
Thanks
UPDATE:
I tried the suggestions regarding replacing carriage return & line feed with just carriage returns from this post but that did not help either.
I tried using vbCrLf
and vbLf
thinking maybe that would be better but that did not help as well. Considering the following quote from the post, I am not surprised:
While I haven't been able to find any definitive documentation, empirical testing shows that Oracle will accept a CR as line break but not necessarily a CR/LF pair.
However, I am hoping someone here is going to have an answer.
SOLUTION:
Spaces & Tabs were most likely the issue because I changed the code to following and the issue went away. Thought I would update the post so that if anyone who has similar issues in the future can try these things in their cases & hopefully arrive at the solution quicker.
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Dim asQueries As Array
Dim sQuery As String
Dim rgxObj As RegularExpressions.Regex
/*Separating using # because ; is used within the queries now*/
asQueries = ReadFile("Setup.sql").ToString.Split("#")
For Each sQuery In asQueries
If sQuery.Trim <> "" Then
sQuery = sQuery.Trim.Replace(vbCrLf, " ").Replace(Chr(9), " ")
rgxObj = New RegularExpressions.Regex("\s+")
sQuery = rgxObj.Replace(sQuery, " ").Replace(Chr(9), " ")
command.CommandText = sQuery
command.CommandType = CommandType.Text
command.Connection = conn
command.ExecuteNonQuery()
End If
Next