views:

61

answers:

1

Hi all i'm looking for a way to pass in a generated id from a sproc to another sproc.

i.e.

@name varchar(12),
@descript varchar(55)
@test_date date,
@test_result varchar(1)
BEGIN
   EXEC ts_create_item @name, @descript
END
if (@test_date is not null) or (isnull(@test_result_id,'')!='')
BEGIN
   Exec ts_update_test_results @itemid(generated from the sproc before), @test_date, @test_result
End

Is there a way to do this in sql server?

Thanks

+1  A: 

Use an output variable

so you would declare the previous proc like

Create proc SomeProc (@parm1 int, @parm2 int, @id int = null OUTPUT)
as
Begin
  ...do some insert
  select @id = scope_identity()
End

And remember, OUTPUT has to be specified both when declaring and assigning the parameter ie.

Exec someproc @parm1, @parm2, @id OUTPUT

Alternately you can use a local variable to hold the result

e.g.

create proc somesample(@in int)
as
Begin
    select @in * 2
End 

declare @var int
exec @var = somesample 1
print @var
2
cmsjr
Thanks cmsjr. I'll try this!
AlteredConcept
np, let me know if you have any problems.
cmsjr