I'm using C# to write to a SQL Compact Edition 3.5 database. I got a table containing e-mail addresses and names for each address.
MailRecipientAddressID int primary key identity(1,1) not null,
Address nvarchar(4000),
Name nvarchar(4000)
In this table I want every address-name combination to be unique. In some cases it's possible that either Address
or Name
is NULL
in a row. Before inserting new rows into this table, I'm using a SELECT
query to check if there is an existing row matching the one I want to insert. When using this query
SELECT MailRecipientAddressID FROM MailRecipientAddress WHERE Address = @Address AND Name = @Name
I will not find existing rows with NULL
values in one column (see here).
Now I got this query, which works and kind of solves my problem
SELECT MailRecipientAddressID FROM MailRecipientAddress WHERE ISNULL(Address, '') = ISNULL(@Address, '') AND ISNULL(Name, '') = ISNULL(@Name, '')
but even though it is no problem in my case that NULL
and empty string values are handled equally, I do not like this solution. I think it's kind of hackish. Is there a better approach
- to apply a filter on a SELECT statement with parameters which can contain NULL
- which works on SQL CE
Edit
I do not understand why, but my query works with SQL Management Studio but it does not in my application (see here). To correct my own approach I would need to use COALESCE.
I don't like the option to replace my NULL
values with empty strings because I think it would be kind of inconsequent to set a value at a place where I got no value or is my understanding of this design question wrong?