Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

problem returning IDENTITY
Hi all,

I have a sp where I only do an insert, and am trying to return the
identity value created. Here's what my sp resemble :

...
AS
SET NOCOUNT ON
-- do the insert
DECLARE @ret
SELECT @ret = SCOPE_IDENTITY()
RETURN @ret

I tried to return directly SCOPE_IDENTITY(), I also tried to change
SCOPE_IDENTITY() with @@IDENTITY, with and without SET NOCOUNT ON...I
don't know what to do anymore. A few times it returned always 1 and at
other times, it was always returning -1. This depended on what options
I've tried, I don't remember what situation returned what value...but
for now, written as above, it is returning -1.

thanks for your help!


Report this thread to moderator Post Follow-up to this message
Old Post
ibiza
02-01-06 01:23 AM


Re: problem returning IDENTITY
ibiza wrote:

> Hi all,
>
> I have a sp where I only do an insert, and am trying to return the
> identity value created. Here's what my sp resemble :
>
> ...
> AS
> SET NOCOUNT ON
> -- do the insert
> DECLARE @ret
> SELECT @ret = SCOPE_IDENTITY()
> RETURN @ret
>
> I tried to return directly SCOPE_IDENTITY(), I also tried to change
> SCOPE_IDENTITY() with @@IDENTITY, with and without SET NOCOUNT ON...I
> don't know what to do anymore. A few times it returned always 1 and at
> other times, it was always returning -1. This depended on what options
> I've tried, I don't remember what situation returned what value...but
> for now, written as above, it is returning -1.
>
> thanks for your help!

It isn't a good idea to use RETURN to return data from a proc. Use
RETURN for error status only: zero = OK, non-zero = error. To return
other values use an output parameter or a result set:

CREATE PROC usp_x
(@param1 INTEGER, @ret INTEGER OUTPUT)
AS
SET NOCOUNT ON;
SET @ret = 123;
RETURN
GO

DECLARE @r INTEGER;
EXEC usp_x @param1 = 1, @ret = @r OUTPUT;
SELECT @r;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--


Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
02-01-06 01:23 AM


Re: problem returning IDENTITY
thank you very much for your reply. Well, it does work with an output
parameter!

And I will take note of your remark for my upcoming posts.

Thanks again! :)


Report this thread to moderator Post Follow-up to this message
Old Post
ibiza
02-01-06 01:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 04:48 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006