Re: How to insert auto increment?



MN wrote:
Hi David,
Thank for reply. Yeah, some day I increate 100 rows, someday 90, or 10...the
value is vary. And there are no other column involved except IDENTITY column.
How can I do that? Regards-MN

Don't. There are two good reasons. 1. It's inefficient (because a
single row will do the same thing). 2. It may be unreliable (an
IDENTITY sequence can have gaps so the maximum value doesn't
necessarily match the number of rows).

Instead, use a single row:

CREATE TABLE tbl (x INTEGER PRIMARY KEY DEFAULT (1) CHECK (x=1) /*
single row constraint */, col1 INTEGER NOT NULL);
INSERT INTO tbl (col1) VALUES (0);
GO

Then keep updating it like this:

UPDATE tbl SET col1 = col1 + 100 ;


In case you do find it useful again, you can populate a table with
default values only or an IDENTITY column only using the DEFAULT VALUES
clause:

INSERT INTO tbl DEFAULT VALUES;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

.



Relevant Pages

  • HELP: Dropped table column causes sp_addmergearticle to fail with replication
    ... I'm setting up merge replication on sql server 2000 sp3. ... there is no identity column in this table ... The article 'tkDPLRS' could not be added to the publication 'TEST1'. ...
    (microsoft.public.sqlserver.replication)
  • Re: HasChanges returns false for one row, true when more than one
    ... Hitchhiker's Guide to Visual Studio and SQL Server ... and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook) ... "Mike G Burton" wrote in message ... dataset has a single row, but True if the dataset has multiple rows. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: @@Identity
    ... How Does SQL Server Manage Identity Values? ... When you add a row to a table with an Identity column you don't include a ... A scope is a module -- a stored procedure, trigger, function, or ... > If you are inserting a row with your ADO.Net code that will increment the ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: cluster index and identity
    ... Hot spotting data gives the cache manager something to grab hold of. ... Clustering on a narrow unique key also has benefits, ... I do not use the identity column as my Primary Key. ... I support the Professional Association for SQL Server ...
    (microsoft.public.sqlserver.server)
  • Re: PostgreSQL Query Errors?
    ... >>> Microsoft OLE DB Provider for ODBC Drivers error '80004005' ... > And he is also opening the whole table instead of a single row. ... So it would be better to use an UPDATE statement? ... has worked with SQL Server for a long time. ...
    (microsoft.public.inetserver.asp.db)