Problem with a scalar function

I am attempting to provide a custom function that should replace provide an IIF function (Access SQL) in an upsized database.

I have defined a new scalar function as follows:

      -- Add the parameters for the function here
      @check bit,
      @TrueVal sql_variant,
      @FalseVal sql_variant
RETURNS sql_variant
      -- Declare the return variable here
      DECLARE @Result sql_variant

      -- Add the T-SQL statements to compute the return value here
      SET @Result = CASE WHEN @check<>0 THEN @TrueVal ELSE @FalseVal END
      -- Return the result of the function
      RETURN @Result


I want the IIF function to take 3 parameters, a boolean value, a value for the true result and a value for the false result.  (the results can be strings, numbers... so I have declared them as variants and require the function to return a variant).

The function compiles (Verify SQL Syntax) without problem.

In a view I have the following:
SELECT      dbo.IIF(dbo.Fibu.[Beleg ID]<600, "Lower", "Upper") AS Expr1 FROM.....

If I then use the 'Verify SQL Syntax' option I get an error - Incorrect syntax near '<'

Just for experimentation I then modified that to
SELECT      dbo.IIF(dbo.Fibu.[Beleg ID], "Lower", "Upper") AS Expr1 FROM.....
and now get the following errors:- Invalid Column Name 'Lower', Invalid Column Name 'Upper'

Obviously I have a serious misunderstanding.
1) Can I add a scalar function to the database and then call it inside a view?
2) Assuming the answer to 1) is yes then what is wrong with my attempt above.
LVL 46
AndyAinscowFreelance programmer / ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about:
SELECT      dbo.IIF( (dbo.Fibu.[Beleg ID]<600) , "Lower", "Upper") AS Expr1 FROM.....

Open in new window

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>1) Can I add a scalar function to the database and then call it inside a view?

the "problem" is that the expression is not necessarily considered "boolean"
TimCotteeHead of Software ServicesCommented:
Hello AndyAinscow,

Let's take 2) first, the problem here is that you are using " rather than ' around Lower and Upper so as you do not presumably have quoted_identifier set it will be looking for these as column names rather than string literals.

Now to the more complicated one, basically what you are trying to achieve is extremely difficult if not impossible. Sql doesn't evaluate an expression as such and return the boolean result. This means that you would either have to pass the expression as a string and then use dynamic sql to evaluate it (could be tricky when you are also passing a column value in!)

All in all it is probably best to simply convert your IIF() functions directly to case statements within the view rather than trying to replicate the IIF() function with a UDF.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

AndyAinscowFreelance programmer / ConsultantAuthor Commented:
OK, some progress.

I have changed the statement to
 dbo.IIF(dbo.FIBU.[Beleg ID], 'Lower', 'Upper') AS Expr1

This compiles correctly and does 'run' (but not exactly what I desire as a result).

Changing to
 dbo.IIF((dbo.FIBU.[Beleg ID]<600), 'Lower', 'Upper') AS Expr1
still results in the error Incorrect Syntax near '<'

>>All in all it is probably best to simply convert your IIF() functions directly to case statements within the view
Er, yes but.  I am upsizing an existing database.  There are many rather complex queries that use an IIF statement.  The first (alphabetically) actually has 5 different IIF statements so I hoped I could take a simpler approach by supplying my own function to directly replace the IIF function.

Any further ideas?
Guy Hengel [angelIII / a3]Billing EngineerCommented:

dbo.IIF( CASE WHEN dbo.FIBU.[Beleg ID]<600 THEN 1 ELSE 0 END, 'Lower', 'Upper') AS Expr1

Open in new window

AndyAinscowFreelance programmer / ConsultantAuthor Commented:
OK, I asked for that.
That looks as if it should work, however it rather defeats the purpose of have the custom function doesn't it.

I suspect this isn't going to go anywhere.  :-(
I think I have a few hours of mind numbing drudgery ahead of me, aaargh!
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I see what you mean :)
and now, I remember I had more or less the same dilemma, and could not find any solution (in sql 2000-2005). I did not yet check in sql 2008, though.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.