I have a requirement in a project where some users insert some expressions in a table that later on will be added to a select statement for running a query. Before running the query itself in the final tool, I need to use a function to check the validity of the statement. Basically, I need to parse the statement to check the validity of the expression. The database is SQL Server 2014.
I know how to do it in Oracle using this small piece of code:
Oracle
create or replace function check_sql( p_sql in CLOB ) return boolean
authid current_user
is
v_cur int;
begin
v_cur:= dbms_sql.open_cursor();
dbms_sql.parse(v_cur,p_sql,dbms_sql.native);
dbms_sql.close_cursor(v_cur);
return true;
exception
when others then
return false;
end;
/
SQL> set serveroutput on size unlimited
SQL> declare
v_result varchar2(20);
is_ok boolean;
begin
is_ok := check_sql ( 'select 1 from dual' ) ;
if is_ok
then
v_result := 'OK' ;
else
v_result := 'ERROR' ;
end if;
dbms_output.put_line(v_result);
end;
/
OK
SQL> set serveroutput on size unlimited
SQL> declare
v_result varchar2(20);
is_ok boolean;
begin
is_ok := check_sql ( 'select 1 from duala' ) ;
if is_ok
then
v_result := 'OK' ;
else
v_result := 'ERROR' ;
end if;
dbms_output.put_line(v_result);
end;
/
ERROR
Can anyone tell me how to do something similar, if possible, in Transact SQL?
Thank you for your help

SET PARSEONLY ONprobably does what you need - see answers to stackoverflow.com/questions/13316328/…