Michael
Michael

Reputation: 13616

Is it possible to make parameter to use in where clause in stored procedure?

I am using SQL Server 2012.

I have this table named myDataTable:

| Id | ExternalId | Subject | CMT           |    
| 1  |   3379     | damage5 | some comment7 |
| 2  |   3380     | damage3 | some comment3 | 
| 3  |   3382     | damage4 | some comment5 |
| 4  |   3381     | damage1 | some comment4 |

The ExternalId column is of type Int.

The CMT column is of type nvarchar(max).

I pass to stored procedure parameter @filterParam of string type.

In stored procedure I need to check if @filterParam contains at least one letter I use @filterParam to filter selection with Subject column, like that :

ALTER PROCEDURE [dbo].[SPGeneralReport]
            @filterParam nvarchar(max),
            @CMTParam nvarchar(max)
AS
BEGIN

SET NOCOUNT ON;
SELECT      *                
FROM        myDataTable
WHERE       Subject = @filterParam AND CMT = @CMTParam
END

In stored procedure I need to check if @filterParam contains only digits I need to cast it to integer and use it in WHERE clause to filter selection with ExternalId column :

ALTER PROCEDURE [dbo].[SPGeneralReport]
            @filterParam nvarchar(max)
AS
BEGIN

SET NOCOUNT ON;
SELECT      *                
FROM        myDataTable
WHERE       ExternalId = @filterParam AND CMT = @CMTParam
END

If @filterParam is NULL I don't want to use it at all in my filter:

ALTER PROCEDURE [dbo].[SPGeneralReport]
            @filterParam nvarchar(max)
AS
BEGIN

SET NOCOUNT ON;
SELECT      *                
FROM        myDataTable
WHERE       CMT = @CMTParam 
END

I don't want to create multiple stored procedures, I want to create single procedure with optional parameter for filter.

Any idea how can I implement it?

Upvotes: 1

Views: 85

Answers (2)

TYY
TYY

Reputation: 2716

Since you have 3 cases in this situation I would do something like this. Filter out the common clause first, then use condition statements on the other filter ( note I had to enable Ole Automation Procedures).

Create Table #Temp
(
    Id INT,
    ExternalId Int,
    [Subject] NVarChar(128),
    Comment NVarChar(128)
)

Insert Into #Temp
Values

( 1  ,   3379     ,'damage5' ,'some comment7' ),
( 2  ,   3380     ,'damage3' ,'some comment4' ),
( 3  ,   3382     ,'damage4' ,'some comment5' ),
( 4  ,   3381     ,'damage1' ,'some comment4' )

Declare @filterParam nvarchar(max) = '3380', 
        @CMTParam nvarchar(max) = 'some comment4'

Select * Into #OtherTemp
From #Temp
Where Comment = @CMTParam

IF(@filterParam IS NULL)
Begin
    Select * 
    From #OtherTemp
End
Else If (dbo.fn_regex('^\d*$',@filterParam) <> 0)
Begin
    Select * 
    From #OtherTemp
    where ExternalId = Cast(@filterParam AS Int)
End
Else
Begin
    Select * 
    From #OtherTemp
    where Subject= @filterParam
End

Drop Table #Temp
Drop Table #OtherTemp

Another alternative, would be to create 2 variables and perform your parsing there and use the resultant in your where clause

Declare @filterSubject NVarChar(128), @filterId Int

If (dbo.fn_regex('^\d*$',@filterParam) <> 0)
Begin
    SET @filterId = Cast(@filterParam AS Int)
End
Else If (@filterParam IS NOT NULL)
Begin
    SET @filterSubject =  @filterParam
End 


Select * 
From #Temp
Where (@filterId IS NULL OR ExternalId = @filterId) 
        AND (@filterSubject IS NULL OR Subject =@filterSubject)
        AND Comment = @CMTParam

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93694

use OR condition

WHERE (ExternalId = @filterParam OR @filterParam IS NULL)
  AND CMT = @CMTParam

When @filterParam is NULL the first condition will not be applied

Upvotes: 1

Related Questions