views:

36

answers:

1

I have a stored procedure which is declared as follows:

    ALTER PROCEDURE dbo.thisProc
 @ID int,@TypeID int, @DocID int, @Section varchar(10)

What I need is to be able to do this:

If @ID is supplied, execute a particular if block if @ID is not supplied then move check if @TypeID is input and then execute another if block.

I don't want to write multiple sql queries to deal with different inputs/page sections that a user is viewing.

Thanks.

SAMPLE CODE:

    CREATE PROCEDURE GetArticle
    @ID int,@DoTypeID int, @DocID int, @Sec varchar(10)
AS
IF @ID IS NOT NULL AND , @DocID IS NOT NULL  AND @Sec = 'inner'
BEGIN
  SELECT  "my query is here" 

WHERE Articles.ID = @ID AND Articles.DocID = @DocID  

END
ELSE IF @ID IS NULL AND @DocID  IS NULL  AND @Sec  = 'list'
BEGIN
    EXEC GetDocList @DocTypeID
END

ELSE IF @ID IS NULL AND @DocID  IS NULL  AND @Sec = 'full'
BEGIN
EXEC GetDocList @DocTypeID
END
+1  A: 

Just give @id a default value of null. E.g.

@id int = null
Ben Robinson
...and don't pass any value in that parameter. That is, you don't have to pass a null value in the @id parameter, just don't include it.
Daniel Dyson
I presume that is what he is doing anyway, hence the "missing parameter" error message ;-)
Ben Robinson