Randomly generate an ID for SQL Create Table command

Hi,
I need help to randomly generate an integer, which i can use as a primary key for the ID of a table field. I have no idea how to do this so that it generates a different number every single time.
Right now it just increments the ID by 1. Maybe, is there a way to get the last ID value of the table field and then increment starting from that one? thanks

Your analysis is flawed! One of my main reasons for
doing this is to reduce the load on the db server. I
don't have to go to the db to get an id every time I
need one. Every now and again (usually less than one
in a 2000) I have to repeat an insert operation.
You already stated that this was the reason, so I used that as part of my assessment. With most, perhaps all databases, the ability to increment a key is done as part of the insert operation. It requires no additional 'trip' to the database to get the ID. There is so much wrong with your idea, I hardly know where to start. I wouldn't be so firm except you stated that you do this all the time. Even using it once is very questionable I wouldn't want this idea to profilerate to other peoples systems without comment.
You also said:
On one project I was having to place usage transaction records
into a database with each one needing an ID. I started using your
technique but it was taking almost as much time as the insert of
each transaction into the database.I agree, and I think dcminter agreed that there are less expensive practices then max(fld)+1 which are in fact using the vendor specific capabilities. However, if this was taking as long as your insert, there was probably something else wrong. For example, if the field you are getting the max value for isn't indexed, or isn't the first field in a contatenated index (an index of multiple fields), then some databases (there are exceptions like redbrick) will require either a full table scan, an index scan or an ackward index lookup to give back the max value. It shouldn't be that way, but it is and you have undestand how your database is resolving max() in order to make the best decisions.
I stand by analysis and I would suggest that rather then use your method "all the time" that you save it for that unusual situation where nothing else will work for you.

