Re: column cannot contain null values - autoincrement column



David++ (David@xxxxxxxxxxxxxxxxxxxxxxxxx) writes:
I have a SQL Mobile database. I have defined in that database which has
once column which is set to auto increment when a new row is added. I
have a dataset which represents my database and have a table adapter
which links to that table. I have created a Sql query for the tabel
adapter which provides an Insert operation. By default the column set to
auto-increment was left out of the Insert statement which I think is th
correct behaviour. However, when I run this query from code it throws an
Exception - 'The column cannot contain null values. [Column name =
QAH_ID, Table name = QAHEADER]. I dont want to insert anything in that
column, I was hoping the column would auto increment by 1 on each
insert. Any ideas?

If SQL Server Mobile works like the real SQL Server, you should indeed
leave out that column. Maybe the columns is not IDENTITY after all?

Anyway, this should work:

DECLARE @id int
SELECT @id = coalesce(MAX(id), 0) + FROM tbl
INSERT tbl (id, ....
VALUES (@id, ....

(Again under the assumption that what works on SQL Server works on
SQL Server CE.)

Identity (or autonumber) values is quite an overvalued feature, and it
is often easier to roll your own.


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: Excell and Autoincrement id-fields
    ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) ... that I have a auto increment field of record id. ... Now the wizard reports that BLABLA_ID can't be NULL, ... some VBS code for it. ...
    (microsoft.public.sqlserver.dts)
  • Insert row in DataGrid, Web App
    ... I have a Web App with data grid connected to MS SQL Server. ... columns is primary key with Auto Increment. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: get the next auto increment
    ... I guess you are using SQL Server.. ... You can throw this at the end of your query ... And then pull @id out with an output parameter from your command object. ... > new id number that auto increment at the same time. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: view with auto_increment field
    ... > auto increment field but it doesn't work the way I want ... > SELECT @@ROWCOUNTas id, * ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: SQL QUERY HELP
    ... > Is there any "SQL query" which helps me to bring the above output ... SQL that generated something like the above. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)

Loading