SQL Server 不使用星号(*)进行乘法操作。

huangapple go评论63阅读模式
英文:

SQL Server Multiply Without Asterisk (*)

问题

我正在使用一个资产管理系统,允许我创建图表。我试图让一个SQL查询在其中工作,但显然他们不希望执行全选查询,所以他们设置了一个条件,阻止了select语句中的星号。然而,我想要将订单数量乘以订单金额,以提供一个显示每个预算花费多少钱的图表。

如何绕过他们的限制,将价格乘以数量的值添加到我的查询中?

select budget, price * qty as 'amount spent'
from orderline
英文:

I'm working in an asset management system that allows me to create charts. I'm attempting to get a SQL query to work in it, and apparently they don't want a select all query performed, so they have a condition that prevents the asterisk in the select statement. However, I want to multiply order quantity by the order amount to provide a chart showing how much money is spent per budget.

How can I bypass their stupidity to get that price times quantity value into my query?

select budget, price * qty as 'amount spent'
from orderline

答案1

得分: -1

我同意这个限制很荒谬。但如果您控制模式,您可以创建一个用户定义的标量函数来执行它。

CREATE FUNCTION MULTIPLY
(
	@param1 AS FLOAT,
	@param2 AS FLOAT
)
RETURNS FLOAT
WITH SCHEMABINDING
AS
BEGIN
	RETURN @param1 * @param2
END

然后:

SELECT budget, dbo.MULTIPLY(price,qty) as 'amount spent' FROM orderline
英文:

I agree this limitation is absurd. If you have control of the schema though, you could create a user-defined scalar function to do it.

CREATE FUNCTION MULTIPLY
(
	@param1 AS FLOAT,
	@param2 AS FLOAT
)
RETURNS FLOAT
WITH SCHEMABINDING
AS
BEGIN
	RETURN @param1 * @param2
END

then:

SELECT budget, dbo.MULTIPLY(price,qty) as 'amount spent' FROM orderline

huangapple
  • 本文由 发表于 2023年7月20日 22:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定