我正在尝试做一个订单总t-sql

时间:2016-03-03 17:37:08

标签: tsql

我试图从订单商品表中获取数量,然后从products表中获取价格,然后将它们乘以@OrderTotal,该函数将返回订单总数。我是一名学生,当讲师解释这些请帮助时生病了。)

代码:

gcloud compute firewall-rules create allow-traffic-to-es --allow tcp:80,tcp:443 --source-ranges 0.0.0.0/0 --target-tags es

错误:

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

1 个答案:

答案 0 :(得分: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