sql - 如何在 SQL 中声明和使用变量
问题描述
这是我的查询 - 请帮助我了解如何在以下查询中使用变量 [标有粗体的变量]
create view cost_sheetNO (cost_No, line_No, EC, ATE, SP, E_GMValue, E_GMperc)
as
select
Vcost_sheet_line.[Cost Sheet No_],
Vsales_line.[Costsheet No_],
Vcost_sheet_line.[Landing Price],
(select SUM(Vcost_sheet_line.[Landing Price])
from Vcost_sheet_line
where Vsales_line.[Costsheet No_] = Vcost_sheet_line.[Cost Sheet No_]),
Vcost_sheet_line.[List Price] * Vcost_sheet_line.[Special Vendor Discount _],
(select declare @a decimal = sales_headerSO.OV - sales_headerSO.EC
from sales_headerSO),
(select @a - sales_headerSO.EC
from sales_headerSO)
from
Vcost_sheet_line, Vsales_line
where
Vsales_line.[Costsheet No_] = Vcost_sheet_line.[Cost Sheet No_]
谢谢
解决方案
视图没有参数,但您可以实现表格函数(您的代码看起来像 SQL Server)以根据参数获得相同的结果。
create function cost_sheetNO (@a decimal)
returns table
return (
select Vcost_sheet_line.[Cost Sheet No_], Vsales_line.[Costsheet No_], Vcost_sheet_line.[Landing Price],
(select SUM(Vcost_sheet_line.[Landing Price])
from Vcost_sheet_line
where Vsales_line.[Costsheet No_] = Vcost_sheet_line.[Cost Sheet No_]),
Vcost_sheet_line.[List Price]*Vcost_sheet_line.[Special Vendor Discount _],
(select @a - sales_headerSO.EC from sales_headerSO)
from Vcost_sheet_line, Vsales_line
where Vsales_line.[Costsheet No_] = Vcost_sheet_line.[Cost Sheet No_]
)
推荐阅读
- python - 根据 pandas 中的某些条件删除特定行
- .net-core - DotNet Nuget Linux:无法加载服务索引
- c - 全屏分辨率和glfw的一些问题
- ios - Microblink:读取图像
- sharepoint-online - 在没有 MS 流的情况下将 MS 表单连接到 SharePoint 列表
- c++ - cin.ignore() 不能删除输入缓冲区
- mongodb - Mongo查询以查找具有包含非空值的数组元素的文档,这些值都不匹配指定值
- python - 如何在运行时在 Pandas DataFrame 上构建语句以执行 groupby 操作?
- c - 为什么我的 for 循环使用 "&&" 而不是 ","
- docker - Dockerfile 似乎没有暴露端口