`
ktnd
  • 浏览: 17298 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Generate a table with number sequence in sql

阅读更多

Generate a table with number sequence in sql
=========

 

1 Pre-defined collection types in Oracle
---------

select to_number(column_value) as num from
   table(sys.dbms_debug_vc2coll(1,2,3,4,5,6,7,8,9));

select to_number(column_value) as num from
   table(sys.KU$_VCNT(1,2,3,4,5,6,7,8,9));

select column_value as num from
   table(sys.KU$_OBJNUMSET(1,2,3,4,5,6,7,8,9));

 

2 Join: pure sql
---------

select a.i+b.i+c.i+d.i+1 as num
from
   (select 0 i from dual union all select 1 from dual) a,
   (select 0 i from dual union all select 2 from dual) b,
   (select 0 i from dual union all select 4 from dual) c,
   (select 0 i from dual union all select 8 from dual) d
order by num;

 

 

3 How to make number dynamic?
---------
?

 

3.1 Define function with collection type as return value

-----------------

It's not only sql.

And myaybe it's not correct or appropriate for dynamic.

However, I like it.

 

First, define a type:

 

create or replace type T_NUM_TAB as TABLE OF NUMBER;

 

Then, define the function:

 

CREATE OR REPLACE FUNCTION NUM_COLLECT (
       NUM_START IN NUMBER, 
       NUM_END IN NUMBER,
       NUM_STEP IN NUMBER DEFAULT 1)
  RETURN T_NUM_TAB
AS
  l_tab   T_NUM_TAB := T_NUM_TAB();
  l_idx   NUMBER;
BEGIN
  l_idx := NUM_START;
  WHILE l_idx <= NUM_END LOOP
    l_tab.extend;
    l_tab(l_tab.last) := l_idx;
    l_idx := l_idx + NUM_STEP;
  END LOOP;

  RETURN l_tab;
END;

 

Now, use it as follows:

 

SQL> select * from table(num_collect(2,10,2));

COLUMN_VALUE
------------
           2
           4
           6
           8
          10
 

 Is it a good idea to take sql string as parameter for the function?

 

分享到:
评论

相关推荐

    generate_sequence.rar_generate Sequence_generate_sequence_markov

    在这个"generate_sequence.rar"压缩包中,包含了四个MATLAB脚本文件,它们都是为了生成特定类型的马尔科夫序列。让我们逐一了解这些文件的功能: 1. **generate_sequence1.m**:这个脚本很可能是用来生成一阶...

    微软内部资料-SQL性能优化5

    Because a clustered index determines the sequence in which rows are stored in a table, there can only be one clustered index for a table at a time. Performance Considerations Keeping your clustered ...

    Red-Gate SQL Professional Toolbelt (part1)

    SQL Multi Script™ Unlimited: execute multiple scripts against multiple SQL Servers with a single click SQL Comparison SDK ™: automate functionality with our APIs SQL Object Level Recovery Native™:...

    [开发工具] SQL Toolbelt 1.8.2.238

    SQL Data Generator - generate test data for database tables, SQL Server. SQL Multi Script - allows you to quickly and easily create complex scripts for SQL Server. SQL Refactor - helps the user to ...

    Red-Gate SQL Professional Toolbelt (part3)

    SQL Multi Script™ Unlimited: execute multiple scripts against multiple SQL Servers with a single click SQL Comparison SDK ™: automate functionality with our APIs SQL Object Level Recovery Native™:...

    Agreement on Target-Bidirectional LSTMs for Sequence-to-Sequence Learning

    to generate unbalanced targets with good prefixes but bad suffixes, and thus performance suffers when dealing with long sequences. We propose a simple yet effective approach to overcome this ...

    MyBatisCodeHelper-Pro.zip

    Generate mybatis sql based on mybatis interface method name like spring data jpa, with this, you don't have to write most sql for non join query support generate statement with if test Database ...

    Combining implicit surfaces with soft blending in a CSG tree

    In this paper, we show an extension of the model proposed by Sabourdy, combining various implicit surfaces with soft blending capacities in a CSG tree....through a limited number of parameters.

    Beginning SQL Queries(Apress,2ed,2016)

    Get started on mastering... Knowing how to write good queries is the foundation for all work done in SQL, and it is a foundation that Clare Churcher's book, Beginning SQL Queries, 2nd Edition, lays well.

    Red-Gate SQL Professional Toolbelt (part2)

    SQL Multi Script™ Unlimited: execute multiple scripts against multiple SQL Servers with a single click SQL Comparison SDK ™: automate functionality with our APIs SQL Object Level Recovery Native™:...

    Red-Gate SQL Professional Toolbelt (part4)

    SQL Multi Script™ Unlimited: execute multiple scripts against multiple SQL Servers with a single click SQL Comparison SDK ™: automate functionality with our APIs SQL Object Level Recovery Native™:...

    WPF Data Binding with LINQ to SQL

    This is the final part of a three-part series on using LINQ to SQL: Part 1: Mapping Tables to Objects Part 2: Adding/Updating/Deleting Data Part 3: WPF Data Binding with LINQ to SQL These tutorials ...

    DevArt dbForge Studio for SQL Server Enterprise Edition 5.0.337

    Boost SQL coding in a convenient scripting environment Analyze and improve T-SQL code quality Design tables and rebuild them without losing data Compare databases, synchronize schemas and data Tackle ...

    mantis db_generate.sql

    db_generate.sql# phpMyAdmin MySQL-Dump # http://phpwizard.net/phpMyAdmin/ # # -------------------------------------------------------- # # Table structure for table 'mantis_bug_table' # DROP TABLE ...

    微软内部资料-SQL性能优化3

    Intent locks improve performance because SQL Server examines intent locks only at the table level to determine whether a transaction can safely acquire a lock on that table. This removes the ...

    MyBatis-Generate

    It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration ...

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Swap bit i and j in a 64 bit number Chapter 19. Reverse the order of bits in an unsigned integer Chapter 20. Convert an integer to a string and a string to an integer Chapter 21. Convert a number ...

Global site tag (gtag.js) - Google Analytics