You Asked
Can I validate SQL syntax without having the objects created in database?
I have a free text where users are supposed to enter valid SQL text. I have to determine which are valid and which are invalid entries.
create table metadata (data_src_name varchar2(30), column_name varchar2(30), data_type varchar2(30));
insert into metadata (data_src_name, column_name, data_type) values ('tab1', 'col1', 'number');
insert into metadata (data_src_name, column_name, data_type) values ('tab1', 'col2', 'varhar2(100)');
insert into metadata (data_src_name, column_name, data_type) values ('tab1', 'col3', 'varhar2(200)');
insert into metadata (data_src_name, column_name, data_type) values ('tab1', 'col4', 'date');
create table t (id number, data_src_name varchar2(30), filter_condition varchar2(4000), is_valid varchar(10));
insert into t (id, data_src_name, filter_condition, is_valid) values (1, 'tab1', 'col1 > 100 and col2 = ''abc''', NULL);
insert into t (id, data_src_name, filter_condition, is_valid) values (2, 'tab1', 'col2 = ''abc'' and col2 = ''xyz''', NULL);
insert into t (id, data_src_name, filter_condition, is_valid) values (3, 'tab1', 'col1 = ''abc''', NULL);
insert into t (id, data_src_name, filter_condition, is_valid) values (4, 'tab2', 'col1 = 90', NULL);
Here id=1 will be translated as : SELECT 1 FROM tab1 WHERE col1 > 100 and col2 = 'abc';
id=2 will be translated as : SELECT 1 FROM tab1 WHERE col2 = 'abc' and col2 = 'xyz';
Desired output:
select id, is_valid from t;
id is_valid
--- ---------
1 Yes
2 Yes
3 No
4 No
Reason for is_valid failure-
3. col1 is numeric
4. col1 does not exist in tab2. Possible combinations are defined in "metdata" table.
Is it possible without tab1 and tab2 actually existing in the schema? Only metadata information is available to me.
I was trying to use dbms_sql.parse. Any pointers to this will be really helpful
and Connor said...
An easy way would be to try open a ref cursor on your SQL
SQL> declare
2 rc sys_refcursor;
3 begin
4 open rc for 'select * from dual';
5 close rc;
6 end;
7 /
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL> declare
2 rc sys_refcursor;
3 begin
4 open rc for 'nonsense';
5 close rc;
6 end;
7 /
declare
*
ERROR at line 1:
ORA-00900: invalid SQL statement
ORA-06512: at line 4
SQL>
SQL> declare
2 rc sys_refcursor;
3 begin
4 open rc for 'select * from missing_table';
5 close rc;
6 end;
7 /
declare
*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at line 4
SQL>
SQL>
or just try get an execution plan
SQL> exec execute immediate 'explain plan for '|| 'select * from dual';
PL/SQL procedure successfully completed.
SQL> exec execute immediate 'explain plan for '|| 'nonsense';
BEGIN execute immediate 'explain plan for '|| 'nonsense'; END;
*
ERROR at line 1:
ORA-00905: missing keyword
ORA-06512: at line 1
SQL> exec execute immediate 'explain plan for '|| 'select * from missing_table';
BEGIN execute immediate 'explain plan for '|| 'select * from missing_table'; END;
*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at line 1
Rating
(1 rating)
Is this answer out of date? If it is, please let us know via a Comment