SQL 在存储过程中根据 Id 连接表
SQL Join tables based on Id in Stored Procedure
我需要使用 JOIN 存储过程提取索赔人 ID 的所有保单列。
伪代码:select * 来自 policies(tbl) 其中 parties.id = policy.policyNumber.
这是我目前所拥有的...
CREATE PROCEDURE [dbo].[usp_GetPolicyForClaimentByPolicyIdNumber]
(
@IdNumber varchar(255) = null
)
AS
BEGIN
SELECT *
FROM [BinderCurrent].[Policy]
LEFT JOIN [BinderCurrent].[Policy] ON ([BinderCurrent].[Parties].Id = [BinderCurrent].[PolicyRoles].PolicyId)
WHERE [BinderCurrent].[PolicyRoles].PolicyRoleTypeId = 40
AND (@IdNumber IS NULL OR [BinderCurrent].[Parties].IdNumber LIKE ''+@IdNumber+'%')
ORDER BY Id DESC
END
使用 ON
子句过滤:
SELECT *
FROM [BinderCurrent].[Policy] LEFT JOIN
[BinderCurrent].[Policy]
ON ([BinderCurrent].[Parties].Id = [BinderCurrent].[PolicyRoles].PolicyId AND
[BinderCurrent].[PolicyRoles].PolicyRoleTypeId = 40
WHERE (@IdNumber IS NULL OR [BinderCurrent].[Parties].IdNumber LIKE ''+@IdNumber+'%')
ORDER BY Id DESC;
注意:Table [BinderCurrent].[PolicyRoles]
也应与 JOIN
一起出现。这假定这是您查询的一部分。
我需要使用 JOIN 存储过程提取索赔人 ID 的所有保单列。
伪代码:select * 来自 policies(tbl) 其中 parties.id = policy.policyNumber.
这是我目前所拥有的...
CREATE PROCEDURE [dbo].[usp_GetPolicyForClaimentByPolicyIdNumber]
(
@IdNumber varchar(255) = null
)
AS
BEGIN
SELECT *
FROM [BinderCurrent].[Policy]
LEFT JOIN [BinderCurrent].[Policy] ON ([BinderCurrent].[Parties].Id = [BinderCurrent].[PolicyRoles].PolicyId)
WHERE [BinderCurrent].[PolicyRoles].PolicyRoleTypeId = 40
AND (@IdNumber IS NULL OR [BinderCurrent].[Parties].IdNumber LIKE ''+@IdNumber+'%')
ORDER BY Id DESC
END
使用 ON
子句过滤:
SELECT *
FROM [BinderCurrent].[Policy] LEFT JOIN
[BinderCurrent].[Policy]
ON ([BinderCurrent].[Parties].Id = [BinderCurrent].[PolicyRoles].PolicyId AND
[BinderCurrent].[PolicyRoles].PolicyRoleTypeId = 40
WHERE (@IdNumber IS NULL OR [BinderCurrent].[Parties].IdNumber LIKE ''+@IdNumber+'%')
ORDER BY Id DESC;
注意:Table [BinderCurrent].[PolicyRoles]
也应与 JOIN
一起出现。这假定这是您查询的一部分。