【问题标题】:SQL Join tables based on Id in Stored ProcedureSQL Join表基于存储过程中的Id
【发布时间】:2020-03-11 09:07:46
【问题描述】:

我需要使用 JOIN 存储过程提取索赔人 ID 的所有策略列。

伪代码:select * from policies(tbl) whereparty.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

【问题讨论】:

  • “我需要”不是问题。你在这里问什么?为什么不是你已经不工作的东西?
  • 您在WHERE 中引用了[BinderCurrent].[PolicyRoles],但在您的FROM 中没有定义它。列的 3+ 部分命名也将被弃用和删除。建议您使用别名并使用这些别名来限定您的列。

标签: sql-server stored-procedures jointable


【解决方案1】:

使用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;

注意:表[BinderCurrent].[PolicyRoles] 也应该与JOINs 一起出现。这假设这是您查询的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多