SQL Server 2000 Questions (Newb)

I am working on a program for a college class.  It is a system for an auction house (live auction, not eBay style).  I am writing the code in C# using Visual Studio 2005 and our database is a SQL Server 2000 database.  In our previous project we used Access 2003 as our database, so I have a few questions about SQL Server:

< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

  1. If I am going to insert a new record (programmatically), and the Primary Key field is an integer, how do I specify that the PK field should be set to the next integer value (In Access, you didn’t have to specify anything if you selected Autonumber as your datatype for the PK field).
  2. Is there a way to return that PK value to the program other than another query to the database (i.e. SELECT MAX(account_id) FROM Account…)

 

Thanks for any help you can give – I’m sure this won’t be my last call for help!

 

Jeff



Answer this question

SQL Server 2000 Questions (Newb)

  • sai81

    For #1, you can do the same thing in SQL Server.  Just create a int field with identity set on it, for example -> create table t1(f1 int identity(1,1), f2 varchar(10)).   You can create the table using the SQL GUI this way as well.

    For #2, you can do this by running "select @@identity" this will return the last allocated identity value on the current connection.



  • Jegant

    Thanks for the info guys!

    Jeff

  • rockstar_Sam

    You might want to use scope_identity() instead of @@identity

    You might get the identity of another insert with @@identity.


  • SQL Server 2000 Questions (Newb)