I need to write a stored procedure to insert a row into a table. The table has an Identity column called ID. My question is should I return the value and if so, how?
As far as I can see,
- I could return it as the return value,
- I could return it as an out parameter, or
- I could do nothing with it.
What would a DB programmer do as a default?
If you are looking for a One True Way Of Writing All Insert Procedures, then you’re wasting your time. There will always be a table somewhere in your database that requires you to violate your own conventions somehow. The best you can hope for is a set of general consistency guidelines (not rules).
For general consistency guidelines, I suggest the following:
intand cannot be changed. Accordingly, it is best reserved for returning zero for success or@@errorfor failure.@@identityvalue and any other auto-generated values (e.g. GUIDs, timestamps) in this manner.