过程:testAlt_item(sAltItem in Alt_item.component%type,OAltItem out varchar2)如何在select中使用?
解决方案 »
- Ora11创建环境时产生的错误,求助
- ORACLE存储过程执行错误
- 请教一条SQL文
- 问下sqlldr的filler参数的使用
- qq好友表中如何存储?
- [安装]Oracle10.2在Red Hat Enterprise Linux4 Update4 上的详细安装文档
- 这语句该怎么写
- 一个高难度update 两表关联,多个更新,复杂的计算,是不是无高效之解???,高手请进!!!
- 创建表的问题
- 帮我看看这条语句是什么意思,尤其是哪个乘号?
- 关于触发器的问题,急!
- CREATE OR REPLACE PROCEDURE p_test(p_string in varchar2,cur_basic out cursor)是错的?
SQL> SELECT TRUNC(SYSDATE,'DD')-1 FROM DUAL;TRUNC(SYSD
----------
12-9月 -05TRUNC()是系统函数
select * from tablename where id=f_test('test');如果是select f_test from tablename就不行,会当成字符串输出如果是procedure,需要定义返回变量,p_test(in1,in2,out1,out2)
你是说把function_name(...) 当成字段吗?好像不行吧,我试过,返回值都是function_name(...) ,表tablename中的记录并没有查到啊。
测试,创建过程
CREATE OR REPLACE FUNCTION TEST
(
p_PARA1 NUMBER
)
RETURN VARCHAR2
IS
v_TIME_STR VARCHAR2(20);
BEGIN
SELECT TO_CHAR(SYSDATE-p_PARA1,'YYYY-MM-DD') INTO v_TIME_STR FROM DUAL;
RETURN v_TIME_STR;
END test;
执行:
SQL> SELECT TEST(1) FROM DUAL;TEST(1)
--------------------------------------------------------------------------------
2005-09-12SQL>
你和bobfang(匆匆过客) 用的都是 DUAL,实际上取的是函数的返回值。这个是可以直接取得的,就像我第一种写法中的where id=f_test('test')也可以写成where id=(select f_test('test') from dual)
你们的写法都是对的。我说的是把返回值当成字段来查,就像select a,b from tablename就不能写成select upper('a'),upper('b') from tablename一样,不能用select f_test from tablename来查询表中的记录不知道我说的是不是清楚。
create or replace function sitem(sAlt_item Alt_item.component%type) return varchar2 is
Result varchar2(200):='';
v_item alt_item.component%type;
v_rev alt_item.revision%type;
cursor c_altitem is select distinct alternative,revision from alt_item where component=sAlt_item;
begin
open c_altitem;
loop
fetch c_altitem into v_item,v_rev ;
if c_altitem%rowcount > 0 then
Result:= Result ||v_item ||chr(91)||v_rev||'chr(93)';
else
Result:= ' ';
end if;
end loop;
close c_altitem;
return(Result);
end sitem;执行:
select SITEM('9366-2110-21-2B') from dual;会出错为什么?
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "GLOVIA_PROD52.SITEM", line 11
ORA-06512: at line 1申明一下Alt_item.component%type 是varchar2(30)类型
loop
fetch c_altitem into v_item,v_rev ;
if c_altitem%rowcount > 0 then
Result:= Result ||v_item ||chr(91)||v_rev||'chr(93)';
else
Result:= ' ';
end if;
end loop;
close c_altitem;
换成
for c_altitem in c_altitem loop ;
Result:= Result ||c_altitem .alternative||chr(91)||c_altitem .revision||'chr(93)';
end loop;
for c_altitem in c_altitem loop
Result:= Result ||c_altitem .alternative||chr(91)||c_altitem .revision||'chr(93)';
end loop;
create or replace function sitem(sAlt_item Alt_item.component%type) return varchar2 is
Result varchar2(200):='';
cursor c_altitem is select distinct alternative,revision from alt_item where component=sAlt_item;
begin
for c_altitem in c_altitem loop
Result:= Result ||c_altitem .alternative||chr(91)||c_altitem .revision||chr(93);
end loop;
return(Result);
end sitem;
越听越糊涂。: )
----------
1
2
1
2
3
1
1已选择7行。SQL> select upper('id') from b;UP
--
ID
ID
ID
ID
ID
ID
ID已选择7行。SQL>
用函数来查和上面的效果是一样的,表中的数据出不来,出的全是函数的返回值。这样应该够清楚了吧 :)
SQL> select id from test_b;I
-
1
2
1
2
3
1
1
已选择7行。
SQL> select upper(id) as id,upper('id') as "ID" from test_b;I ID
- --
1 ID
2 ID
1 ID
2 ID
3 ID
1 ID
1 ID已选择7行。
select * from tablename where id=f_test('test');
有些function以列名为参数,将每列的内容通过函数算出后返回
select f_test(a) from tablename
如果是procedure,需要定义返回变量
p_test(in1,in2,out1,out2)