The error message Msg 8133, Level 16, State 1, Line 2 None of the result expressions in a CASE specification can be NULL. is a run time error message and may be generated at all versions of SQL Srever. The said error message is generated when one result expression in a CASE expression is NULL. To remove the error message, analyze your case statement and modify it to make sure that no expression is NULL.
Column names are commonly used in order by clause of any select statement in T-SQL. For example a simple select statement in AdventureWorks database using order by clause is as follows.
ORDER BY Groupname
Result is obviously three columns ordered by [GroupName]. Let us use another more simple approach while using order by clause. Instead of using column name, we may bring order to our results by using integer in order by clause. For example ordering our result set through integer, we have following way.
Result of both of our scripts is in same order. SQL Server starts count of columns used in select list by 1 and increments by 1. In case of above scripts used, as we have three columns in select list, so we may use integer value from 1 to 3 for first, second and third column respectively. Value provided may not be less than 1. And also it may not be greater than number of columns used in select list. As in our case maximum value is 3.
This syntax of order by using integers may be used in all versions of SQL Server. It is simple than using full column names. Changing the order of columns in select list will also change the criteria being used in order by clause.
A short coming is there in this approach. By using integer in order by clause you can not order by any column that is not in select list. While by using column names in order list you can order even by the column that is not in select list.
Since NULL is an unknown entity, the result of comparing two unknowns is always false. This means that the PRINT statements are never executed as the result of the IF statement is always false.
Ref: IF..ELSE – http://msdn.microsoft.com/en-us/library/ms182717.aspx
Note: The Question is taken from SQLServercentral.com.
Question: Which of the following operations can be performed by MERGE statement in SQL Server 2008?
WHEN NOT MATCHED BY TARGET
WHEN NOT MATCHED BY SOURCE
Explanation: Merge is used to check if values match in both Source and Target, which is equal to WHEN MATCHED and an UPDATE is fired. When value exist in SOURCE table but not in TARGET table then WHEN NOT MATCHED [ BY TARGET ] then INSERT the new value and if the value exist in TARGET and not in SOURCE then WHEN NOT MATCHED [BY SOURCE] and DELETE is fired.
Until SQL Server 2000 T-SQL, we have to use multiple insert statements for inserting multiple rows of data in a table. For SQL Server 2005 T-SQL we can insert multiple rows by using a single insert statement. And for SQL Server 2008 T-SQL we have two methods to insert multiple rows by using a single insert statement.
In following example we will go through the statements used in SQL Server 200 T-SQL, SQL Server 2005 T-SQL, SQL Server 2008 T-SQL to insert multiple rows by using single insert statement.