How do I pass values to the in condition in the stored procedure? (To find the processing of a corresponding record based on multiple IDs)

Source: Internet
Author: User
Tags rtrim

Read the article first.

http://www.itpub.net/viewthread.php?tid=1441454&pid=17785072&page=1&extra=page%3D1#pid17785072

Then look at the following processing: This is mainly to show a use of the storage function, for example, the parameter is a string of comma-separated IDs, "123,124,125,126", get the corresponding 4 records, first write a storage function, to resolve the ID string:
SQL codeCreateor replacefunctionStr2varlist (p_string in VARCHAR2) returnVartabletype asV_str Longdefaultp_string | |     ',';     V_n VARCHAR2 (2000); V_data Vartabletype: = Vartabletype ();beginLoop v_n: =instr (V_str, ', '); Exit when(NVL (v_n,0) = 0);        V_data.extend;        V_data (v_data.count): = LTrim (RTrim (substr (v_str,1,v_n-1))); V_STR: = substr (V_str, v_n+1); EndLoop returnV_data; End;

Create or Replace function str2varlist (p_string in varchar2) return Vartabletype
 as
 v_str long default p_string || ',';
 V_n varchar2 ();
 V_data Vartabletype: = Vartabletype ();
 Begin
 	Loop
 		v_n: =instr (V_str, ', ');
	Exit when (NVL (v_n,0) = 0);
	V_data.extend;
 	V_data (v_data.count): = LTrim (RTrim (substr (v_str,1,v_n-1)));
	V_STR: = substr (V_str, v_n+1);
 	End Loop;
 	return v_data;
 End

The query statement then writes:
SQL code SELECTId name, Birthday, Address from Table(Str2varlist (' 123,124,125,126 ')) T, T_user E WHEREE.id = T.column_value
SELECT ID, name, birthday, address from 
  table (str2varlist (' 123,124,125,126 ')) T, T_user e 
  WHERE e.id = T.column_v Alue

You can also write this:
SQL code SELECTId name, Birthday, Address fromT_user E WHEREID in ( Select* fromThe ( SelectCast (Str2varlist (#ids #) asVartabletype) fromDual))
Select ID, name, birthday, address from 
  t_user e 
  WHERE ID in (SELECT * from "(#ids #) as Vartabletype) from dual))

When dealing with this situation, we typically spell the ID in a section of SQL, such as: ID in (123, 124, 125), which solves the problem, but is not very secure, and can cause problems if there is SQL injection.

CREATE OR REPLACE TYPE Numtabletype as TABLE of number; /CREATE OR REPLACE TYPE Vartabletype as TABLE of VARCHAR2 (1000); /CREATE OR REPLACE FUNCTION str2numlist (p_string in VARCHAR2) return Numtabletype as V_str LONG DEFAULT p_string | | ','; V_n number; V_data Numtabletype: = Numtabletype (); BEGIN LOOP V_n: = To_number (INSTR (V_str, ', ')); EXIT When (NVL (v_n, 0) = 0); V_data. EXTEND; V_data (V_data. COUNT): = LTRIM (RTRIM (SUBSTR (v_str, 1, v_n-1)); V_STR: = SUBSTR (V_str, v_n + 1); End LOOP; return v_data; End; /CREATE OR REPLACE FUNCTION str2varlist (p_string in VARCHAR2) return Vartabletype as V_str LONG DEFAULT p_string | | ','; V_n VARCHAR2 (2000); V_data Vartabletype: = Vartabletype (); BEGIN LOOP V_n: = INSTR (V_str, ', '); EXIT When (NVL (v_n, 0) = 0); V_data. EXTEND; V_data (V_data. COUNT): = LTRIM (RTRIM (SUBSTR (v_str, 1, v_n-1)); V_STR: = SUBSTR (V_str, v_n + 1); End LOOP; return v_data; End; /SELECT E.userid, e.username from TABLE (str2varlist (' 1,124,125,126 ')) T, T_user E WHERE E.userid = T.column_value;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.