2016-03-03 2 views
-1

Я пытаюсь получить количество из таблицы пунктов заказа и цены из таблицы продуктов, а затем умножить их на @OrderTotal, и функция вернет общую сумму заказа. Я студент и был болен, когда лектор объяснил это, пожалуйста, помогите :)Я пытаюсь сделать заказ всего t-sql

Код:

create function [worksheet02].[udf_getOrderTotal](@orderID Bigint) 
    returns Bigint 
    as 
     begin 
      declare @OrderTotal Bigint 
      declare @quantity integer 
      declare @price integer 
      @quantity = (select quantity 
         from [worksheet02].[tbl_order_items] 
         where order_id = @orderID); 

      @price = (select unit_price 
         from [worksheet02].tbl_order_items oitm 
         join[worksheet02].[tbl_products] prd 
         on(oitm.product_id =prd.product_id) 
         where oitm.order_id = @orderID); 

      @OrderTotal = @quantity * @price; 
      return @orderTotal; 
     end; 
    go 

Ошибка:

 Msg 102, Level 15, State 1, Procedure udf_getOrderTotal, Line 68 
     Incorrect syntax near '@quantity'. 
     Msg 102, Level 15, State 1, Procedure udf_getOrderTotal, Line 72 
     Incorrect syntax near '@price'. 
     Msg 102, Level 15, State 1, Procedure udf_getOrderTotal, Line 78 
     Incorrect syntax near '@OrderTotal'. 
+2

'SET @quantity = ...', 'SET @price = ...', 'SET @OrderTotal = ...' – squillman

ответ

0

Я считаю, что вы просто не хватает набора и выбрать для установки ваши переменные. См. Пример ниже:

CREATE TABLE tbl_order_items 
    (
    order_id int, 
    product_id int, 
    quantity int 
) 

    INSERT INTO tbl_order_items 
    Values (1,1,1) 

    CREATE TABLE tbl_products 
    (
    order_id int, 
    product_id int, 
    unit_price int 
) 

    INSERT INTO tbl_products 
    Values (1,1,2) 

GO 

create function dbo.[udf_getOrderTotal](@orderID Bigint) 
returns Bigint 
as 
    begin 
     declare @OrderTotal Bigint 
     declare @quantity integer 
     declare @price integer 
     set @quantity = (select quantity 
        from tbl_order_items 
        where order_id = @orderID); 

     set @price = (select unit_price 
        from tbl_order_items oitm 
        join tbl_products prd 
        on(oitm.product_id =prd.product_id) 
        where oitm.order_id = @orderID); 

     select @OrderTotal = @quantity * @price 
     return @orderTotal; 
    end 
go 

select dbo.[udf_getOrderTotal](1) 

    drop table tbl_products 
    drop table tbl_order_items 

go 
Смежные вопросы