1

I am new to SQL Sg server and am trying to run code using the create function to convert a price fro USD to decimal. I'm getting the following flag

"Incorrect Syntax: 'Create Function' must be the only statement in the batch.

Can anyone point out what I'm dong wrong? Thank you!

CREATE FUNCTION week3.ConvertToEuro ( @priceUSD decimal(5,2))
returns decimal (5,2)

begin
        declare @PriceEuro decimal (5,2)
        set @PriceEuro =(@priceUSD * .89)
        return @PriceEuro

        end 

SELECT TOP (1000) [ProductName]
      ,[price]
      ,week3.ConvertToEuro(price) as PriceEuro
  FROM [AA5100_edmistonml].[Week3].[productprice]
4
  • Unless you're using SQL Server 2019 (and making use of inline scalar functionality), I would recommend an inline table-value function. Scalar function can be slow. Commented Apr 5, 2019 at 11:25
  • Exchange rate is likely to change, so better to make it a parameter (or query from up-to-date table). Commented Apr 5, 2019 at 11:27
  • @Larnu. . . Side note SQL Server 2019 is not yet released. Commented Apr 5, 2019 at 11:29
  • @YogeshSharma it's now in CTP 2.4, and (the preview) has been publically available for some time. Of course, that's why I stated "unless you're using 2019"; with the high likelihood they are not (especically as 2.4 is now only available as an evaluation version, and not developement/express). Commented Apr 5, 2019 at 11:33

4 Answers 4

2

Both statements are different you either run separate or put GO :

CREATE FUNCTION week3.ConvertToEuro ( @priceUSD decimal(5,2))
returns decimal (5,2)
begin
        declare @PriceEuro decimal (5,2)
        set @PriceEuro =(@priceUSD * .89)
        return @PriceEuro
end 

GO

SELECT TOP (1000) [ProductName], [price], week3.ConvertToEuro(price) as PriceEuro
FROM [AA5100_edmistonml].[Week3].[productprice];
Sign up to request clarification or add additional context in comments.

Comments

1

try like below

CREATE FUNCTION week3.ConvertToEuro ( @priceUSD decimal(5,2))
returns decimal (5,2)
as -- add this 
begin
        declare @PriceEuro decimal (5,2)
        set @PriceEuro =(@priceUSD * .89)
        return @PriceEuro

        end 

go -- add this
SELECT TOP (1000) [ProductName]
      ,[price]
      ,week3.ConvertToEuro(price) as PriceEuro
  FROM [AA5100_edmistonml].[Week3].[productprice]

1 Comment

Hey Zaynul... Congratulations for being awarded the sql gold badge today... that's a great achievement, cheers!
1

Try by creating as statement and add go between statements

CREATE FUNCTION week3.ConvertToEuro ( @priceUSD decimal(5,2))
returns decimal (5,2)
as  
begin
        declare @PriceEuro decimal (5,2)
        set @PriceEuro =(@priceUSD * .89)
        return @PriceEuro

        end 

go  
SELECT TOP (1000) [ProductName]
      ,[price]
      ,week3.ConvertToEuro(price) as PriceEuro
  FROM [AA5100_edmistonml].[Week3].[productprice]

Comments

0

My fix was to put GO on first and last line

GO 
CREATE FUNCTION [dbo].[SplitString]
(
    @List NVARCHAR(MAX),
    @Delim VARCHAR(255)
)
RETURNS TABLE
AS
    RETURN ( SELECT [Value] FROM 
      ( 
        SELECT 
          [Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
          CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
        FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
          FROM sys.all_objects) AS x
          WHERE Number <= LEN(@List)
          AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim
      ) AS y
    );
GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.