Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / General DB Topics / General DB Topics / September 2004

Tip: Looking for answers? Try searching our database.

Any ideas on how to write this stored procedure..Oracle

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sashikiran Rachakonda - 21 Sep 2004 21:45 GMT
Just wondering if you have done this before in some
stored procedure...

Lets say we have customer table

CustID            CustName

12          Sundeep
32          Ravi
43          Sashi
54          Mallik
45          Lakshman
65          Jack
76          Sam

Lets say i give  a VarChar "12,32,43" to a stored
Procedure...it should return me a ref cursor which
contains 2 fields the CustID and CustName based on the
VarChar input i passed...so in this case it is going
to be

12          Sundeep
32          Ravi
43          Sashi

--Sashi.
Ed prochak - 22 Sep 2004 18:40 GMT
> Just wondering if you have done this before in some
> stored procedure...
[quoted text clipped - 22 lines]
>
> --Sashi.

Do a google search in comp.databases.oracle.misc for dynamic SQL and ref cursor.

In general ORACLE questions will find better answers in the four oracle groups.

-- Ed
code_wrong - 22 Sep 2004 23:18 GMT
> Just wondering if you have done this before in some
> stored procedure...
[quoted text clipped - 20 lines]
> 32          Ravi
> 43          Sashi

something like this maybe:
CREATE OR REPLACE PROCEDURE customer_by_id
                                             (id_passed IN
supplier.address%TYPE)IS
BEGIN
            SELECT * FROM customer WHERE address LIKE id_passed;
END;
code_wrong - 23 Sep 2004 14:09 GMT
> > Just wondering if you have done this before in some
> > stored procedure...
[quoted text clipped - 28 lines]
>              SELECT * FROM customer WHERE address LIKE id_passed;
> END;

ooops I meant:

CREATE OR REPLACE PROCEDURE customer_by_id
                                              (id_passed IN
customer.CustID%TYPE)IS
BEGIN
             SELECT * FROM customer WHERE CustID LIKE id_passed;
END;

hmmmmm
--CELKO-- - 23 Sep 2004 03:14 GMT
This is a VERY poor way to write SQL.  You can use Dynamic SQL or the
stinking kludge I will post shortly.  They are dangerous and slow.  It
shows you are still thinking in temrs of BASIC and SQL.

The right way is to load a one column table and use "WHERE cust_id IN
(SELECT cust_id FROM Parmlist)" in your code.

Passing a list of parmeters to a stored procedure can be done by
putting them into a string with a separator.  I like to use the
traditional comma.  Let's assume that you have a whole table full of
such parameer lists:

CREATE TABLE InputStrings
(keycol CHAR(10) NOT NULL PRIMARY KEY,
input_string VARCHAR(255) NOT NULL);

INSERT INTO InputStrings VALUES ('first', '12,34,567,896');
INSERT INTO InputStrings VALUES ('second', '312,534,997,896');
...

This will be the table that gets the outputs, in the form of the
original key column and one parameter per row.

CREATE TABLE Parmlist
(keycol CHAR(5) NOT NULL PRIMARY KEY,
parm INTEGER NOT NULL);

It makes life easier if the lists in the input strings start and end
with a comma.  You will need a talbe of sequential numbers -- a
standard SQL programming trick,  Now, the real query:

INSERT INTO ParmList (keycol, parmlist)
SELECT keycol,  
      CAST (SUBSTRING (I1.input_string
                       FROM S1.seq +1
                        FOR MIN(S2.seq) - S1.seq -1)
        AS INTEGER)
 FROM InputStrings AS I1 ,  Sequence AS S1 , Sequence AS S2
WHERE SUBSTRING (‘,' || I1.input_string || ‘,' FROM S1.seq FOR 1) =
‘,'
  AND SUBSTRING (‘,' || I1.input_string || ‘,' FROM S2.seq FOR 1) =
‘,'
  AND S1.seq < S2.seq
GROUP BY I1.keycol, I1.input_string, S1.seq;

The S1 and S2 copies of Sequence are used to locate bracketing pairs
of commas, and the entire set of substrings located between them is
extracts and cast as integers in one non-procedural step.  The trick
is to be sure that the right hand comma of the bracketing pair is the
closest one to the first comma.

You can then write:

SELECT *
  FROM Foobar
 WHERE x IN (SELECT parm FROM Parmlist WHERE key_col = :something);
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.