开发者

incerement the values in Oracle insert statement (neagative numbers)

I have to insert data in a table. It has a colum whose values should negative numbers like -1,-2,-3..... so on upto -50.

I thought of using sequence but it wont accept the negative values.

Ho开发者_StackOverflow中文版w can i genarate i mean insert the values???


"I thought of using sequence but it wont accept the negative values."

Are you sure?

SQL> create table t1 (c1 number)
  2  /

Table created.

SQL> create sequence myseq increment by -1
  2  /

Sequence created.

SQL> insert into t1 values (myseq.nextval)
  2  /

1 row created.

SQL> r
  1* insert into t1 values (myseq.nextval)

1 row created.

SQL> r
  1* insert into t1 values (myseq.nextval)

1 row created.

SQL> select * from t1
  2  /

        C1
----------
        -1
        -2
        -3

SQL>

What version of the database are you using? This is from Oracle 11g R1, but I don't think it's a recent feature.


search for any table with minium recourd count you like, and then write:

use a select to generate the data:

select (rownum * -1) as negRowNum from whatevertableyoufound where rownum <= 50


Use 0 - rownum.

SELECT 0 - rownum
FROM   DUAL
CONNECT BY LEVEL <= 50;

0-ROWNUM
-1
-2
-3
-4
-5
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