Solved

Conditional Computed Columns in SQL Server Views

Posted on 2013-06-28
4
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

691 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