文档课题:增加表字段且按顺序数字填充.标签:sort,填充,段且,30,VARCHAR2,表字,NUMBER,LOGOFF,255 From: https://blog.51cto.com/u_12991611/5795128
数据库:oracle 11.2.0.4 64位
应用场景:表与表之间连接时,很多时候需要增加一个不重复的数值字段作为连接条件.
以下测试过程.
SQL> desc aud$_ts;
Name Type Nullable Default Comments
--------------- -------------- -------- ------- --------
SESSIONID NUMBER
ENTRYID NUMBER
STATEMENT NUMBER
TIMESTAMP# DATE Y
USERID VARCHAR2(30) Y
USERHOST VARCHAR2(128) Y
TERMINAL VARCHAR2(255) Y
ACTION# NUMBER
RETURNCODE NUMBER
OBJ$CREATOR VARCHAR2(30) Y
OBJ$NAME VARCHAR2(128) Y
AUTH$PRIVILEGES VARCHAR2(16) Y
AUTH$GRANTEE VARCHAR2(30) Y
NEW$OWNER VARCHAR2(30) Y
NEW$NAME VARCHAR2(128) Y
SES$ACTIONS VARCHAR2(19) Y
SES$TID NUMBER Y
LOGOFF$LREAD NUMBER Y
LOGOFF$PREAD NUMBER Y
LOGOFF$LWRITE NUMBER Y
LOGOFF$DEAD NUMBER Y
LOGOFF$TIME DATE Y
COMMENT$TEXT VARCHAR2(4000) Y
CLIENTID VARCHAR2(64) Y
SPARE1 VARCHAR2(255) Y
SPARE2 NUMBER Y
OBJ$LABEL RAW(255) Y
SES$LABEL RAW(255) Y
PRIV$USED NUMBER Y
SESSIONCPU NUMBER Y
NTIMESTAMP# TIMESTAMP(6) Y
PROXY$SID NUMBER Y
USER$GUID VARCHAR2(32) Y
INSTANCE# NUMBER Y
PROCESS# VARCHAR2(16) Y
XID RAW(8) Y
AUDITID VARCHAR2(64) Y
SCN NUMBER Y
DBID NUMBER Y
SQLBIND CLOB Y
SQLTEXT CLOB Y
OBJ$EDITION VARCHAR2(30) Y
SQL> create table sort as select * from aud$_ts where rownum<101;
Table created.
Elapsed: 00:00:00.16
SQL> alter table sort add idnum integer;
Table altered.
--按sessionid排序,将idnum字段用数值填充.
merge into sort t
using (select rowid as rid, row_number() over (order by sessionid) as rn from sort) s
on (s.rid = t.rowid)
when matched then update set idnum = rn;
说明:以上测试填充的是不重复的数字,可以满足笔者需求,至于要按顺序填充数字后续在更新.