Sql 在select查询中使用SET语句
这是我的设想。我有一个具有select查询的存储过程Sql 在select查询中使用SET语句,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,这是我的设想。我有一个具有select查询的存储过程 Select M ,X , Y, dbo.function(parameters) as Z from SomeTable where condition1 and condition2 and Z > SomeValue where子句中的Z是函数的计算值 函数始终返回单个值 我不想在where子句中使用函数。我想将该值设置为一个变量,并在where子句中使用该变量 如何做到这一点?请帮忙。你必须做以下事情 DECLARE
Select M ,X , Y, dbo.function(parameters) as Z
from SomeTable
where condition1
and condition2
and Z > SomeValue
where子句中的Z是函数的计算值
函数始终返回单个值
我不想在where子句中使用函数。我想将该值设置为一个变量,并在where子句中使用该变量
如何做到这一点?请帮忙。你必须做以下事情
DECLARE @Z TYPE_GOES_HERE = dbo.function(parameters)
Select M ,X , Y, @Z as Z
from SomeTable
where condition1
and condition2
and @Z > SomeValue
;WITH Vals AS (
Select M ,X , Y, dbo.function(parameters) as Z
from SomeTable
)
SELECT *
FROm Vals
where condition1
and condition2
and Z > SomeValue
只有当传递给函数的参数不是表中的列时,这才有效。如果它是独立的,你可以使用CTE。差不多
DECLARE @Z TYPE_GOES_HERE = dbo.function(parameters)
Select M ,X , Y, @Z as Z
from SomeTable
where condition1
and condition2
and @Z > SomeValue
;WITH Vals AS (
Select M ,X , Y, dbo.function(parameters) as Z
from SomeTable
)
SELECT *
FROm Vals
where condition1
and condition2
and Z > SomeValue
对于您的需求,您可以在select中使用select,STARDY=)
请显示您的函数和您的总SPI如果函数是确定性的,那么无论它在查询中以文本形式出现多少次,它应该只为每行调用一次。