Hmmm... Well here is a quick sample SP I just wrote that should help you with the variable usage.
USE pubs
GO
-- First check to see if the sproc exists and if so drop it.
IF EXISTS(SELECT * FROM sysobjects WHERE id = object_id('MySampleSproc'))
DROP PROCEDURE MySampleSproc
GO
/*---------------------------------------------------------------------------------------
Name: MySampleSproc
Description: sample procedure for how to use variables.
Sample Call:
DECLARE @nAuthorID varchar(11) , @dtStartDate datetime
SET @dtStartDate = convert(datetime,'01/01/1989')
EXEC MySampleSproc 'Green', @nAuthorID OUTPUT, @dtStartDate
SELECT @nAuthorID AuthorID
History:
3/24/2004 gary johnson Created
---------------------------------------------------------------------------------------*/
-- Now create the sproc
CREATE PROCEDURE MySampleSproc
(
-- Add in the parameter variables
@nMyVar varchar(40) -- Input only
, @nMyVar2 varchar(11) = NULL OUTPUT -- Assigns default value and allow output of variable
-- Also makes variable not be required to call
, @dtMyVar3 datetime = NULL
)
AS
BEGIN -- Procedure
DECLARE -- Procedure Level Variables
@dtMyVar4 datetime
SET @dtMyVar4 = GetDate()
IF @dtMyVar3 IS NULL
BEGIN
SET @dtMyVar3 = GetDate() - 50
END
-- Get AuthorID
SELECT @nMyVar2 = au_id
FROM dbo.authors
WHERE au_lname = @nMyVar
-- Now show All Sales for this author between today and @dtMyVar3
SELECT s.stor_id
, s.ord_num
, s.ord_date
, s.qty
, s.payterms
, s.title_id
FROM dbo.sales s
JOIN dbo.titleauthor ta ON s.title_id = ta.title_id and ta.au_id = @nMyVar2
WHERE s.ord_date BETWEEN @dtMyVar3 and @dtMyVar4
RETURN
END -- Procedure
Gary Johnson
Microsoft Natural Language Group
DBA, Sr. DB Engineer
This posting is provided "AS IS" with no warranties, and confers no rights. The opinions expressed in this post are my own and may not reflect that of my employer.