Author |
Topic |
pehim
Starting Member
6 Posts |
Posted - 2002-05-30 : 06:10:58
|
I have problem with at Stored Procedure on SQL Server 2000. I need to get an @@IDENTITY back from a SP.The SP looks like this:CREATE PROCEDURE sp_NewCompany( @LCID nvarchar (4), @Email nvarchar (200), @Password nvarchar (200), @Name nvarchar (320), @PostalCode int, @City nvarchar (100), @Postbox nvarchar (300), @Address nvarchar (300), @CVR nvarchar (40), @Website nvarchar (100), @Phone nvarchar (100), @Fax nvarchar (100), @Logo nvarchar (200))AS INSERT INTO Companies (LCID,Email,Password,Name,PostalCode,City,Postbox,Address,CVR,Website,Phone,Fax,Logo) VALUES (@LCID,@Email,@Password,@Name,@PostalCode,@City,@Postbox,@Address,@CVR,@Website,@Phone,@Fax,@Logo) RETURN SELECT @@IDENTITYGOBut, when I run the SP from Query Analyzer I get no return value:sp_NewCompany 'DK','company@test.dk','secret','Jim Jones',1234,'Copenhagen','264','Street 1','23456789','www.test.dk','2828282','2929929','companylogo.gif'It only says:(1 row(s) affected)What am I doing wrong, can anybody help?Edited by - peh@immisceo.dk on 05/30/2002 07:27:33 |
|
MakeYourDaddyProud
184 Posts |
Posted - 2002-05-30 : 06:47:18
|
Try saving @@identity to a local variable first. I suspect that the @@identity is be posted from the result of the return, hence zero. Rather similar to the behaviour of @@error system variable. |
 |
|
pehim
Starting Member
6 Posts |
Posted - 2002-05-30 : 07:17:02
|
Thanx for answering so fast, but could you post some code to do that - I'm kind of new to SQL Server |
 |
|
YellowBug
Aged Yak Warrior
616 Posts |
Posted - 2002-05-30 : 07:19:47
|
Try this:-- Execute the stored procedure and return code values.DECLARE @ReturnCode INTEXEC @ReturnCode = sp_NewCompany 'DK','company@test.dk','secret','Jim Jones,1234,'Copenhagen','264','Street 1','23456789', 'www.test.dk','2828282','2929929','companylogo.gif' |
 |
|
pehim
Starting Member
6 Posts |
Posted - 2002-05-30 : 07:25:36
|
That didn't work :-( Any other suggestions? |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-05-30 : 08:12:38
|
sp_NewCompanydeclare @rc intinsert ...select @rc = @@identityreturn @rcgocall byexec @id = sp_NewCompany ...I prefer to use output parameters though and reserve return codes for error codes.create procedure sp_NewCompany@id int output..asinsert ...select @id = @@identityreturngocall byexec @rc = sp_NewCompany @id output, ...If this doesn't work check there isn't something that is losing the identity like a trigger.Alsl check @@error to make sure the insert works.==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy.Edited by - nr on 05/30/2002 08:13:25 |
 |
|
ToddV
Posting Yak Master
218 Posts |
Posted - 2002-05-30 : 08:22:25
|
I agree with nr about the output parameter, but I will also point out one more thing.When the original procedure gets to return it does not return, It does not run any of the statements after it. If you want to return the Identity as a record set, move select @@Identity before the returnSELECT @@IDENTITY RETURN To Return @@Identity as a return status, you can simply remove the select (I think).RETURN @@IDENTITY I however agree with nr's recommendation to use an output parameter. |
 |
|
pehim
Starting Member
6 Posts |
Posted - 2002-05-30 : 08:40:15
|
Hmm... The Output method doesn't work:SP:CREATE PROCEDURE sp_NewCompany( @LCID nvarchar (4), @Email nvarchar(100), @Password nvarchar (200), @Name nvarchar (320), @PostalCode int, @City nvarchar (100), @Postbox nvarchar (300), @Address nvarchar (300), @CVR nvarchar (40), @Website nvarchar (100), @Phone nvarchar (100), @Fax nvarchar (100), @Logo nvarchar (200), @FK_CID int output)AS INSERT INTO Companies (LCID,Email,Password,Name,PostalCode,City,Postbox,Address,CVR,Website,Phone,Fax,Logo) VALUES (@LCID,@Email,@Password,@Name,@PostalCode,@City,@Postbox,@Address,@CVR,@Website,@Phone,@Fax,@Logo) SELECT @FK_CID = @@IDENTITY RETURNGOQA:DECLARE @FK_CID intexec @FK_CID = sp_NewCompany 'DK','company@test.dk','123','Jim Jones',1234,'Copenhagen','264','Street 1','23456789','www.test.dk','2828282','2929929','companylogo.gif',@FK_CIDIt returns: (1 row(s) affected)However when I use the return method it works fine:CREATE PROCEDURE sp_NewCompany (...)ASINSERT ....SELECT @@IDENTITY RETURNAm I doing something wrong or what??? |
 |
|
ToddV
Posting Yak Master
218 Posts |
Posted - 2002-05-30 : 08:43:28
|
Try this with output parameter method.DECLARE @FK_CID intexec @FK_CID = sp_NewCompany 'DK','company@test.dk','123','Jim Jones',1234,'Copenhagen','264','Street 1','23456789','www.test.dk','2828282','2929929','companylogo.gif',@FK_CID OUTPUTSELECT "@FK_CID" = @FK_CID |
 |
|
pehim
Starting Member
6 Posts |
Posted - 2002-05-30 : 08:57:42
|
Hmm, that doesn't work either - I guess I stick to the Return method |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-05-30 : 10:41:53
|
Should work.If you get this sort of problem it is always worth writing some test code.Try thiscreate procedure spTest@i int outputasdeclare @j intselect @i = @i + 1, @j = @ireturn @j gocreate procedure spCallasdeclare @i int, @rc intset @i = 5exec @rc = spTest @i outputselect @rc, @igoexec spCall==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
oob
Starting Member
15 Posts |
Posted - 2002-05-30 : 12:09:51
|
try this CREATE PROCEDURE sp_NewCompany ( @NewID int output,@LCID nvarchar (4), @Email nvarchar (200), @Password nvarchar (200), @Name nvarchar (320), @PostalCode int, @City nvarchar (100), @Postbox nvarchar (300), @Address nvarchar (300), @CVR nvarchar (40), @Website nvarchar (100), @Phone nvarchar (100), @Fax nvarchar (100), @Logo nvarchar (200) ) AS INSERT INTO Companies (LCID,Email,Password,Name,PostalCode,City,Postbox,Address,CVR,Website,Phone,Fax,Logo) VALUES (@LCID,@Email,@Password,@Name,@PostalCode,@City,@Postbox,@Address,@CVR,@Website,@Phone,@Fax,@Logo) set @NewID =(SELECT @@IDENTITY) |
 |
|
|
|
|