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?