Be careful with CASE and NULL statement

Checking NULL values in database can be a little tricky. It is very easy to trickle those if you do not provide correct syntax. The recommend way to check null is ISNULL(myfieldname,”). When using NULL in case statement, it might run without any error but will give you in correct result. Consider this example

Script to create #temp table. Note that # in front of tablename means it is temporary table. it will be automatically destroyed once you close you window.

Now lets say you want to pull column A value if it is not empty and column B value if column A is empty. You might want to write something like this which is incorrect

This basically pulls column A value and does not bother about column B value at all. Here is the result

The correct syntax to populate column C with either A or B depending which one has value is

Here is the result

executenonquery returns -1 on success

ExecuteNonQuery is suppose to return the number of rows inserted, updated or deleted but in my case it always return -1 even though the record is successfully added.

Found out


was missing my in my stored procedure. If you have multiple SQL statements in your stored procedure, initially set it to on by


which will not count any rows affected. Later to monitor the statement you are actually interested in, set it to OFF.

Hope that resolves your problem!