473,809 Members | 2,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IF..ELSE in function - unknown error

Hi all,

I have the below user-defined function on mssql 2000 and I can't work
out why i'm getting the following error:
-----
Server: Msg 156, Level 15, State 1, Procedure
fnCalculateOutw orkerPaymentFor Box, Line 15
Incorrect syntax near the keyword 'IF'.
Server: Msg 170, Level 15, State 1, Procedure
fnCalculateOutw orkerPaymentFor Box, Line 23
Line 23: Incorrect syntax near ')'.
-----
-----
CREATE FUNCTION fnCalculateOutw orkerPaymentFor Box(@boxid int)
RETURNS money

AS
BEGIN
RETURN (
/* if the box is a paperback */
IF (SELECT COUNT(BoxID) AS NoOfBoxes FROM OutworkerBoxes WHERE BoxID
= @boxid AND BoxCode LIKE '%PAPER%') > 1

/* If the books are paperback, charge 15p each and add on 30p for a
description book to make 45p */
SELECT ((endref - StartRef) * 0.15) + (NoOfDescriptio nBooks * 0.30)
FROM OutworkerBoxes WHERE BoxID = @boxid
ELSE
/* If the books are normal, charge 25p each and add 20p on for
description books to make 45p */
SELECT ((endref - StartRef) * 0.25) + (NoOfDescriptio nBooks * 0.20)
FROM OutworkerBoxes WHERE BoxID = @boxid

)

END
-----

Below is the sql for the table it works with:

