create or replace procedure p_test
(
id number) is
begin
update test set edit='true'
where id in (id);
commit;
end p_test;
在程序中生成参数ID的值,
当ID的值为:只有一个数字时,能正确执行。
当ID的值为:1,2,33 即多于一个数字时,就报错:无效数字。直接把数字写进去:
update test set edit='true'
where id in (1,2,33);运行后是正确的,这是怎么会事啊?
(
id number) is
begin
update test set edit='true'
where id in (id);
commit;
end p_test;
在程序中生成参数ID的值,
当ID的值为:只有一个数字时,能正确执行。
当ID的值为:1,2,33 即多于一个数字时,就报错:无效数字。直接把数字写进去:
update test set edit='true'
where id in (1,2,33);运行后是正确的,这是怎么会事啊?
上面写错了:
把上面的id number改为:id varchar2
(
id number) is
begin
update test set edit='true'
where id in (id);
commit;
end p_test;Oracle 中有 charindex 吗?可以用MS SQL 中的 Charindex 处理.
(
id varchar2
) is
begin
execute immediate 'update test set edit=''true'' where id in (' || id || ')';
commit;
end p_test;但参数也得这样输入:'1,2,3'
(
id varchar2) is
begin
update test set edit='true'
where id in (id);
commit;
end p_test;用上面的过程,下面的三种方法全报:无效的数字的错
call test('to_Number(1),to_Number(2)')call test('to_char(1),to_char(2)')call test('"1","2"')
--------------------------------------------------------------------------------最终测试正确的如下所示:
call test('1,2')
create or replace procedure p_test
(
id varchar2
) is
begin
execute immediate 'update test set edit='''||'true'||''' where id in (' || id || ')';
commit;
end p_test;
不知道edit='''||'true'||'''为什么要这样写,写成edit="true",就报true是无效的错误.
这些都是为什么?
在字符串中两个''代表一个'
否则就会报错
execute immediate 'update test set edit=''true'' where id in (' || id || ')';
起到的作用就是把'update test set edit=''true'' where id in (' || id || ')'字符串当作sql命令来运行,你把id放进来看看是不是正好呢?
create or replace procedure p_test
(
id varchar2) is
beginexecute immediate 'update test set edit=''true'' where id in (' || id || ')';commit;
end p_test;