How can you use IIf in Transact-SQL?
2008-01-08 09:04
441 查看
This is a quite common question. It is usually asked by people arriving at SQL Server with a background in Microsoft Access. They either want to use SQL Server as a backend for their Access project, or they are otherwise upsizing from Access to SQL Server. The answer, however, is usually not much appreciated at first: There is no IIf in SQL Server's Transact SQL language! Like it or not, such queries have to be rewritten using the CASE expression. Let's look at a simple example:
SELECT Customers.CustomerID , Customers.CompanyName , Customers.Country , IIf([Country]="Germany","0049 " & [Phone],[Phone]) AS Telefon FROM Customers
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
This is a valid query in Access, which evaluates within Access' Northwind sample database whether a Customer is located in Germany or not. If this is the case (pun intended!), it automatically adds the international telephone number for Germany in front of the phone number. If you try to run this in SQL Server's Query Analyzer, you'll get:
Server: Msg 170, Level 15, State 1, Line 5 Line 5: Incorrect syntax near '='.
That's it. The query stops with this error message. So, as was mentioned above, the query has to be rewritten using the CASE expression. That might look something like this:
SELECT Customers.CustomerID , Customers.CompanyName , Customers.Country , CASE WHEN Country='Germany' THEN '0049 ' + Phone ELSE Phone END AS Phone FROM Customers
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
This is a valid Transact-SQL query, which SQL Server can understand and execute.
CASE is one of the most powerful commands in the Transact-SQL language. In contrast to IIf, where you only evaluate one logical expression at a time, this limitation does not exist for CASE. Try, for example, to put this in one single IIf expression:
SELECT Customers.CustomerID , Customers.CompanyName , Customers.Country , CASE Country WHEN 'Germany' THEN '0049 ' + Phone WHEN 'Mexico' THEN 'Fiesta ' + Phone WHEN 'UK' THEN 'Black Pudding (Yuk!) ' + Phone ELSE Phone END AS Phone FROM Customers
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Don't spent too much time here on the sense of this query, but you will get the idea of what is possible with CASE. And once you are familiar with using CASE, you'll hardly miss IIf anymore.
相关文章推荐
- After restoring database in SQL Server, if you can't use diagram, execute these sentences
- Question[SQL]:Can you create a cross-tab report in my SQL Server!
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
- How You Can Cash In On The Green Gold Rush of 2014
- How do I use a variable in a TOP clause in SQL Server?
- MySql 执行语句错误 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
- 60款开源云应用【Part 2】(60 Open Source Apps You Can Use in the Cloud)
- How to use a 32bit Oracle11_g client in 64 win system and not conflict with sqldeveloper 64 bit tool
- How can I connect Unity to an SQL database in order to implement an MMO?
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- How to Manage and Use LVM (Logical Volume Management) in Ubuntu In our previous article we told you
- How can you decide to use UINavigationBar, UIToolbar and UITabbar
- How can I use wget in Windows
- 插入mysql语句报错:1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
- How you can use qemu/kvm base images to be more productive
- [转]How do I use variables in Oracle SQL Developer?
- How To Use Dynamic Sql in Sql Server ?
- MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause
- Question[SQL]:Can you use a SQL statement to calculating it!