based on this answer
i tryed to create a Select for on of my table
ALTER PROCEDURE _Einrichtung_Select
-- Parameters with default values
@EinrichtungId AS int = NULL,
@EinrichtungName AS nvarchar(50) = NULL,
@IsKueche AS bit = NULL,
@RefEinrichtungId AS int = NULL,
@RefSpeiseplantypId AS int = NULL
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- generic SELECT query
SELECT *
FROM Einrichtung
WHERE EinrichtungId = ISNULL(@EinrichtungId, EinrichtungId)
AND EinrichtungName = ISNULL(@EinrichtungName, EinrichtungName)
AND IsKueche = ISNULL(@IsKueche, IsKueche)
AND RefEinrichtungId = ISNULL(@RefEinrichtungId, RefEinrichtungId)
AND RefSpeiseplantypId = ISNULL(@RefSpeiseplantypId, RefSpeiseplantypId)
ORDER BY EinrichtungName
RETURN
but i got a problem with the bit type example sqlfiddle like you can see it should return 4 rows but it only returns 3 so what do i miss?
It's because you can have null as values of your columns. And SQL have three-value logic, so checking null = null will return UNKNOWN instead of TRUE (as you may expect).
I think this query will help you:
select *
from myTable
where
(@EinrichtungId is null or EinrichtungId = @EinrichtungId) and
(@EinrichtungName is null or EinrichtungName = @EinrichtungName) and
(@IsKueche is null or IsKueche = @IsKueche) and
(@RefEinrichtungId is null or RefEinrichtungId = @RefEinrichtungId) and
(@RefSpeiseplantypId is null or RefSpeiseplantypId = @RefSpeiseplantypId)
sql fiddle demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With