-----
CREATE TABLE [OutworkerBoxes] (
[BoxID] [int] IDENTITY (1, 1) NOT NULL ,
[OutworkerID] [int] NOT NULL ,
[ImportedBy] [int] NULL ,
[StartRef] [int] NOT NULL ,
[endref] [int] NOT NULL ,
[DateIssued] [datetime] NOT NULL ,
[BoxCode] [nvarchar] (50) COLLATE Latin1_General_ CI_AS NOT NULL ,
[DealerID] [int] NULL ,
[StatusID] [int] NOT NULL ,
[IssuedBy] [int] NOT NULL ,
[BoxNotes] [nvarchar] (200) COLLATE Latin1_General_ CI_AS NULL ,
[DateImported] [datetime] NULL ,
[NoOfDescription Books] [int] NOT NULL CONSTRAINT
[DF_OutworkerBox es_NoOfDescript ionBooks] DEFAULT (0),
CONSTRAINT [PK_OutworkerBox es] PRIMARY KEY CLUSTERED
(
[BoxID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO

-----

If anyone can advise me i'd be most grateful.

Thanx in advance

James

Jul 23 '05 #1
2 5571
IF is a control-of-flow statement so you can't specify it as a RETURN
expression. Below are a couple of untested examples that show how you can
return the desired expression.

CREATE FUNCTION fnCalculateOutw orkerPaymentFor Box(@boxid int)
RETURNS money
AS
BEGIN
DECLARE @BoxCount int
DECLARE @Result money
SELECT @BoxCount = COUNT(BoxID)
FROM OutworkerBoxes
WHERE BoxID = @boxid AND BoxCode LIKE '%PAPER%'

IF @BoxCount > 1
SELECT @Result = ((endref - StartRef) * 0.15) + (NoOfDescriptio nBooks *
0.30)
FROM OutworkerBoxes
WHERE BoxID = @boxid
ELSE
SELECT @Result = ((endref - StartRef) * 0.25) + (NoOfDescriptio nBooks *
0.20)
FROM OutworkerBoxes
WHERE BoxID = @boxid
RETURN @Result

END
GO

ALTER FUNCTION fnCalculateOutw orkerPaymentFor Box(@boxid int)
RETURNS money
AS
BEGIN
DECLARE @BoxCount int
SELECT @BoxCount = COUNT(BoxID)
FROM OutworkerBoxes
WHERE BoxID = @boxid AND BoxCode LIKE '%PAPER%'

RETURN(
SELECT ((endref - StartRef) *
CASE WHEN @BoxCount > 1 THEN 0.15 ELSE 0.25 END
) + (NoOfDescriptio nBooks *
CASE WHEN @BoxCount > 1 THEN 0.30 ELSE 0.20 END)
FROM OutworkerBoxes
WHERE BoxID = @boxid
)
END
GO
--
Hope this helps.

Dan Guzman
SQL Server MVP

"phaser2001 " <ph********@hot mail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi all,

I have the below user-defined function on mssql 2000 and I can't work
out why i'm getting the following error:
-----
Server: Msg 156, Level 15, State 1, Procedure
fnCalculateOutw orkerPaymentFor Box, Line 15
Incorrect syntax near the keyword 'IF'.
Server: Msg 170, Level 15, State 1, Procedure
fnCalculateOutw orkerPaymentFor Box, Line 23
Line 23: Incorrect syntax near ')'.
-----
-----
CREATE FUNCTION fnCalculateOutw orkerPaymentFor Box(@boxid int)
RETURNS money

AS
BEGIN
RETURN (
/* if the box is a paperback */
IF (SELECT COUNT(BoxID) AS NoOfBoxes FROM OutworkerBoxes WHERE BoxID
= @boxid AND BoxCode LIKE '%PAPER%') > 1

/* If the books are paperback, charge 15p each and add on 30p for a
description book to make 45p */
SELECT ((endref - StartRef) * 0.15) + (NoOfDescriptio nBooks * 0.30)
FROM OutworkerBoxes WHERE BoxID = @boxid
ELSE
/* If the books are normal, charge 25p each and add 20p on for
description books to make 45p */
SELECT ((endref - StartRef) * 0.25) + (NoOfDescriptio nBooks * 0.20)
FROM OutworkerBoxes WHERE BoxID = @boxid

)

END
-----

Below is the sql for the table it works with:

-----
CREATE TABLE [OutworkerBoxes] (
[BoxID] [int] IDENTITY (1, 1) NOT NULL ,
[OutworkerID] [int] NOT NULL ,
[ImportedBy] [int] NULL ,
[StartRef] [int] NOT NULL ,
[endref] [int] NOT NULL ,
[DateIssued] [datetime] NOT NULL ,
[BoxCode] [nvarchar] (50) COLLATE Latin1_General_ CI_AS NOT NULL ,
[DealerID] [int] NULL ,
[StatusID] [int] NOT NULL ,
[IssuedBy] [int] NOT NULL ,
[BoxNotes] [nvarchar] (200) COLLATE Latin1_General_ CI_AS NULL ,
[DateImported] [datetime] NULL ,
[NoOfDescription Books] [int] NOT NULL CONSTRAINT
[DF_OutworkerBox es_NoOfDescript ionBooks] DEFAULT (0),
CONSTRAINT [PK_OutworkerBox es] PRIMARY KEY CLUSTERED
(
[BoxID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO

-----

If anyone can advise me i'd be most grateful.

Thanx in advance

James

Jul 23 '05 #2
Thanx - i just tried the second suggestion and it works a treat :-) The
only thing was that in the below section the boxcount had to be more
than 0, i.e. boxes existed, instead of one.

----
CASE WHEN @BoxCount > 1 THEN 0.15 ELSE 0.25 END
) + (NoOfDescriptio nBooks *
CASE WHEN @BoxCount > 1 THEN 0.30 ELSE 0.20 END)
----

Many thanks for your prompt reply!

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
381
by: Jakob Vesterstrom | last post by:
Hi all I have question regarding function passing. I have class A class A { public: ... void attach(double f(double in)) {
4
3354
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added and gives the error message (in Borland C++Builder 6) Unit1.cpp E2285 Could not find a match for
3
5243
by: Torrent | last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown function." It was annoying because I had checked created the whole document and tested it in Internet Explorer 6.0 and everything worked perfectly; no errors or warnings. After doing research i found out that most often times the error that i was...
2
7486
by: Johann Robette | last post by:
Hi, I'm trying to call the array_to_string function like this : SELECT array_to_string(array, '~^~') --> it comes directly from the doc. I get this error msg : ERROR: parser: parse error at or near ", text) does not exist Unable to identify a function that satisfies the given argument types
9
2987
by: Christian Christmann | last post by:
Hi, I was just going through this exercise http://www.cas.mcmaster.ca/~franek/books/membook-answers/ch4/answers-ch4-3.html and I'am confused about the answer. It says: "... the compiler actually does not "know" the signature of malloc(), hence it assumes by default, that it returns int..." How can the function call of 'malloc' work at all if it is unknown?
2
7705
by: news | last post by:
I just upgraded to PHP 4.4.2 on my Slackware 10.2 system. And Apache/mySQL/PHP all work great through a browser. No errors. But when I try to run a PHP script through the command line, which I need to do, I get blocks of errors like: root@slackserve:/var/www/htdocs# php ./phptest.php PHP Warning: Unknown(): Unable to load dynamic library '/usr/lib/php/extensions/mysql.so' - libmysqlclient.so.14: cannot open shared object file: No such...
2
1910
by: Qingning Huo | last post by:
Hi, Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun CC 5.8. --cut -- template<class T> class TClass { public:
4
1830
by: lostlander | last post by:
In ARMCC, and Microsoft C, when i use a function which is never defined or delared, it gives out a warning, not a compiling error? why? (This leads to a bug to my program since I seldom pay much attention to warnings...) Thanks for explanation!
2
7398
by: Calvin Cheng | last post by:
Hi, I am attempting to convert a bunch of .txt files into html using the docutils package. It works for most of the txt files except for the index.txt file which gives 2 errors: (1) <Error/3Unknown Directive type "toctree" (2) (ERROR/3) Unknown interpreted text role "ref".
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.