| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.server -> DBA requires basic SQL assistance please (LONG POST)
Afternon all,
I'm certain I've done this before, but try as I might, I cannot seems to make it work now ! What I think I've done before is this :
select * from table where something in (some_function());
but, the bit in parenthesis is generated as output from a function either via an OUT parameter or by a direct function result. So the function returns <'a','b','c'> and the in clause 'just worked'. Or at least, that's what I think happened.
Here's where I'm losing the plot :
I have a table in the database holding employee names as a single column, in the format first_name || space || last_name. I have soundexed every word (separated by space or hyphen) in the name column into a separate table which holds a soundex code and the employee id. So my own name 'Norman Dunbar' has two rows in the soundex table - 'N655' and 'D516', both of which point back at my row in the employee table.
When I search for an employee by name or part therof, I can find my row using soundex('Norman') or soundex('Dunbar') - which is the intention I have for the app. However, my problem is based on the waffle at the top of this post - I cannot get the 'select ... from ... where ... in()' to work.
I have a function BuildSoundex which returns the count of the number of soundex codes generated for a passed in varchar2 name, and an output paramater which returns the list formatted with quotes and commas as appropriate :
CREATE OR REPLACE FUNCTION Buildsoundex(iString IN VARCHAR2,
oSoundex OUT VARCHAR2,
iWithCommas IN BOOLEAN DEFAULT
FALSE,
iWithQuotes IN BOOLEAN DEFAULT
FALSE)
--
MySoundex := MySoundex || ThisSoundex;
IF MyComma THEN
MySoundex := MySoundex || ',';
END IF;
END IF;
MyStart := MySpace + 1;
MySpace := INSTR(MyString, ' ', MyStart);
END LOOP;
-- If we are adding commas, then we have one at the end and we
don't want it
IF iWithCommas THEN
MySoundex := SUBSTR(MySoundex, 1, LENGTH(MySoundex) -1 );
END IF;
oSoundex := MySoundex;
RETURN MyCount;
DECLARE
MySoundex VARCHAR2(20); -- Because I know I won't generate a bigger
than 20 chare return.
MyCount INTEGER := 0;
BEGIN
MyCount := BuildSoundex('Norman Dunbar', MySoundex, TRUE, TRUE);
DBMS_OUTPUT.PUT_LINE('MyCount := ' || TO_CHAR(MyCount));
DBMS_OUTPUT.PUT_LINE('MySoundex := ' || MySoundex);
END;
The dbms_output buffer looks like the following :
MyCount := 2
MySoundex := 'N655','D516'
Now, what I can do this :
SELECT Employee_name
FROM employee
WHERE employee_id IN (SELECT esx_employee_id
FROM employee_soundex
WHERE esx_soundex IN ('N655','D516')
);
Which works fine with the soundex codes hard coded in, but I really really want to do this instead :
BEGIN
MyCount := BuildSoundex('Norman Dunbar', MySoundex, TRUE, TRUE);
FOR e IN (SELECT Employee_name
FROM employee
WHERE employee_id IN (SELECT esx_employee_id
FROM employee_soundex
WHERE esx_soundex IN (:MySoundex)
)
)
LOOP
DBMS_OUTPUT.PUT_LINE(e.employee_name);
END LOOP;
Hopefully the wrapping, fonts etc won't get screwed up too much.
Cheers for any comments, put downs, advice, etc - be brutal, I can take it :o)
Regards,
Norman.
Tel: 0113 289 6265 Fax: 0113 289 3146 URL: http://www.Lynx-FS.com -------------------------------------Received on Mon Mar 10 2003 - 10:17:22 CST
![]() |
![]() |