all groups > sql server programming > january 2004 >
You're in the

sql server programming

group:

returning id of new row


returning id of new row Brian Henry
1/3/2004 10:03:05 PM
sql server programming:
What is the best way to do this? (in a stored procedure of course) this is
how i was thinking, are there any problems with this? here is my pseudo code
for the stored proc...

* Data recieved through paramaters
* Select top 1 ID from table ordered in descending order save into a temp id
+ 1 so its the next blank id in @currentid
* insert into table with ID = @currentid
* return @currentid

is that the standard way to do it or is there a better way? thanks!



Re: returning id of new row Aaron Bertrand [MVP]
1/4/2004 12:07:00 AM
Use an IDENTITY column, and then determine afterward what it was by using
the SCOPE_IDENTITY() function.

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/




[quoted text, click to view]

Re: returning id of new row Brian Henry
1/4/2004 12:36:46 AM
ah thank you :) learning T-SQL very very slowly here...


[quoted text, click to view]

Re: returning id of new row Joe Celko
1/5/2004 10:01:53 AM
[quoted text, click to view]

To do what? You described a procedure, but did not tell us the goal.

When you are learning RDBMS, you need to start with an abstract view of
the world that wil become your data model. You should be discovering
verifiable identifiers in the reality you are modeling and not just
adding a meaningless machine generated code to a row.

There is no such thing in a valid relational model as a "general
purpose, one-size-fits-all identifier"; to be is to be something in
particular.

What does this identifier identify? Is there an industry standard for
it? Is there a company standard for it? Does it need a check digit?
etc.

The non-relational, proprietary kludge is to use IDENTITY as a if it
were a valid key. Don't; take the time to do the model correctly and
your boss will not have to hire me to re-do the schema in a few years.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button