PLSQL开发实现字符串拆分
在应用程序开发中,会出现单选或多选框条件输入的需求。如输入框的输入值为sz,或sz|nj|zj|nt,在SQL中会这样处理。 select * from tab_1 where col_1=sz ;这是单选框输入。 select * from tab_1 where col_1 =sz|nj ;这是多选框输入。 很明显,多选输入值不
在应用程序开发中,会出现单选或多选框条件输入的需求。如输入框的输入值为'sz',或'sz|nj|zj|nt',在SQL中会这样处理。select * from tab_1 where col_1='sz' ;这是单选框输入。
select * from tab_1 where col_1 ='sz|nj' ;这是多选框输入。
很明显,多选输入值不会查询出结果。
-
如何解决这个问题?
有使用动态SQL实现的方法,如拼装成这样的SQL语句:select * from tab_1 where col_1 in ('sz','nj') ;
还有将'sz|nj'拆分插入到临时表中,再关联该临时表实现,如select * from tab_1 where col_1 in (select a from tt);
临时表涉及到表的创建和维护,还有IO。
我最近想到一个方法:将传入的字符串以嵌套表类型返回,使用表函数调用,实现这类需求。
函数代码如下:
1.create or replace function f_get_unitstring(p_str_all in varchar2,
2. p_str_gap in varchar2) 3. return t_ntb_allstring is 4. --create or replace type t_ntb_allstring is table of varchar2(20); 5. v_ntb_allstring t_ntb_allstring;
6.
7. str_unit varchar2(20);
8. str_char varchar2(1);
9.
10. i_str_length number;
11. i_str_index number;
12.
13.begin 14. /*p_str_city:='nj~wx~sz~cz~zj~nt~yc~';*/ 15. --p_str_all := '1|2|3|'; 16. --p_str_gap := '|'; 17.
18. v_ntb_allstring := t_ntb_allstring();
19.
20. i_str_length := length(p_str_all);
21.
22. i_str_index := 1;
23.
24. while (i_str_index 25. str_char := substr(p_str_all, i_str_index, 1);
26.
27. if (str_char = p_str_gap) then 28.
29. if (str_unit is not null) then 30. v_ntb_allstring.extend(1);
31. v_ntb_allstring(v_ntb_allstring.count) := str_unit; 32. str_unit := null; 33. end if; 34.
35. else 36. str_unit := str_unit || str_char;
37.
38. if (i_str_index = i_str_length) then 39. v_ntb_allstring.extend(1);
40. v_ntb_allstring(v_ntb_allstring.count) := str_unit; 41. str_unit := ''; 42. end if; 43.
44. end if; 45.
46. i_str_index := i_str_index + 1;
47. end loop; 48.
49. return(v_ntb_allstring); 50.end; 测试如下:
1.SQL> select * from table(f_get_unitstring('1aa|2cc|3bb','|')); 2.
3.COLUMN_VALUE
4.-------------------- 5.1aa
6.2cc
7.3bb
8.
9.SQL>
以上解决方法仅供参数,欢迎交流。
再增加一种方法。
使用pipelined函数也能实现这个需求。但在此处性能优势不会体现出来,如果您碰巧碰到大数据量如亿级别的字符串拆分,该方法就能派上用场。
代码如下:
1.create or replace function f_get_unitstring(p_str_all in varchar2,
2. p_str_gap in varchar2) 3. return t_ntb_allstring 4. pipelined is 5. --create or replace type t_ntb_allstring is table of varchar2(20); 6. v_ntb_allstring t_ntb_allstring;
7. str_unit varchar2(20);
8. str_char varchar2(1);
9. i_str_length number;
10. i_str_index number;
11.begin 12. v_ntb_allstring := t_ntb_allstring();
13. i_str_length := length(p_str_all);
14. i_str_index := 1;
15. while (i_str_index 16. str_char := substr(p_str_all, i_str_index, 1);
17. if (str_char = p_str_gap) then 18. if (str_unit is not null) then 19. -- v_ntb_allstring.extend(1); 20. -- v_ntb_allstring(v_ntb_allstring.count) := str_unit; 21. pipe row(str_unit);
22. str_unit := null; 23. end if; 24. else 25. str_unit := str_unit || str_char;
26. if (str_unit is not null) then 27. -- v_ntb_allstring.extend(1); 28. -- v_ntb_allstring(v_ntb_allstring.count) := str_unit; 29. pipe row(str_unit);
30. str_unit := null; 31. end if; 32. end if; 33. i_str_index := i_str_index + 1;
34. end loop; 35. --return(v_ntb_allstring); 36.end;

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment