Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a query as :

SQL
select @name=name,@address=address from Attendance_Master where OffEmail=@Email and LoginTime between @fromdate and @todate

and I want to use this query in stored procedure.

In Procedure I want to pass three values
1) @Email
2) @fromdate
3) @todate
and want to return two values
1) @name
2) @address

Please let me know how to do it and also how to store returning values during execution of stored procedure.

thanking You
Mohd. Wasif
Posted
Updated 8-Apr-11 7:01am
v2

You can create a proc using OUTPUT
SQL
CREATE PROCEDURE myProc
@Email    VARCHAR(100),
@fromdate DATETIME,
@todate   DATETIME,
@name     VARCHAR(100) OUTPUT,
@address  VARCHAR(100) OUTPUT
AS
select  TOP 1
        @name    = name,
        @address = address
from    Attendance_Master
where   OffEmail=@Email
        and
        LoginTime between @fromdate and @todate

This is how you can use the proc
SQL
DECLARE @name     VARCHAR(100)
DECLARE @address  VARCHAR(100)

EXEC myProc @Email='email@email.com',@fromdate='2011-01-01',@todate='2011-01-31', @name = @name OUTPUT, @address = @address OUTPUT

PRINT @name
PRINT @address
 
Share this answer
 
Comments
Mohd Wasif 11-Apr-11 7:31am    
Costica Thank you .
Can you do a favour for me .
How to use this procedure through aspx page.
Very simple!

End your stored procedure with :

SELECT @Name as name, @address as address
 
Share this answer
 
You could use output variables

SQL
CREATE PROCEDURE  GetDetails

	(
		@Input1			SMALLINT,
		@Input2			SMALLINT,
		@Output1		INT OUTPUT
		@Output2		VARCHAR(50) OUTPUT
	)

AS

SET NOCOUNT ON

SELECT
	@Output1 = Field1, 
	@Output2 = Field2
FROM
	MyTable
WHERE
	SomeField = @Input1
AND
	SomeOtherField = @Input2 


Then just grab the values from whatever code is consuming the procedure

http://dotnetgems.blogspot.com/2007/10/using-output-parameters-in-stored.html[^]

Or you could just return a ResultSet and read the values, both are valid.
 
Share this answer
 
I didn't see if you mentioned the amount of returning data rows. If you're returning several rows from the procedure, you could simply write:

SQL
create procedure SomeProcedure 
   @email varchar(max),
   @fromdate datetime,
   @todate datetime as
begin
   select name,address 
   from Attendance_Master 
   where OffEmail=@Email 
   and LoginTime between @fromdate and @todate
end


One question though: If you're fetching few values from a table based on conditions on few fields, why do you want to do this using a procedure? You didn't mention the technology you're going to use to call this procedure, but if you're using for example C#, wouldn't it be simpler to execute the query as-is from the client?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900