Why this is not possible in SQL Server?
I am getting:
Incorrect syntax near '@pp'.
SQL:
declare @pp INT = null
select *
from ExtraExaminationsReportView
where [PayerId] is @pp
The syntax you wrote won't work for a few reasons.
Firstly, if @pp was an int, the IS @PP syntax is incorrect, IS syntax is used in conjunction with NULL:
SELECT * FROM TABLE WHERE COL IS NULL
You can't do:
SELECT * FROM TABLE WHERE COL IS 1
You'd need to use = when checking for a numeric value:
SELECT * FROM TABLE WHERE COL = 1
So to cater for both a NULL value or an INT value, you can use an OR in the WHERE clause to check the value like so:
DECLARE @pp INT = NULL -- you can set this to 1 to show it works for both
-- temp table with a null value and an int value = 1
SELECT 1 val
INTO #TMP
UNION ALL
SELECT NULL
SELECT *
FROM #TMP
WHERE ( @pp IS NULL
AND val IS NULL
)
OR ( @pp IS NOT NULL
AND val = @pp
)
DROP TABLE #TMP
Modifying your code:
DECLARE @pp INT = NULL
SELECT *
FROM ExtraExaminationsReportView
WHERE ( @pp IS NULL
AND PayerId IS NULL
)
OR ( @pp IS NOT NULL
AND PayerId = @pp
)
You have to change your query logic. The only way is:
declare @pp INT = null
select * from ExtraExaminationsReportView where ([PayerId] IS NULL OR [PayerId] = @pp)
@pp is a number, then you will also return rows where playerId is null, which may not be desired.
@ppto e.g. 4. Your query would bewhere [PayerId] is 4which is definitly wrong.