Re: Automatic random number on row creation




"Kuon" <kuon@xxxxxxxxxx> a écrit dans le message de news: dp0uo3$1t3$1@xxxxxxxxxxxxxxxxxx
| Hello,
|
| I have a ticket system which need a random (act as a password) string
| generated. But this string also act as a key (need to be unique).
|
| The approach of the former dev does not suites me.
|
| He was generating a random string, doing a select key where key = ... To
| verify the uniqueness of the key.
|
| If the select returned nothing, he inserted the new row.
|
| Of course this is not good, for many reasons.
|
| I want to do it database side.
|
| I know something like that:
| select dbms_random.string('U', 20) str from dual
|
| for my random number.
|
| But how, when I do:
|
| insert into myTable (otherInfos) values ("myotherinfos"),
|
| how can the "key" column been automaticaly assigned a random and unique
| string?
|
| Then I can select this column and pass it in my app.
|
| Thanks a lot
|
| Regards
|
| Kuon

Notwithstanding Mladen answer (you can handle the duplicate key in
your application and retry the insert), you can do something like:

SQL> create table t2 (id varchar2(20), val number);

Table created.

SQL> create trigger t2_bir before insert on t2 for each row
2 begin
3 :new.id := dbms_random.string('U', 20) ;
4 end;
5 /

Trigger created.

SQL> var id varchar2(20)
SQL> insert into t2 (val) values(1) returning id into :id;

1 row created.

SQL> print id
ID
--------------------------------
ZFQACDEGCROHBYTXXXNK

SQL> select * from t2;
ID VAL
-------------------- ----------
ZFQACDEGCROHBYTXXXNK 1

1 row selected.

Regards
Michel Cadot


.



Relevant Pages

  • Re: Problems with Delete Command
    ... The SQL could get fairly messy if you need to construct it in code, ... ContactID, and WebComID, and create your on-the-fly SQL on that saved query, ... to find the list of ContactIDs from the junction table, ... This is a style/readability thing: if you are going to use string ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Need help with Code Please!!!
    ... posted in response to my last post was some SQL, but this is not the SQL that ... the actual string that gets built at the end of the SQ1 build process. ... The message says Syntax error in query expression ... Dim rs As Recordset 'object ref to qryCompany\USFNumber ...
    (microsoft.public.access.formscoding)
  • RE: Web Part and Access database
    ... I dont know if it is possible to connect to access,but you can download SQL ... Server Error in '/Webparts' Application. ... The connection string specifies a local Sql Server Express instance ... String user, String password, Boolean trusted, String connectionString) ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: INDEXES: BTRIEVE vs EXTFH (cobol)
    ... Bill Bach wrote: ... In SQL, you can specify just about anything, but at the lower MKDE ... the engine has to pick the right Btrieve ... When COBOL needs to find this value, it knows that the key is a string. ...
    (comp.databases.btrieve)
  • Re: INDEXES: BTRIEVE vs EXTFH (cobol)
    ... In SQL, you can specify just about anything, but at the lower MKDE ... the engine has to pick the right Btrieve ... When COBOL needs to find this value, it knows that the key is a string. ...
    (comp.databases.btrieve)