?
Solved

Conditional Computed Columns in SQL Server Views

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

750 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