定位到 return 到变量 SQL DB2

Getting LOCATE to return to variable SQL DB2

是否可以使用

 CREATE PROCEDURE TEST(IN QUERYSTRING VARCHAR(128))
 LANGUAGE SQL
 BEGIN
 DECLARE switch integer default 0;
 set switch = LOCATE('select', querystring,1);
 IF switch <> 0 then
 .......
 ELSE
 .......
 END IF;
 END@

查询字符串可以是 'select count(*) from testtag' 或 'insert into testtab'

我是否可以这样做以及如何将 LOCATE return 值放入变量中有什么想法吗?

Windows DB2 10.5

谢谢

这是一个示例,比较它在您的环境中的输出:

--#SET TERMINATOR @
set serveroutput on@
update command options using s on@

CREATE or replace PROCEDURE locateTEST(IN QUERYSTRING VARCHAR(128))
 LANGUAGE SQL
 specific locatetest
 BEGIN
     DECLARE switch integer default 0;
     set switch = LOCATE('select', querystring,1);
     IF switch <> 0 then
       call dbms_output.put_line('Switch: '||varchar(switch));
    ELSE
       call dbms_output.put_line('Switch: 0');
    END IF;
END@

call locatetest('    insert into fred(mycol) values(1)')@
call locatetest('    select count(*) from testtag')@

如果从 Db2 命令行(例如,在 windows 上从 db2cmd.exe window 上执行,并且已经存在到数据库的连接,则上面给出以下输出:

set serveroutput on
DB20000I  The SET SERVEROUTPUT command completed successfully.

update command options using s on
DB20000I  The UPDATE COMMAND OPTIONS command completed successfully.

CREATE or replace PROCEDURE locateTEST(IN QUERYSTRING VARCHAR(128))
 LANGUAGE SQL
 specific locatetest
 BEGIN
     DECLARE switch integer default 0;
     set switch = LOCATE('select', querystring,1);
     IF switch <> 0 then
       call dbms_output.put_line('Switch: '||varchar(switch));
    ELSE
       call dbms_output.put_line('Switch: 0');
    END IF;
END
DB20000I  The SQL command completed successfully.

call locatetest('    insert into fred(mycol) values(1)')

  Return Status = 0

Switch: 0

call locatetest('    select count(*) from testtag')

  Return Status = 0

Switch: 5