Hi all, I cannot figure out a solution to this problem I am having and I cannot find anything helpful on the internet or on this forum after searching for a while... Any help is therefore greatly appreciated!
I am programming in C# using (strongly typed) datasets. There is one table consisting mostly of foreign keys that are nullable. So if there is nothing to refer to, the value is null. I recently changed this from having zero values in these cases after finally programming the relationships into the SQL SERVER database.
Now I want to be able to search for specific records based on parameters passed from the application layer. These parameters too, are nullable. I would like an exact match of all the foreign key values including any nulls.
The following SQL Statement no longer works after enforcing relationships and replacing all zero values with nulls:
It doesn't work because the proper syntax in case of null values should be 'IS NULL' and not '= NULL'.
So instead I am experimenting with CASE WHEN statements, but I am not coming anywhere with things like this:
Isn't there an easy solution to this that does not require using stored procedures?
Fedor Steeman
Geological Museum Copenhagen
Denmark
I am programming in C# using (strongly typed) datasets. There is one table consisting mostly of foreign keys that are nullable. So if there is nothing to refer to, the value is null. I recently changed this from having zero values in these cases after finally programming the relationships into the SQL SERVER database.
Now I want to be able to search for specific records based on parameters passed from the application layer. These parameters too, are nullable. I would like an exact match of all the foreign key values including any nulls.
The following SQL Statement no longer works after enforcing relationships and replacing all zero values with nulls:
Code:
SELECT taxid_ID, HigherTaxon, Genus, Subgenus, Species, Subspecies, IdentificationQualifier, IdentificationNotes
FROM TaxonIdentifications
WHERE (HigherTaxon = @hitaxon) AND (Genus = @genus) AND (Subgenus = @subgenus) AND (Species = @species) AND (Subspecies = @subspecies) AND
(IdentificationQualifier = @idqualifier)
It doesn't work because the proper syntax in case of null values should be 'IS NULL' and not '= NULL'.
So instead I am experimenting with CASE WHEN statements, but I am not coming anywhere with things like this:
Code:
SELECT taxid_ID, HigherTaxon, Genus, Subgenus, Species, Subspecies, IdentificationQualifier, IdentificationNotes
FROM TaxonIdentifications
WHERE (HigherTaxon = @hitaxon) AND (Genus = @genus) AND CASE WHEN @subgenus IS NULL THEN (Subgenus IS NULL) ELSE (Subgenus = @subgenus)
END AND (Species = @species) AND CASE WHEN @subspecies IS NULL THEN (Subspecies IS NULL) ELSE (Subspecies = @subspecies) END AND
(IdentificationQualifier = @idqualifier)
Isn't there an easy solution to this that does not require using stored procedures?
Fedor Steeman
Geological Museum Copenhagen
Denmark