[英]Why wouldn't we use INNER JOIN on a Table Valued Function?
After searching online I have not been able to find anything that suggests you can or should use INNER JOIN with a table valued function and I'm curious, why? 在线搜索后,我找不到任何建议可以或应该将INNER JOIN与表值函数一起使用的方法,我很好奇,为什么? For example, there is a very common TVF that SSRS report developers use for multi-valued parameters in reports, so they can parse the results as a table.
例如,SSRS报表开发人员使用非常常见的TVF作为报表中的多值参数,因此他们可以将结果解析为表格。
CREATE FUNCTION [dbo].[ParmSplit] (
@List nvarchar(MAX),
@SplitOn nchar(1) )
RETURNS @RtnValue TABLE (
SplitValue nvarchar(250) )
AS
BEGIN
WHILE (CHARINDEX(@SplitOn,@List) > 0)
BEGIN
INSERT INTO @RtnValue (SplitValue)
SELECT SplitValue = LTRIM(RTRIM(SUBSTRING(@List,1,CHARINDEX(@SplitOn,@List) - 1)))
SET @List = SUBSTRING(@List,CHARINDEX(@SplitOn,@List) + LEN(@SplitOn), LEN(@List))
END
INSERT INTO @RtnValue (SplitValue)
SELECT SplitValue = LTRIM(RTRIM(@List))
RETURN
END
Ok so lets imagine we have a states table, and in our report we have a states multi-valued parameter. 好的,假设我们有一个状态表,并且在我们的报告中有一个状态多值参数。 We want to match the states passed in the parameter to states in a table.
我们希望将参数中传递的状态与表中的状态进行匹配。 Here is the states table definition:
这是状态表的定义:
CREATE TABLE #StateTable (
StatID int IDENTITY (1,1)
,StateCode char(2) );
Insert some records: 插入一些记录:
INSERT INTO #StateTable
SELECT 'AL';
INSERT INTO #StateTable
SELECT 'AK';
INSERT INTO #StateTable
SELECT 'AZ';
INSERT INTO #StateTable
SELECT 'AR';
INSERT INTO #StateTable
SELECT 'CA';
INSERT INTO #StateTable
SELECT 'CO';
INSERT INTO #StateTable
SELECT 'CT';
INSERT INTO #StateTable
SELECT 'DE';
INSERT INTO #StateTable
SELECT 'DC';
INSERT INTO #StateTable
SELECT 'FL';
INSERT INTO #StateTable
SELECT 'GA';
INSERT INTO #StateTable
SELECT 'GU';
INSERT INTO #StateTable
SELECT 'HI';
INSERT INTO #StateTable
SELECT 'ID';
INSERT INTO #StateTable
SELECT 'IL';
INSERT INTO #StateTable
SELECT 'IN';
INSERT INTO #StateTable
SELECT 'IA';
INSERT INTO #StateTable
SELECT 'KS';
INSERT INTO #StateTable
SELECT 'KY';
INSERT INTO #StateTable
SELECT 'LA';
INSERT INTO #StateTable
SELECT 'ME';
INSERT INTO #StateTable
SELECT 'MD';
Now query using the TVF and an INNER JOIN 现在使用TVF和INNER JOIN查询
DECLARE @MultiValuedParameter varchar(500) = 'IA, KS, KY, LA, ME, MD, IN, IL'
SELECT
StateCode
FROM #StateTable s
INNER JOIN dbo.ParmSplit(@MultiValuedParameter, ',') p ON s.StateCode = p.SplitValue
The same query using CROSS APPLY. 使用CROSS APPLY进行相同的查询。
DECLARE @MultiValuedParameter varchar(500) = 'IA, KS, KY, LA, ME, MD, IN, IL'
SELECT
StateCode
FROM #StateTable
CROSS APPLY dbo.ParmSplit(@MultiValuedParameter, ',')
WHERE SplitValue = StateCode
The CROSS APPLY requires a WHERE clause which effectively creates an INNER JOIN. CROSS APPLY需要一个WHERE子句,该子句可以有效地创建一个INNER JOIN。
I was always curious about this. 我一直对此感到好奇。 Even though this example only returns a single column, you could still apply this to a multi-column table result.
即使此示例仅返回单列,您仍可以将其应用于多列表结果。 Why when we talk about TVF are we only supposed to use CROSS APPLY?
为什么当我们谈论TVF时,我们只应使用CROSS APPLY?
In reality, you wouldn't/shouldn't do this. 实际上,您不会/不应该这样做。 You are applying a TVF with a static value.
您正在应用具有静态值的TVF。 APPLY (CROSS or OUTER) should be used when the parameter will be dynamic from your dataset.
当参数将根据数据集动态变化时,应使用APPLY(交叉或外部)。
Any who says you need/should to use APPLY with a TVF? 谁说您需要/应该将TVLY与APPLY一起使用? You showed it using a JOIN and I can also see the value is using it as a sub-query.
您使用JOIN展示了它,我还可以看到该值正在将它用作子查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.