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
fnCalculateOutworkerPaymentForBox, Line 15
Incorrect syntax near the keyword 'IF'.
Server: Msg 170, Level 15, State 1, Procedure
fnCalculateOutworkerPaymentForBox, Line 23
Line 23: Incorrect syntax near ')'.
-----
-----
CREATE FUNCTION fnCalculateOutworkerPaymentForBox(@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) + (NoOfDescriptionBooks * 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) + (NoOfDescriptionBooks * 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 ,
[NoOfDescriptionBooks] [int] NOT NULL CONSTRAINT
[DF_OutworkerBoxes_NoOfDescriptionBooks] DEFAULT (0),
CONSTRAINT [PK_OutworkerBoxes] 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 2 5535
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 fnCalculateOutworkerPaymentForBox(@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) + (NoOfDescriptionBooks *
0.30)
FROM OutworkerBoxes
WHERE BoxID = @boxid
ELSE
SELECT @Result = ((endref - StartRef) * 0.25) + (NoOfDescriptionBooks *
0.20)
FROM OutworkerBoxes
WHERE BoxID = @boxid
RETURN @Result
END
GO
ALTER FUNCTION fnCalculateOutworkerPaymentForBox(@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
) + (NoOfDescriptionBooks *
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********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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 fnCalculateOutworkerPaymentForBox, Line 15 Incorrect syntax near the keyword 'IF'. Server: Msg 170, Level 15, State 1, Procedure fnCalculateOutworkerPaymentForBox, Line 23 Line 23: Incorrect syntax near ')'. ----- ----- CREATE FUNCTION fnCalculateOutworkerPaymentForBox(@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) + (NoOfDescriptionBooks * 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) + (NoOfDescriptionBooks * 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 , [NoOfDescriptionBooks] [int] NOT NULL CONSTRAINT [DF_OutworkerBoxes_NoOfDescriptionBooks] DEFAULT (0), CONSTRAINT [PK_OutworkerBoxes] 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
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
) + (NoOfDescriptionBooks *
CASE WHEN @BoxCount > 1 THEN 0.30 ELSE 0.20 END)
----
Many thanks for your prompt reply! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)) {
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
|
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...
|
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)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |