Solved

Conditional Computed Columns in SQL Server Views

Posted on 2013-06-28
4
477 Views
Last Modified: 2013-06-29
I am trying to create a SQL Server 2008 View from an existing Table Students with the following Query it gives me an error mentioned below (Also attached)

SELECT Std_ID, Name, Sub1, Sub2, IIf ((Sub1 + Sub2)>160, 'A',IIf ((Sub1 + Sub2)>100, 'B','C')) As Legend FROM dbo.Student

Error in list of function arguments: '>' not recognized. Unable to parse query text.

I am expecting the view with the calculated field 'Legend'

Query execute perfectly in Query Designer but view is not created.

Please advice
error.jpg
0
Comment
Question by:jkvaman
4 Comments
 
LVL 8

Assisted Solution

by:didnthaveaname
didnthaveaname earned 125 total points
Comment Utility
I feel like you can't use IIF in a view.  Why don't you try it in a case statement and see if that works?

SELECT 
   Std_ID, 
   Name, 
   Sub1, 
   Sub2, 
   case
      when (Sub1 + Sub2) > 160 then 'A'
      when (Sub1 + Sub2) > 100 then 'B'
      else 'C'
   end as Legend 
FROM 
   dbo.Student;

Open in new window

0
 
LVL 5

Expert Comment

by:DOSLover
Comment Utility
The statement looks fine. Can you please post the complete 'Create View' statement?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 125 total points
Comment Utility
SQL Server 2012 IIF is a shorthand way for writing a CASE expression.
use the case expressions provided by didnthaveaname

try: IIF in 2008 (don't work)
try: IIF in 2012 does work

I personally would use case expressions anyway :)
0
 

Author Closing Comment

by:jkvaman
Comment Utility
Thanks for the quick response, unfortunately we cannot to upgrade to version 2012. So better  use Case statement suggested by  didnthaveaname. I would also accept the answer from  PortletPaul as this gives a complete solution.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now