【问题标题】:SQL Passing tables as parameters?SQL 将表作为参数传递?
【发布时间】:2016-04-13 22:47:01
【问题描述】:

我正在执行需要来自两个不同表的两列的乘法运算,然后在单独的查询中使用结果。我认为这是可能的观点:

SELECT SUM(A.salesAmt * B.sales%) AS rebateAmount

但是在这里可以使用表值函数,还是更合适?表 A 和表 B 是否可以作为参数传递以返回最终的 rebateAmount 作为总和?

  • 表 A 包含:salesID, salesAmt
  • 表 B 包含:salesID, sales%

如果可能的话,是否希望 TVF 返回 (salesAmt * sales%) as rebateAmount 的总和?这是 SQL Server 2014。

提前致谢。

【问题讨论】:

    标签: sql view user-defined-functions sql-server-2014 table-valued-parameters


    【解决方案1】:

    将表格作为参数传递似乎无法解决您的问题。

    相反,似乎有一种更简单的方法: -- 创建临时表 创建表 #a (salesID int, salesAmt decimal(15,2) ) 创建表 #b (salesID int, salesPerc decimal(5,2) )

    -- insert data
    Insert into #a
    Select 1, 2567.34
    Union
    Select 2, 335.57
    Union
    Select 3, 95.35
    Union
    Select 4, 303.83
    Union
    Select 5, 743.66
    
    -- insert data
    Insert into #b
    Select 1, 4.5
    Union
    Select 2, 10.0
    Union
    Select 3, 2.5
    Union
    Select 4, 6.0
    Union
    Select 5, 20.0
    
    -- to get the data of individual columns + the multiple of the salesAmt & salesPerc
    Select a.*, b.salesPerc, convert(decimal(15,2), a.salesAmt * b.salesPerc )                        as Mult from #a a inner join #b b on a.salesID = b.salesID
    
    -- to get the sum of the multiple of the salesAmt & salesPerc
    Select Sum (convert(decimal(15,2), a.salesAmt * b.salesPerc )) as SumOfMult from #a a inner join #b b on a.salesID = b.salesID
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2013-01-16
      相关资源
      最近更新 更多