Solved

Conditional Computed Columns in SQL Server Views

Posted on 2013-06-28
4
483 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
ID: 39286119
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
ID: 39286144
The statement looks fine. Can you please post the complete 'Create View' statement?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 125 total points
ID: 39286176
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
ID: 39286461
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

25 Experts available now in Live!

Get 1:1 Help Now