August 5, 2011 at 5:24 am
Declare @Sno int
exec @Sno = exec proc1 @p1, @p2
/*Say exec proc1 @p1, @p2 returns int value 2 */
select @Sno-- It does not show any value neither throws any error
Though we can do it using table vriable or output variable.
August 5, 2011 at 6:08 am
it can be confusing how you are able to get data back from a procedure.
there are three ways, and you can use them ALL at the same time:
1. reutrn a resultset of rows/table.
2. output parameters
3. the RETURN command to return an integer.
CREATE Proc ReturningStuff(@name varchar(128),@id int OUTPUT) --the output param can be returned
AS
BEGIN
SELECT @id = object_id from sys.tables where name = @name -- assigning a value to the output variable
SELECT * FROM sys.tables where name = @name --creating a dataset/table to return back
--maybe you wanted to know how many rows?
--return can ONLY return an integer...
--usually 0 for no errors, anything else for failure SQL defaults the return to zero if not explcitly returning a value.
RETURN @@rowcount
END
GO
--and to test:
declare @AnObject varchar(128),
@myid as integer,
@results integer
SET @AnObject= 'TallyCalendar'
EXEC @results = ReturningStuff @AnObject,@myid OUTPUT --you have to say OUTPUT to sue the variable for output
PRINT CONVERT(varchar,@results) + ' Rows int he table?'
PRINT CONVERT(varchar,@myid) + ' objectID?'
/*
(1 row(s) affected)
1 Rows int he table?
405576483 objectID?
*/
Lowell
August 9, 2011 at 1:39 am
Thanks Powell. Since I could not change the existing procedure, tried putting the result into table variable.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply