views:

32

answers:

1

Hi guys, I am using sql server 2008 which is communicating to java web service which is communicationg to asp.net application. I have written stored procedure as follows:

create procedure abc(@intA int,@intB int,@intOutputParameter int output)
as
  begin 
     insert into tbluser(A,B) values (@intA,@intB) 
       /*where B is foregin key */
      set @intOutputParameter  = IDENT_CURRENT ('tbluser')

end

             /*where B is foregin key */

--now when i am passing B as which is not present in master table it is throwing me error (In sql server 2008 )which is obsullatly fine and accepted But In java web method s My sp is geeting executed and my insert statement is not getting executed and directly the statement after insert is getting executed (identity_current("Tbluser")) ...why is it so

If u need more explanition please ask for it...

+1  A: 

a) Your @intOutputParameter parameter is not declared as output; use:

CREATE PROCEDURE abc (
     @intA int
    ,@intB int
    ,@intOutputParameter int OUTPUT
    )
AS

b) Are you positive that table tbluser has something like id int IDENTITY(1,1)?

c) What's with the begin end ?

UPDATE:

You could solve this by using try-catch inside the procedure, like:

ALTER PROCEDURE abc (
     @intA int
    ,@intB int
    ,@intOutputParameter int OUTPUT
    )
AS 
    BEGIN
        SET NOCOUNT ON
        BEGIN TRY
      /*where B is foregin key */
            INSERT  INTO tbluser ( A, B ) VALUES  ( @intA, @intB ) 
            SET @intOutputParameter = IDENT_CURRENT('tbluser')
        END TRY
        BEGIN CATCH
            SET @intOutputParameter = -1
        END CATCH   
    END

This will return -1 in case of an error, so if your table IDs are >1, then the Java application can differentiate between successful insert and an error.

Damir Sudarevic
sorry for mistakes ...yes..there is identity key
hrishi
and do not use ident_current for this. Use scope_identity(). Ident_current may give you an indentity value from a record someone else inserted.
HLGEM
Thanx man.....let me try....
hrishi
don u know any link where i can see standered Sps to refer ...
hrishi