创建函数SQL-Server错误语法

时间:2019-04-05 11:22:05

标签: sql-server tsql stored-functions

我是SQL Sg服务器的新手,正在尝试使用create函数运行代码,以将美元的价格转换为小数。我收到以下标志

  

“语法错误:'创建函数'必须是   批处理。

有人能指出我错了吗?谢谢!

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]

3 个答案:

答案 0 :(得分:2)

两个语句都不同,您可以单独运行,也可以放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];

答案 1 :(得分:1)

尝试如下

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]

答案 2 :(得分:1)

尝试创建as语句,并在语句之间添加go

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]