MS SQL server 2008 r2
尝试创建一个使用表变量的sql函数。当我尝试创建函数时,它不断抛出错误。它要么抱怨我需要声明标量变量@LocationUpdate,要么我使用表别名LU它抱怨多部分标识符lu.ID不能被绑定。
任何人都知道我做错了什么?我之前有一个sql语句插入到表变量中,并且它没有抛出任何错误。我有一种感觉,我做的事情很愚蠢 - 但我没有看到它。
DECLARE @LocationUpdate TABLE (
[ID] [int] NOT NULL,
[FacilityName] [nvarchar](500) NULL,
[TrialUpdateID] [int] NOT NULL,
[StatusID] [int] NULL,
[City] [nvarchar](100) NULL,
[StateID] [int] NULL,
[Zip] [nvarchar](100) NULL,
[CountryID] [int] NULL,
[ContactName] [nvarchar](500) NULL,
[ContactPhone] [nvarchar](500) NULL,
[ContactPhoneExt] [nvarchar](500) NULL,
[ContactEmail] [nvarchar](500) NULL
)
SELECT @Return =
case when l.ContactFirstName +' ' +l.ContactLastName <> @LocationUpdate.ContactName then 1 else 0 END
+ CASE WHEN l.ContactEmail <> @LocationUpdate.ContactEmail THEN 1 ELSE 0 END
+ CASE WHEN l.ContactPhone <> @LocationUpdate.ContactPhone THEN 1 ELSE 0 END
+ CASE WHEN l.StatusID <> @LocationUpdate.StatusID THEN 1 ELSE 0 END
+ CASE WHEN l.Zip <> @LocationUpdate.Zip THEN 1 ELSE 0 END,
@Cleared = ISNULL(luh.Cleared, 0)
FROM @LocationUpdate lu, Location l
LEFT JOIN LocationUpdateHistory luh ON @LocationUpdate.ID = luh.idx
WHERE l.TrialID = @TrialID
AND l.City = @LocationUpdate.City
AND l.FacilityName = @LocationUpdate.FacilityName
答案 0 :(得分:3)
显式连接优先于逗号连接。在您的查询中,您实际上仅将LocationUpdateHistory
加入Location
,而不是@LocationUpdate
和Location
的交叉加入,因此您无法引用lu
一种可能的解决方案是将逗号加入替换为CROSS JOIN
,然后lu
子句中可以访问ON
别名。
但您可能还需要提供一些条件,以便 Location
与LocationUpdateHistory
和/或@LocationUpdate
Location
加入WHERE
。
好像你已经拥有了所有必要的条件,只有你将它们保存在WHERE
子句中。因此,只需将逗号连接+ ...
FROM @LocationUpdate lu
INNER JOIN Location l ON l.City = lu.City
AND l.FacilityName = lu.FacilityName
LEFT JOIN LocationUpdateHistory luh ON lu.ID = luh.idx
WHERE l.TrialID = @TrialID
转换为另一个显式连接:
{{1}}
答案 1 :(得分:2)
在查询的选择部分中,将var的名称括在方括号中或使用表别名
支架
SELECT @Return =
case when l.ContactFirstName +' ' +l.ContactLastName <> [@LocationUpdate].ContactName then 1 else 0 END
+ CASE WHEN l.ContactEmail <> [@LocationUpdate].ContactEmail THEN 1 ELSE 0 END
+ CASE WHEN l.ContactPhone <> [@LocationUpdate].ContactPhone THEN 1 ELSE 0 END
+ CASE WHEN l.StatusID <> [@LocationUpdate].StatusID THEN 1 ELSE 0 END
+ CASE WHEN l.Zip <> [@LocationUpdate].Zip THEN 1 ELSE 0 END,
@Cleared = ISNULL(luh.Cleared, 0)
FROM @LocationUpdate , Location l
LEFT JOIN LocationUpdateHistory luh ON [@LocationUpdate].ID = luh.idx
WHERE l.TrialID = @TrialID
AND l.City = [@LocationUpdate].City
AND l.FacilityName = [@LocationUpdate].FacilityName
别名
SELECT @Return =
case when l.ContactFirstName +' ' +l.ContactLastName <> lu.ContactName then 1 else 0 END
+ CASE WHEN l.ContactEmail <> lu.ContactEmail THEN 1 ELSE 0 END
+ CASE WHEN l.ContactPhone <> lu.ContactPhone THEN 1 ELSE 0 END
+ CASE WHEN l.StatusID <> lu.StatusID THEN 1 ELSE 0 END
+ CASE WHEN l.Zip <> lu.Zip THEN 1 ELSE 0 END,
@Cleared = ISNULL(luh.Cleared, 0)
FROM @LocationUpdate lu, Location l
LEFT JOIN LocationUpdateHistory luh ON lu.ID = luh.idx
WHERE l.TrialID = @TrialID
AND l.City = lu.City
AND l.FacilityName = lu.FacilityName
和强>
尝试打开你的连接并使用TOP 1(清晰度),其中赋值变量
SELECT TOP 1
@Return =
case when l.ContactFirstName +' ' +l.ContactLastName <> lu.ContactName then 1 else 0 END
+ CASE WHEN l.ContactEmail <> lu.ContactEmail THEN 1 ELSE 0 END
+ CASE WHEN l.ContactPhone <> lu.ContactPhone THEN 1 ELSE 0 END
+ CASE WHEN l.StatusID <> lu.StatusID THEN 1 ELSE 0 END
+ CASE WHEN l.Zip <> lu.Zip THEN 1 ELSE 0 END,
@Cleared = ISNULL(luh.Cleared, 0)
FROM @LocationUpdate lu
JOIN Location l
ON
l.City = lu.City
AND l.FacilityName = lu.FacilityName
LEFT JOIN LocationUpdateHistory luh ON lu.ID = luh.idx
WHERE l.TrialID = @TrialID