Similar Messages

  • Using javascript to dynamically generate the html for a  Spry table

    I have some javascript which generates html code for a Spry
    table which displays the content of a Spry data set. The problem is
    that after I generate the html, Spry never fills in the values for
    the table.
    Here's what I'm doing:
    Using a data set observer, check for onPostLoad and generate
    the html code
    Using innerHTML, stuff the code into the appropriate
    <div>
    Use regionName.updateContent() to update the region I just
    created with the html code I generated
    Interestingly, if I take the generated source for my page and
    remove the data set observer, the Spry table displays correctly
    with all the expected values. This suggests to me that my generated
    html code is correct, but that there is some sort of timing issue
    (i.e. Spry doesn't know about the new code I generated).
    Any suggestions?

    jalperin wrote:
    > Here's what I'm doing:
    >
    Using a data set observer, check for
    onPostLoad and generate the html
    > code
    >
    Using innerHTML, stuff the code into
    the appropriate <div>
    >
    Use regionName.updateContent() to
    update the region I just created
    > with the html code I generated
    >
    > Interestingly, if I take the generated source for my
    page and remove the data
    > set observer, the Spry table displays correctly with all
    the expected values.
    > This suggests to me that my generated html code is
    correct, but that there is
    > some sort of timing issue (i.e. Spry doesn't know about
    the new code I
    > generated).
    >
    > Any suggestions?
    I'd guess that it is a timing issue as well. I haven't looked
    much at the dataset capability in Spry, but it may be that the
    dataset "finds" all of the regions and code that it applies to
    before the onPostLoad event is triggered. If that is the case, then
    your generated code doesn't exist yet,and so the dataset thinks
    that there is nothing for it to apply to.
    Will the HTML that you generate change based upon the dataset
    that is actually returned? I guess I'm looking for the reason why
    the HTML is being generated when it could be in the page itself and
    avoid this issue completely.
    Danilo Celic
    |
    http://blog.extensioneering.com/
    | WebAssist Extensioneer
    | Adobe Community Expert

  • JDBB SQL create table PreparedStatement

    Hi
    How can I use preparedSatement to create a variable table?
    PreparedStatement ps=con.prepareStatement("what is the SQL Code for a table named [?]");
    ps.setString(1, tableName);
    ResultSet r = ps.executeQuery();

    I think it'd be the usual SQL:
    CREATE TABLE X
       FOO INTEGER,
       BAR VARCHAR2(80),
       etc.
    )The types would have to be correct for your RDB.
    I don't think it'd be a query, so it wouldn't return a ResultSet. I believe it's like an UPDATE, so you'd call executeUpdate on the statement. - MOD

  • SYS.DBMS_CDC_PUBLISH errors during create table command

    While trying to create tables I am getting the following errors.
    @dv001_bld_emp_profile.sql
    create table emp_profile_tbl (
    ERROR at line 1:
    ORA-00604: error occurred at recursive SQL level 1
    ORA-01422: exact fetch returns more than requested number of rows
    ORA-06512: at "SYS.DBMS_CDC_PUBLISH", line 326
    ORA-06512: at line 3
    I tried to reload the predefined Oracle packages by re-running the catproc.sql script but that had no effect.
    Can you help???

    I tried that but no luck yet, also tried put all in a single line, but the same error. It doesn't something tricky here, but actual problem with package. "ddl_markers" is new to 11g and not much documentation is available on that.
    Also setting ddl_markers=N removed 3 columns from Publisher table, and the effect of it on Subscriber's view is still a gray area,

  • Passing variable to create table command

    Is it possible to pass a varaiable to the create table command.
    At each yearend we have to manually create copy tables of the years data these are held as table_name_2002 etc. The original table only holds the current years data. We would like to create a script to create all these, along the lines of
    CREATE TABLE table_name_||'&year'
    AS
    SELECT * FROM table_name;
    As his is run every year we would like to be able to run the same script, and just pass in the year each time.
    however i cannot get this to work, is there another way round this?
    Thanks

    try this (assuming the year is passed in as the first runtime parameter):
    CREATE TABLE table_name_&1.
    AS
    SELECT * FROM table_name;
    otherwise if you do not want to work with runtime parameters you can do it like this so that v_year is only asked for once:
    define vv_year="&v_year."
    CREATE TABLE table_name_&vv_year.
    AS
    SELECT * FROM table_name;
    or you can ask for it your self, which is probably the nicest way of dealing with it ;) :
    accept v_year char prompt Please input the year:
    CREATE TABLE table_name_&v_year.
    AS
    SELECT * FROM table_name;
    HTH,
    Lennert

  • How to generate test data for all the tables in oracle

    I am planning to use plsql to generate the test data in all the tables in schema, schema name is given as input parameters, min records in master table, min records in child table. data should be consistent in the columns which are used for constraints i.e. using same column value..
    planning to implement something like
    execute sp_schema_data_gen (schemaname, minrecinmstrtbl, minrecsforchildtable);
    schemaname = owner,
    minrecinmstrtbl= minimum records to insert into each parent table,
    minrecsforchildtable = minimum records to enter into each child table of a each master table;
    all_tables where owner= schemaname;
    all_tab_columns and all_constrains - where owner =schemaname;
    using dbms_random pkg.
    is anyone have better idea to do this.. is this functionality already there in oracle db?

    Ah, damorgan, data, test data, metadata and table-driven processes. Love the stuff!
    There are two approaches you can take with this. I'll mention both and then ask which
    one you think you would find most useful for your requirements.
    One approach I would call the generic bottom-up approach which is the one I think you
    are referring to.
    This system is a generic test data generator. It isn't designed to generate data for any
    particular existing table or application but is the general case solution.
    Building on damorgan's advice define the basic hierarchy: table collection, tables, data; so start at the data level.
    1. Identify/document the data types that you need to support. Start small (NUMBER, VARCHAR2, DATE) and add as you go along
    2. For each data type identify the functionality and attributes that you need. For instance for VARCHAR2
    a. min length - the minimum length to generate
    b. max length - the maximum length
    c. prefix - a prefix for the generated data; e.g. for an address field you might want a 'add1' prefix
    d. suffix - a suffix for the generated data; see prefix
    e. whether to generate NULLs
    3. For NUMBER you will probably want at least precision and scale but might want minimum and maximum values or even min/max precision,
    min/max scale.
    4. store the attribute combinations in Oracle tables
    5. build functionality for each data type that can create the range and type of data that you need. These functions should take parameters that can be used to control the attributes and the amount of data generated.
    6. At the table level you will need business rules that control how the different columns of the table relate to each other. For example, for ADDRESS information your business rule might be that ADDRESS1, CITY, STATE, ZIP are required and ADDRESS2 is optional.
    7. Add table-level processes, driven by the saved metadata, that can generate data at the record level by leveraging the data type functionality you have built previously.
    8. Then add the metadata, business rules and functionality to control the TABLE-TO-TABLE relationships; that is, the data model. You need the same DETPNO values in the SCOTT.EMP table that exist in the SCOTT.DEPT table.
    The second approach I have used more often. I would it call the top-down approach and I use
    it when test data is needed for an existing system. The main use case here is to avoid
    having to copy production data to QA, TEST or DEV environments.
    QA people want to test with data that they are familiar with: names, companies, code values.
    I've found they aren't often fond of random character strings for names of things.
    The second approach I use for mature systems where there is already plenty of data to choose from.
    It involves selecting subsets of data from each of the existing tables and saving that data in a
    set of test tables. This data can then be used for regression testing and for automated unit testing of
    existing functionality and functionality that is being developed.
    QA can use data they are already familiar with and can test the application (GUI?) interface on that
    data to see if they get the expected changes.
    For each table to be tested (e.g. DEPT) I create two test system tables. A BEFORE table and an EXPECTED table.
    1. DEPT_TEST_BEFORE
         This table has all EMP table columns and a TEST_CASE column.
         It holds EMP-image rows for each test case that show the row as it should look BEFORE the
         test for that test case is performed.
         CREATE TABLE DEPT_TEST_BEFORE
         TESTCASE NUMBER,
         DEPTNO NUMBER(2),
         DNAME VARCHAR2(14 BYTE),
         LOC VARCHAR2(13 BYTE)
    2. DEPT_TEST_EXPECTED
         This table also has all EMP table columns and a TEST_CASE column.
         It holds EMP-image rows for each test case that show the row as it should look AFTER the
         test for that test case is performed.
    Each of these tables are a mirror image of the actual application table with one new column
    added that contains a value representing the TESTCASE_NUMBER.
    To create test case #3 identify or create the DEPT records you want to use for test case #3.
    Insert these records into DEPT_TEST_BEFORE:
         INSERT INTO DEPT_TEST_BEFORE
         SELECT 3, D.* FROM DEPT D where DEPNO = 20
    Insert records for test case #3 into DEPT_TEST_EXPECTED that show the rows as they should
    look after test #3 is run. For example, if test #3 creates one new record add all the
    records fro the BEFORE data set and add a new one for the new record.
    When you want to run TESTCASE_ONE the process is basically (ignore for this illustration that
    there is a foreign key betwee DEPT and EMP):
    1. delete the records from SCOTT.DEPT that correspond to test case #3 DEPT records.
              DELETE FROM DEPT
              WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT_TEST_BEFORE WHERE TESTCASE = 3);
    2. insert the test data set records for SCOTT.DEPT for test case #3.
              INSERT INTO DEPT
              SELECT DEPTNO, DNAME, LOC FROM DEPT_TEST_BEFORE WHERE TESTCASE = 3;
    3 perform the test.
    4. compare the actual results with the expected results.
         This is done by a function that compares the records in DEPT with the records
         in DEPT_TEST_EXPECTED for test #3.
         I usually store these results in yet another table or just report them out.
    5. Report out the differences.
    This second approach uses data the users (QA) are already familiar with, is scaleable and
    is easy to add new data that meets business requirements.
    It is also easy to automatically generate the necessary tables and test setup/breakdown
    using a table-driven metadata approach. Adding a new test table is as easy as calling
    a stored procedure; the procedure can generate the DDL or create the actual tables needed
    for the BEFORE and AFTER snapshots.
    The main disadvantage is that existing data will almost never cover the corner cases.
    But you can add data for these. By corner cases I mean data that defines the limits
    for a data type: a VARCHAR2(30) name field should have at least one test record that
    has a name that is 30 characters long.
    Which of these approaches makes the most sense for you?

  • Simple java class for SQL like table?

    Dear Experts,
    I'm hoping that the java people here at the Oracle forums will be able to help me.
    I've worked with Oracle and SQL since the 90s.
    Lately, I'm learning java and doing a little project in my spare time. 
    It's stand alone on the desktop.
    The program does not connect to any database 
    (and a database is not an option).
    Currently, I'm working on a module for AI and decision making.
    I like the idea of a table in memory.
    Table/data structure with Row and columns.
    And the functionality of:
    Select, insert, update, delete.
    I've been looking at the AbstractTableModel.
    Some of the best examples I've found online (they actually compile and work) are:
    http://www.java2s.com/Code/Java/Swing-JFC/extendsAbstractTableModeltocreatecustommodel.htm
    http://tutiez.com/simple-jtable-example-using-abstracttablemodel.html
    Although they are rather confusing.
    In all the examples I find, there always seems to be
    at least three layers of objects:
    Data object (full of get/set methods)
    AbstractTableModel
    GUI (JFrame, JTable) (GUI aspect I don't need or want)
    In all the cases I've seen online, I have yet to see an example
    that has the equivalent of Delete or Insert.
    Just like in SQL, I want to define a table with columns.
    Insert some rows. Update. Select. Delete.
    Question:
    Is there a better java class to work with?
    Better, in terms of simpler.
    And, being able to do all the basic SQL like functions.
    Thanks a lot!

    Hi Timo,
    Thanks. yes I had gone thru the java doc already and  they have mentioned to use java.sql.Struct, but the code which got generated calls constructor of oracle.jpub.runtime.MutableStruct where it expects oracle.sql.STRUCT and not the java.sql.STRUCT and no other constructor available to call the new implementation and that is the reason i sought for some clues.
      protected ORAData create(CmnAxnRecT o, Datum d, int sqlType) throws SQLException
        if (d == null) return null;
        if (o == null) o = new CmnAxnRecT();
        o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
        return o;
    here CmnAxnRecT is the class name of the generated java class for sqlType.
    Thanks again. Please let me know if you have any more clues.
    Regards,
    Vinothgan AS

  • How do I AUTOMATICALLY generate Transport request for a Z table data?

    Hello friends,
    I would like to automatically create/generate a transport request for a Z table every time I change, add or delete data in that table.
    Currently, every time I modify data in the Z table, I need to go and manually create a transport request and then indicate keys for the row(s) which were modified. I find this a tedious, time consuming, and error prone process.
    So I would like to automate this process. That is to say, the transport request is generated for the rows that I modified. The same way as is done when you change a program. It automatically prompts you for a transport request thingy.
    Any ideas?
    Your help is greatly appreciated.

    You need to make the table contents transportable by creating a "table maintenance generator" in SE11 under Utilities -> Table maintenance generator.
    In that screen for the section "Dialog data transport Details" the "Standard Recording Routine" option should be selected.
    Then only will the system ask for Transport request when entering data through the generated maintenance view in SM30.
    Also change the "delivery class' in the table attributes to 'C' - Customizing table

  • EXEC SQL & CREATE TABLE

    Hi,
    I am trying to create table using EXEC SQL. But, the table that I had created using EXEC SQL is not shown in data dictionary. I can not see that table from SE11. If I run the same program again, then gives error about there is already a table that I want to create. So, How can see it? Is seeing that table possible?
    EXEC SQL.
    CREATE TABLE ZCOPYTABLE(
    NAME CHAR(15) NOT NULL,
    SURNAME CHAR(15) NOT NULL,
    TEL CHAR(10) NOT NULL,
    PRIMARY KEY (NAME, SURNAME)
    ENDEXEC.

    Hello Huseyin,
    I don't think you can use the Native SQL statements for DDL (data definition language) statements in SQL.
    The reason is that the Native SQL statements by-pass the SAP Application Server and are executed directly at the Database Server. You will find more information on the SAP Online Help Documentation site.
    Since the ABAP Dictionary is very much an application, it fails to recognize the tables which were not created through it.
    The DDL statements in Native SQL are only for advanced database adminstration tasks.
    Hope this helps.
    Regards,
    Anand Mandalika.

  • SQL Create Table

    Hi guys
    im getting an exception with create table. Im sure im doing it with the right syntax but maybe you guys could help me.
    This is the exception:
    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement.
            at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
            at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
            at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
            at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
            at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)this is the code
    Statement stmt1 = conn.createStatement();
    stmt1.executeQuery("CREATE TABLE " + t_UID + "Bank (Type int(5), amount int(10))");Ne help is appreciated thanks guys

    Statement stmt1 = conn.createStatement();
    stmt1.executeQuery("CREATE TABLE " + t_UID + "Bank
    (Type int(5), amount int(10))");Print out the String you are trying to execute - I would guess you are missing a space or a "." between t_UID and "Bank".

  • Using Create table command in a Pl/Sql Anonymous Block

    Hi,
    I need to create a table dynamically based on the table_name and column_names that a user wants. When I use a Pl/sql Anonymous block to do this, it complains. Any suggestions ?
    Thanks,
    Marisa

    Personally this sounds like a bad design to me. I would say under most "normal" circumstances, you should not be creating tables on the fly. Especially one where a user has control over what columns,datatypes to use. Let a developer or dba take care of that.

  • How to generate web services for MDM Taxanomy Tables?

    Hi Experts,
    I have a Taxonomy table called eClass in material repository which contains the Hierarchical data. The data[Attributes of this table ] can be seen only in Taxonomy Mode, I want to generate the web service using MDM Web Service Generator. The service should perform the CRUD operation over this table and retrieve the attributes. I generated the service for this table but I am not able to retrieve the attributes. Please help me out for this situations. Any pointers regarding this will be appreciated.
    Thanks & Regards
    Jeetendra

    Hi Jeethandra,
    Please refere to the below link might be some help.
    [http://help.sap.com/saphelp_mdmgds55/helpdata/en/45/069152856241e1e10000000a1553f6/frameset.htm]
    regards
    shankar

  • PL\SQL Create table

    Hi can anyone help with the below i have 3 variables that I select into then I want to use these variables in the create table but the CREATE table part errors saying it was expecting some thing else ???
    declare
    month_1 date;
    month_2 date;
    month_3 date;
    begin
    select cast('15-'||to_char(add_months(trunc(sysdate),-2),'Mon')||to_char(add_months(trunc(sysdate),-2),'-YYYY')as date) into month_1 from dual ;
    select cast('15-'||to_char(add_months(trunc(sysdate),-1),'Mon')||to_char(add_months(trunc(sysdate),-1),'-YYYY')as date) into month_2 from dual ;
    select cast('15-'||to_char(trunc(sysdate),'Mon')||to_char(trunc(sysdate),'-YYYY')as date) into month_3 from dual ;
    CREATE TABLE TEMP_TABLE
    (ACCOUNT_NO      NUMBER(11),
    ACCOUNT_NAME VARCHAR(152bytes),
    month_1          NUMBER,
    month_2 NUMBER,
    month_3          NUMBER
    END;

    There were some issues with your code. You can't (without the use of double quotes) start a column name with a number or include hyphens. Also, VARCHAR(152bytes) should be VARCHAR2(152 byte) or even VARCHAR2(152).
    This may help;
    SQL> declare
       month_1 varchar2(11);
       month_2 varchar2(11);
       month_3 varchar2(11);
    begin
       select to_char(add_months(trunc(sysdate,'mm')+14,-2),'MON_DD_YYYY') into month_1 from dual;
       select to_char(add_months(trunc(sysdate,'mm')+14,-1),'MON_DD_YYYY') into month_2 from dual;
       select to_char(trunc(sysdate,'mm')+14,'MON_DD_YYYY') into month_3 from dual;
       execute immediate 'create table temp_table( '
                                 || 'ACCOUNT_NO NUMBER(11), '
                                 || 'ACCOUNT_NAME VARCHAR(152), '
                                 || month_1 || ' NUMBER, '
                                 || month_2 || ' NUMBER, '
                                 || month_3 || ' NUMBER) ';
    end;
    PL/SQL procedure successfully completed.
    SQL> desc temp_table
    TABLE temp_table
    Name                                      Null?    Type                       
    ACCOUNT_NO                                         NUMBER(11)                 
    ACCOUNT_NAME                                       VARCHAR2(152)              
    DEC_15_2006                                        NUMBER                     
    JAN_15_2007                                        NUMBER                     
    FEB_15_2007                                        NUMBER

  • Setting the runtime environment for MySQL & creating tables,compile query

    Hi....
    i am a new user of MySQL server.i don't know the Rutime Environment on my machine.so please tell me how can i set the environment for MySQL.
    How can i create tables & compile query please tell me?

    All of those are MySQL only questions. Nothing to do with JDBC.
    There are MySQL forums. And there is certainly lots of MySQL documentation. So what of that have you read?

  • SSDT-BI for SQL Server 2014, Command line(silent unattendant automatic) installation script/steps

    Hi,
    I have downloaded SSDT-BI software and looking for commandline steps,config file for this.
    http://www.microsoft.com/en-us/download/details.aspx?id=42313
    Any one have Command line installations steps for SSDT-BI automatic(command line) installation for SQL Server 2014 with giving the files location,where to install.....etc, i mean config.xml.
    I found below but not sure it will work or not. If any one have working correct command line script, please reply on this forum.
    Looking for config.xml file, i searched online and did not find any where with clear config.xml file details.
    --Found below from link and not sure it will work or not
    http://blogs.technet.com/b/ilikesql_by_dandyman/archive/2013/03/10/how-to-automate-the-installation-of-sql-server-ssdt-office-2013-and-visual-studio-2012.aspx
    setup.exe /ACTION=INSTALL /FEATURES=SSDTBI_VS2012 /Q /IACCEPTSQLSERVERLICENSETERMS /norestart
    thanks in advance.

    Hi SQLDBA321,
    The SSDT-BI is automatically installed under xxx:\Program Files\Microsoft SQL Server\. Now matter in GUI or command line, we can't change the path. Because SSDT is a feature of SQL Server, not a feature of Visual Studio. It just use the Visual
    Studio Shell. So SSDT will only be installed with SQL Server.
    Reference:
    Install SQL Server Data Tools - Business Intelligence for Visual Studio 2013
    If you have any question, please feel free to ask. 
    Simon Hou
    TechNet Community Support

Maybe you are looking for

  • How do i download photos from iphone 4 to pc

    how do i download pictures from iphone 4 to my pc

  • Issues with opening multiple artboard docs in Illustrator CS6

    Hi. I'm working in CS6 Cloud. I have a document with multiple artboards. However, when I go to open it, a dialogue box comes up and says "Open Pdf - Select a page from PDF to open...". I can then only open single artboards as separate documents. How

  • Calling External WebService to send data from SharePoint O365 Aspx page

    Hi , There is a Public Website on O365 environment. which has  Apply Now Page on this User enters Name, email and Message and click Submit button., we want to pass these  All three fields data value  to External WebService , Which is developed in Asp

  • Business Process Integration using Web Services

    Hi! I'm interested to know if there are any products available today that can help a Java developer integrate loosely coupled components into a business process. I thought of doing this using existing Java APIs but I don't want to build all the infra

  • Ip igmp snooping querier?

    i've been reading these forums and noticed that lot of people have problems with configuring multicast. basicly, problems are flooding bandwidth, no querier, confusing pim router configuration ... also, ive seen some promisses from tehnical stuff tha