Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conditional Computed Columns in SQL Server Views

Posted on 2013-06-28
4
Medium Priority
?
517 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 500 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

618 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