【发布时间】:2020-12-19 08:20:11
【问题描述】:
我有一个项目的需求,其中一些用户在表中插入一些表达式,稍后将添加到选择语句中以运行查询。在最终工具中运行查询本身之前,我需要使用一个函数来检查语句的有效性。基本上,我需要解析语句来检查表达式的有效性。数据库是 SQL Server 2014。
我知道如何使用这段代码在 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
如果可能的话,谁能告诉我如何在 Transact SQL 中做类似的事情?
感谢您的帮助
【问题讨论】:
-
SET PARSEONLY ON可能满足您的需求 - 请参阅stackoverflow.com/questions/13316328/…的答案
标签: sql-server oracle tsql plsql