Board index » delphi » Insert and Identities/MS SQL

Insert and Identities/MS SQL

New to MS SQL...
I have created a test table w/an Identity value to use as my
autoincrememtal key.
How do I do an insert on an Identity value?  DEFAULT does not work in
this case.
Any suggestions?

code sample:
CREATE TABLE ITJAENIC.test (
     trackid int IDENTITY (100, 1) PRIMARY KEY NONCLUSTERED ,
     status varchar (15) NOT NULL)

SET IDENTITY_INSERT ITJAENIC.test ON

INSERT test (trackid,status) values (DEFAULT,'status 1')

Thanks,
     Karla

 

Re:Insert and Identities/MS SQL


Don't insert anything into the Identity field.  It will be populated by
the server.

Re:Insert and Identities/MS SQL


Re:Insert and Identities/MS SQL


--------------C21F6E1DF734F73952103489
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Let me try again.....  I'm new to MS SQL.  Right now I'm just using SQLW
so that I can test out my contraints and triggers.  All I want to know
is how do I get the Identity value to insert itself via a INSERT sql
statement?  If I use the code below I get this error:

/*-----------------------------
INSERT test (trackid,status) values (default,'status 1')
-----------------------------*/
Msg 515, Level 16, State 3
Attempt to insert the value NULL into column 'trackid', table
'CAMS2.ITJAENIC.test'; column does not allow nulls.  INSERT fails.
Command has been aborted.

None of the books I have completely explain how to do this

code sample:
CREATE TABLE ITJAENIC.test (
     trackid int IDENTITY (100, 1) PRIMARY KEY NONCLUSTERED ,
     status varchar (15) NOT NULL)

SET IDENTITY_INSERT ITJAENIC.test ON

INSERT test (trackid,status) values (DEFAULT,'status 1')

However,  this works fine:

create table #temp (col1 int identity(100,1))
select into #temp default values

....  I understand this part..... but what about
create table #temp (col1 int identity(100,1),status varchar(15))
select * from #temp where identitycol=@@identity

now, how do I use an insert statement with an identity and other
datatypes ?
insert into #temp values (@@identity,'status 1') ?   ....

Thanks,
   Karla

Quote
Scott Samet [TeamB] wrote:
> Don't insert anything into the Identity field.  It will be populated
> by
> the server.

--------------C21F6E1DF734F73952103489
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<HTML>
Let me try again.....&nbsp; I'm new to MS SQL.&nbsp; Right now I'm just
using SQLW so that I can test out my contraints and triggers.&nbsp; All
I want to know is how do I get the Identity value to insert itself via
a INSERT sql statement?&nbsp; If I use the code below I get this error:

<P>/*-----------------------------
<BR>INSERT test (trackid,status) values (default,'status 1')
<BR>-----------------------------*/
<BR>Msg 515, Level 16, State 3
<BR>Attempt to insert the value NULL into column 'trackid', table 'CAMS2.ITJAENIC.test';
column does not allow nulls.&nbsp; INSERT fails. Command has been aborted.

<P>None of the books I have completely explain how to do this

<P>code sample:
<BR>CREATE TABLE ITJAENIC.test (
<BR>&nbsp;&nbsp;&nbsp;&nbsp; trackid int IDENTITY (100, 1) PRIMARY KEY
NONCLUSTERED ,
<BR>&nbsp;&nbsp;&nbsp;&nbsp; status varchar (15) NOT NULL)

<P>SET IDENTITY_INSERT ITJAENIC.test ON

<P>INSERT test (trackid,status) values (DEFAULT,'status 1')

<P>However,&nbsp; this works fine:

<P>create table #temp (col1 int identity(100,1))
<BR>select into #temp default values

<P>....&nbsp; I understand this part..... but what about
<BR>create table #temp (col1 int identity(100,1),status varchar(15))
<BR>select * from #temp where identitycol=@@identity

<P>now, how do I use an insert statement with an identity <B>and</B> other
datatypes ?
<BR>insert into #temp values (@@identity,'status 1') ?&nbsp;&nbsp; ....&nbsp;

<P>Thanks,
<BR>&nbsp;&nbsp; Karla
<BR>&nbsp;

Quote
<P>Scott Samet [TeamB] wrote:

<BLOCKQUOTE TYPE=CITE>Don't insert anything into the Identity field.&nbsp;
It will be populated by
<BR>the server.</BLOCKQUOTE>
&nbsp;&nbsp;</HTML>

--------------C21F6E1DF734F73952103489--

Other Threads