传递参数为in('','','') 不定数据时
2010-01-30 16:50
274 查看
Thomas -- Thanks for the question regarding "How can I do a variable "in
list"", version 8.1.5
Submitted on 2-May-2000 15:57 Central time zoneTom's latest followup |
Bookmark | BottomLast updated 15-Jul-2009 16:57
You Asked
I have a simple stored procedure, that I would like to have a passed in
string(varchar2)
for used in select from where col1 in (var1) in a stored
procedure. I've tried
everything but doesn't work. Followed is my proc.
Thanks
CREATE OR REPLACE PROCEDURE WSREVSECT_5
pSectNos varchar2,
pRetCode OUT varchar2
)
AS
nCount number;
BEGIN
SELECT count(fksrev) into nCount
FROM SREVSECT
WHERE sectno IN (pSectNos ) /* as in 'abc', 'xyz', '012' */
;
pRetCode:=to_char(ncount);
End;
and we said...
it works -- the above is the same as
where sectno = pSectNos
though, not what you want. You want it to be:
where sectno in ( 'abc', 'xyz', '012' )
NOT:
where sectno in ( '''abc'', ''xyz'', ''012''' )
which is effectively is (else you could never search on a string with
commas and quotes
and so on -- it is doing the only logical thing right now).
You can do this:
SQL> create or replace type myTableType as table
of varchar2 (255);
2 /
Type created.
ops$tkyte@dev8i> create or replace
function in_list( p_string in varchar2 ) return myTableType
2 as
3 l_string long default p_string || ',';
4 l_data myTableType := myTableType();
5 n number;
6 begin
7 loop
8 exit when l_string is null;
9 n := instr( l_string, ',' );
10 l_data.extend;
11 l_data(l_data.count) :=
ltrim( rtrim( substr( l_string, 1, n-1 ) ) );
12 l_string := substr( l_string, n+1 );
13 end loop;
14
15 return l_data;
16 end;
17 /
Function created.
ops$tkyte@dev8i> select *
2 from THE
( select cast( in_list('abc, xyz, 012') as
mytableType ) from dual ) a
3 /
COLUMN_VALUE
------------------------
abc
xyz
012
ops$tkyte@dev8i> select * from all_users where username in
2 ( select *
3 from THE ( select cast( in_list('OPS$TKYTE, SYS, SYSTEM')
as mytableType ) from dual ) )
4 /
USERNAME USER_ID CREATED
------------------------------ ---------- ---------
OPS$TKYTE 23761 02-MAY-00
SYS 0 20-APR-99
SYSTEM 5 20-APR-99
list"", version 8.1.5
Submitted on 2-May-2000 15:57 Central time zoneTom's latest followup |
Bookmark | BottomLast updated 15-Jul-2009 16:57
You Asked
I have a simple stored procedure, that I would like to have a passed in
string(varchar2)
for used in select from where col1 in (var1) in a stored
procedure. I've tried
everything but doesn't work. Followed is my proc.
Thanks
CREATE OR REPLACE PROCEDURE WSREVSECT_5
pSectNos varchar2,
pRetCode OUT varchar2
)
AS
nCount number;
BEGIN
SELECT count(fksrev) into nCount
FROM SREVSECT
WHERE sectno IN (pSectNos ) /* as in 'abc', 'xyz', '012' */
;
pRetCode:=to_char(ncount);
End;
and we said...
it works -- the above is the same as
where sectno = pSectNos
though, not what you want. You want it to be:
where sectno in ( 'abc', 'xyz', '012' )
NOT:
where sectno in ( '''abc'', ''xyz'', ''012''' )
which is effectively is (else you could never search on a string with
commas and quotes
and so on -- it is doing the only logical thing right now).
You can do this:
SQL> create or replace type myTableType as table
of varchar2 (255);
2 /
Type created.
ops$tkyte@dev8i> create or replace
function in_list( p_string in varchar2 ) return myTableType
2 as
3 l_string long default p_string || ',';
4 l_data myTableType := myTableType();
5 n number;
6 begin
7 loop
8 exit when l_string is null;
9 n := instr( l_string, ',' );
10 l_data.extend;
11 l_data(l_data.count) :=
ltrim( rtrim( substr( l_string, 1, n-1 ) ) );
12 l_string := substr( l_string, n+1 );
13 end loop;
14
15 return l_data;
16 end;
17 /
Function created.
ops$tkyte@dev8i> select *
2 from THE
( select cast( in_list('abc, xyz, 012') as
mytableType ) from dual ) a
3 /
COLUMN_VALUE
------------------------
abc
xyz
012
ops$tkyte@dev8i> select * from all_users where username in
2 ( select *
3 from THE ( select cast( in_list('OPS$TKYTE, SYS, SYSTEM')
as mytableType ) from dual ) )
4 /
USERNAME USER_ID CREATED
------------------------------ ---------- ---------
OPS$TKYTE 23761 02-MAY-00
SYS 0 20-APR-99
SYSTEM 5 20-APR-99
相关文章推荐
- Mybatis 对单独传入参数string进行判断时报There is no getter for property named '**' in 'class java.lang.String解决办法
- [生产环境数据恢复]innobackupex: fatal error: OR no 'datadir' option in group 'mysqld' in MySQL options
- 关于mybatis的传递参数报错Type handler was null on parameter mapping for property 'userId'.
- 用"堆栈区数据复制"理解Java赋值和参数传递机制的心得
- [MySQL生产环境数据恢复]innobackupex: fatal error: OR no 'datadir' option in group 'mysqld' in MySQL options
- MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it is already used by statement which invoked this
- Linux C 多线程【3】--传递参数&共享进程数据
- [生产环境数据恢复]innobackupex: fatal error: OR no 'datadir' option in group 'mysqld' in MySQL options
- Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。
- [iOS]将含有NSData数据的数组转化为json字符串报错:reason: 'Invalid type in JSON write (NSConcreteMutableData)
- 查询结果按照 'in' 里面的数据排序:order by decode
- 一 uboot传递参数'console=ttyXXX'的作用
- 为什么二进制文件与文本文件存入同样的数据,文件大小差异会这么大?(from <<Thinking in C++>>'s execise)
- Android Post参数提交 List<NameValuePair>,web端获取数据
- Android C++向java传递不定长且不同类型的参数
- A - Who's in the Middle
- [2388]Who's in the Middle(水题)
- hidden symbol `__fini_array_end' in xmlwf/.libs/xmlwf is referenced by DSO
- Android NDK: jni/Android.mk: Cannot find module with tag 'cocos2dx' in import path解决办法
- 'BAPI_QUOTATION_CREATEFROMDATA2'导入数据时,订单数量为0